Skip to content

Instantly share code, notes, and snippets.

@danrspencer
danrspencer / sizeOf.js
Last active August 15, 2016 14:08
Get in memory size of a JavaScript value/object
const typeSizes = {
'boolean': () => 4,
'number': () => 8,
'string': (item) => 2 * item.length,
'object': (item) => Object.keys(item).reduce(
(sum, key) => sum + sizeOf(key) + sizeOf(item[key]), 0
)
};
export default const sizeOf = (value) => typeSizes[typeof value](value);
@danrspencer
danrspencer / create_git_undo.sh
Created August 25, 2016 13:08
Setup Git undo alias
#!/bin/bash
# Credit to: http://megakemp.com/2016/08/25/git-undo/
git config --global alias.undo '!f() { \
git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; \
}; f'
@danrspencer
danrspencer / validateAgainstSchema.php
Last active April 29, 2021 06:43
Validate a PHP array against a schema to ensure all desired keys are present (supports nested)
function validateAgainstSchema(array $schema, $data) {
$valid = true;
$done = false;
$path = "";
$current = each($schema);
while($valid !== false && $current !== false) {
list($key, $value) = $current;
@danrspencer
danrspencer / delete_unused_files.sh
Last active April 4, 2017 15:40
Delete unused files in a static website
#!/usr/bin/env bash
TEMP="./tmp"
REGEX="img\/[^>]+?(\"|\)|'| )"
ROOTPATH="./public"
PATH_TO_CLEAN="./public/img"
egrep -iowhr "$REGEX" * | rev | cut -c 2-1000 | rev | xargs -I {} sh -c "mkdir -p $TEMP/{} && rm -r $TEMP/{} && cp $ROOTPATH/{} $TEMP/{}"
@danrspencer
danrspencer / setup-shell.sh
Last active August 31, 2023 14:41
Setting up shell on new machine
#!/usr/bin/env bash
# Setup the dev volume: https://brianboyko.medium.com/a-case-sensitive-src-folder-for-mac-programmers-176cc82a3830
set -e
# Install Brew
# https://brew.sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
@danrspencer
danrspencer / curry.js
Created June 5, 2017 09:28
Chicken Korma
function curry(func, ...args) {
return (...moreArgs) => func(...args, ...moreArgs);
}
const target = (a, b, c, d, e) => a+b+c+d+e;
const abTarget = curry(target, "a", "b");
const abcdTarget = curry(abTarget, "c", "d");
@danrspencer
danrspencer / fn.js
Last active June 29, 2017 11:04
Some functions to allow a more functional paradigm in js
export const compose = (...fns) =>
fns.reduce((f, g) => (...args) => f(g(...args)));
export const pipe = (...fns) => compose.apply(compose, fns.reverse());
export const partial = (fn, [args]) => (...moreArgs) =>
fn(...[args, ...moreArgs]);
@danrspencer
danrspencer / fix_git_email.sh
Last active June 15, 2018 20:50
Fix Committer Email
#!/usr/bin/env bash
git config user.email "[email protected]"
git filter-branch -f --env-filter '
NAME="Dan Spencer"
EMAIL="[email protected]"
if [ "$GIT_COMMITTER_NAME" = "$NAME" ]
then
export GIT_COMMITTER_EMAIL="$EMAIL"
@danrspencer
danrspencer / update.js
Created June 20, 2018 07:36
Use lenses to update state in reducers
const update = (lensGenerator, func) => (state, payload) =>
over(
lensGenerator(payload),
obj => ({ ...obj, ...func(payload, obj, state) }),
state
);
@danrspencer
danrspencer / gist:0b75870b9c0d69fbc0a4011d68524edf
Created October 11, 2018 13:44
Open all pages for git commits matching regex
git log --grep "##regex##" --basic-regexp --pretty=format:%h | xargs -I '{}' open https://github.com/danrspencer/repo/commit/'{}'