Skip to content

Instantly share code, notes, and snippets.

View davidmyersdev's full-sized avatar

David Myers davidmyersdev

View GitHub Profile
const app = async () => {
// encrypt message
const first = 'Hello, World!'
const key = await generateKey()
const { cipher, iv } = await encrypt(first, key)
// pack and transmit
await fetch('/secure-api', {
method: 'POST',
body: JSON.stringify({
█▄▄▄ ▄
█▄█▄ ▄▄█ ▄█▄ ▄▄ ███ ▄▄█ ▄▄▄
@davidmyersdev
davidmyersdev / atom.sh
Last active November 12, 2022 20:42
Launch Atom from the Windows Subsystem for Linux (WSL)
#!/bin/bash
# only convert the path if one is provided
if [ ! -z $1 ]
then
linuxPath="$(realpath $1)"
windowsPath="$(wslpath -w $linuxPath)"
fi
# avoid the UNC path warning
function doTheThing(element) {
let children = Array.from(element.childNodes);
if (children.length) {
children.forEach(child => doTheThing(child));
} else {
element.textContent = 'hi';
}
}
@davidmyersdev
davidmyersdev / interpolating-data-into-rails-views-view.html.erb
Created April 1, 2020 23:32
Interpolating data into Rails views -- View segment
<!-- app/views/notifications/index.html.erb -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/he/1.2.0/he.min.js" integrity="sha256-awnFgdmMV/qmPoT6Fya+g8h/E4m0Z+UFwEHZck/HRcc=" crossorigin="anonymous"></script>
<script>
const notifications = JSON.parse(
// Rails encodes special characters for our security when interpolating strings into views
// the he library allows us to safely convert those characters back
he.decode('<%= notifications.to_json %>')
);
// do something with the notifications
@davidmyersdev
davidmyersdev / interpolating-data-into-rails-views-controller.rb
Created April 1, 2020 23:03
Interpolating data into Rails views -- Controller segment
# app/controllers/notifications_controller.rb
class NotificationsController < ApplicationController
helper_method :notifications
def index
# automatically renders app/views/notifications/index.html.erb
end
private
@davidmyersdev
davidmyersdev / promise.rb
Created November 6, 2019 21:35
A rudimentary implementation of promises for Ruby.
class Promise
def initialize(&callback)
@callback = callback
@next_promise = nil
end
def resolve(callback_result = nil)
callback.call(callback_result).tap do |result|
next_promise.resolve(result) if next_promise
end
@davidmyersdev
davidmyersdev / snippets.rb
Last active March 26, 2020 14:43
Random code snippets
# generate a Rails migration timestamp
Time.now.strftime('%Y%m%d%H%M%S')
Set Weight Reps Rest
Set 1 205 (100%) AMRAP 3 minutes
Set 2 185 (90%) AMRAP 3 minutes
Set 3 165 (80%) AMRAP 5 minutes
Set Weight Reps Rest
Set 1 200 (100%) 8 3 minutes
Set 2 180 (90%) 9 3 minutes
Set 3 160 (80%) 9 5 minutes