Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
@stefanfrede
stefanfrede / flipandcurry_recipe.js
Created August 3, 2016 05:22
flipAndCurry is a function that takes a function as argument, “flips” the order of arguments around, and then curries it.
const flipAndCurry = (fn) =>
(first) => (second) => fn(second, first);
// Example:
// https://gist.github.com/stefanfrede/596597d8b544de08491364eab20053c6
const mapWith = flipAndCurry(map);
// flipAndCurry throws the current context away, so it can’t be used to flip methods.
// A small alteration gets the job done:
const flipAndCurry = (fn) =>
@stefanfrede
stefanfrede / object_assign_recipe.js
Created August 3, 2016 05:28
Extend objects by assigning properties.
// Copy an object by extending an empty object:
Object.assign({}, {
apples: 12,
oranges: 12
})
//=> { apples: 12, oranges: 12 }
// Extend one object with another:
const inventory = {
apples: 12,
@stefanfrede
stefanfrede / memoized_recipe.js
Created August 19, 2016 06:28
Use a lookup table for recursive functions to look up results to avoid recalculation
const memoized = (fn) => {
const lookupTable = {};
return function (...args) {
const key = JSON.stringify(this, args);
return lookupTable[key] || (lookupTable[key] = fn.apply(this, args));
}
}
@stefanfrede
stefanfrede / getwith_recipe.js
Created August 20, 2016 06:03
getWith takes the name of an attribute and returns a function that extracts the value of that attribute from an object.
const getWith = (attr) => (object) => object[attr]
/**
* Examples:
*/
const inventory = {
apples: 0,
oranges: 144,
eggs: 36
};
@stefanfrede
stefanfrede / pluckwith_recipe.js
Created August 22, 2016 05:23
Combine the mapWith and the getWith recipe.
const pluckWith = (attr) => mapWith(getWith(attr));
// or even better
const pluckWith = compose(mapWith, getWith);
/**
* Example
*/
const inventories = [
@stefanfrede
stefanfrede / deepmapping_recipe.js
Created August 23, 2016 06:08
Working with arrays that represent trees.
const deepMapWith = (fn) =>
function innerdeepMapWith (tree) {
return Array.prototype.map.call(tree, (element) =>
Array.isArray(element)
? innerdeepMapWith(element)
: fn(element)
);
}
/**
@stefanfrede
stefanfrede / SimpleStore.js
Last active July 28, 2017 13:32 — forked from ksafranski/SimpleStore.js
Simple localStorage function with Cookie fallback for older browsers.
/**
* Simple [local|session]Storage with Cookie Fallback
* v.1.2.0
*
* USAGE:
* ----------------------------------------
* Set New / Modify:
* store({
* storage: {
* key: 'my_key',
@stefanfrede
stefanfrede / uuidv4.js
Created November 29, 2018 21:37
A RFC4122 version 4 compliant UUID solution
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
console.log(uuidv4());
@stefanfrede
stefanfrede / README-Template.md
Created June 20, 2019 18:58 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@stefanfrede
stefanfrede / switch-delegator.js
Created June 29, 2019 05:41
A alternate approach to write a switch statement that supports composability and reusability.
// Example returns for illustration only.
const cases = {
alpha() {
return [ "Alpha", arguments.length ];
},
beta() {
return [ "Beta", arguments.length ];
},
_default() {
return [ "Default", arguments.length ];