Skip to content

Instantly share code, notes, and snippets.

Avatar

zspecza zspecza

View GitHub Profile
@zspecza
zspecza / setArity.js
Last active Dec 6, 2016
sets the length property of a given function to the given arity
View setArity.js
function setArity (arity, fn) {
if (typeof setArity.cache === 'undefined') {
setArity.cache = {}
}
if (typeof setArity.cache[arity] === 'undefined') {
setArity.cache[arity] = (fn) => {
switch (arity) {
case 0: return function () { return fn.apply(this, arguments) }
case 1: return function (a) { return fn.apply(this, arguments) }
case 2: return function (a, b) { return fn.apply(this, arguments) }
@zspecza
zspecza / curryN.js
Last active Apr 12, 2017
curryN with placeholder support (not optimized)
View curryN.js
// this file is just so I can reference the idea behind placeholders - if you want a maintained version, check out:
// - https://github.com/thisables/curry
// - http://ramdajs.com
const __ = Symbol
const ARITIES = {}
const setArity = (arity, fn) => {
if (!ARITIES[arity]) {
View composition.js
const users = {
1: { name: 'bob' },
2: { name: 'sally' }
}
const posts = [
{ author: 'sally', title: 'Hello World' },
{ author: 'bob', title: 'How to do stuff' }
]
View sequentialCallback.js
function sequentialCallback (...fns) {
return (...args) => {
const done = args.pop()
const next = (error, ...args) => {
if (error) return done(error)
if (fns.length) {
const fn = fns.shift()
return fn(...args, next)
}
return done(null, ...args)
View multiple-values.js
const validateTransformation = sequentialCallback(
transformSomething,
validateTransformed,
(validated, callback) => callback(null, validated, 'data is valid:')
)
validateTransformation({ foo: 'bar' }, (error, validated, label) => {
if (error) {
console.error(new Error(error))
} else {
View passing-data.js
const validateTransformation = sequentialCallback(
transformSomething,
validateSomething
)
const something = { foo: 'bar' }
validateTransformation(something, (error, validated) => {
if (error) {
console.error(new Error(error))
View sequential-callbacks.js
const getValidatedTransformation = sequentialCallback(
findSomething,
transformSomething,
validateTransformed
)
getValidatedTransformation((error, validated) => {
if (error) {
console.error(new Error(error))
} else {
View point-free-promise.js
findSomething()
.then(transformSomething)
.then(validateTransformed)
.then((validated) => console.log('data is valid:', validated))
.catch((error) => console.error(new Error(error))
View promise-heaven.js
findSomething()
.then((something) => transformSomething(something))
.then((transformed) => validateTransformed(transformed))
.then((validated) => console.log('data is valid:', validated)
.catch((error) => console.error(new Error(error))
View callback-hell.js
findSomething((error, something) => {
if (error) {
throw new Error(error)
}
transformSomething(something, (error, transformed) => {
if (error) {
throw new Error(error)
}
validateTransformed(transformed, (error, validated) => {
if (error) {
You can’t perform that action at this time.