Skip to content

Instantly share code, notes, and snippets.

@erikjung
erikjung / gist:3030803
Created July 2, 2012 03:20
Storing and fetching image data from MongoDB with PHP
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
/*
export const memoizedAdd = () => {
const cache = new Map();
return (...args) => {
const key = args.join();
const cached = cache.get(key);
if (cached) {
console.log(`Returning cached value for ${key}`);
return cached;
} else {
const result = args.reduce((total, val) => total + val);
@erikjung
erikjung / index.js
Created April 18, 2018 01:50
Merging results of Promise.all
const a = new Promise(r => r({ a: 'A' }));
const b = new Promise(r => r({ b: 'B' }));
const c = new Promise(r => r({ c: 'C' }));
const result = (async () => {
const abc = await Promise.all([a, b, c]);
return abc.reduce((result, obj) => ({...result, ...obj}), {});
})();
@erikjung
erikjung / fp-array-utils.js
Created March 2, 2018 10:52
A snippet to generate FP array one-liners
const ArrayUtils = [
'map',
'filter',
'find',
]
.reduce((utils, method) => ({
...utils,
[method]: (fn, arr) => arr ? arr[method](fn) : arr => arr[method](fn)
}), {})
export default const renameProps = (map, obj) =>
Object.entries(map).reduce((accum, [a, b]) => {
delete accum[a]
return obj[a]
? Object.assign(accum, {[b]: obj[a]})
: accum
}, {...obj})
@erikjung
erikjung / config.rb
Created July 11, 2012 23:50
Testing Compass Vertical Rhythm Loveliness
http_path = "/"
css_dir = ""
sass_dir = ""
images_dir = ""
javascripts_dir = ""
output_style = :expanded
line_comments = false
preferred_syntax = :scss
@erikjung
erikjung / gist:3079806
Created July 9, 2012 23:39
PHP 5.4 Namespace-based Autoloader
<?php
// Probably unnecessary, but wanted to test the waters of 5.4
trait NamespaceConverter
{
function nsToPath($class) {
return str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
}
}
/**
* Tweens a number from `startValue` to `endValue` over the `duration` time period,
* calling `onAnimationFrame` on each animation frame.
*
* @example
* // Tween to 100 from 0 over 2 seconds
* tweenNumber(n => console.log(`step value: ${n}`), 100, 0, 2000);
*/
function tweenNumber (
onAnimationFrame,
module.exports = function leftpad (str, len, ch) {
const val = String(str);
const pad = ch === undefined ? ' ' : String(ch);
return pad.repeat(Math.max(0, len - val.length), pad) + val;
};
@erikjung
erikjung / zipObject.js
Last active February 27, 2016 20:17
This combines two arrays into an object.
const zipObject = (keys, vals) => {
return keys.reduce(
(prev, val, i) => Object.assign(prev, { [val]: vals[i] }), {}
)
}