// array utils | |
// ================================================================================================= | |
const combine = (...arrays) => [].concat(...arrays); | |
const compact = arr => arr.filter(Boolean); | |
const contains = (() => Array.prototype.includes | |
? (arr, value) => arr.includes(value) | |
: (arr, value) => arr.some(el => el === value) | |
)(); | |
const difference = (arr, ...others) => { | |
var combined = [].concat(...others) | |
return arr.filter(el => !combined.some(exclude => el === exclude)) | |
}; | |
const head = arr => arr[0]; | |
const initial = arr => arr.slice(0, -1); | |
const intersection = (...arrays) => | |
[...Set([].concat(...arrays))].filter(toFind => | |
arrays.every(arr => arr.some(el => el === toFind)) | |
); | |
const last = arr => arr.slice(-1)[0]; | |
const sortedIndex = (arr, value) => | |
[value].concat(arr).sort().indexOf(value); | |
const tail = arr => arr.slice(1); | |
const toArray = (() => Array.from ? Array.from : obj => [].slice.call(obj))(); | |
const union = (...arrays) => [...Set([].concat(...arrays))]; | |
const unique = arr => [...Set(arr)]; | |
const without = (arr, ...values) => | |
arr.filter(el => !values.some(exclude => el === exclude)); | |
// object utils | |
// ================================================================================================= | |
const getValues = obj => Object.keys(obj).map(key => obj[key]); | |
const merge = (() => { | |
const extend = Object.assign ? Object.assign : (target, ...sources) => { | |
sources.forEach(source => | |
Object.keys(source).forEach(prop => target[prop] = source[prop]) | |
); | |
return target; | |
}; | |
return (...objects) => extend({}, ...objects); | |
})(); | |
const toMap = (() => { | |
const convert = obj => new Map(Object.keys(obj).map(key => [key, obj[key]])); | |
return obj => obj instanceof Map ? obj : convert(obj); | |
})(); | |
// math | |
// ================================================================================================= | |
const min = arr => Math.min(...arr); | |
const max = arr => Math.max(...arr); | |
const sum = arr => arr.reduce((a, b) => a + b); | |
const product = arr => arr.reduce((a, b) => a * b); | |
// function decorators | |
// ================================================================================================= | |
const not = fn => (...args) => !fn(...args); | |
const maybe = fn => | |
(...args) => { | |
if (args.length < fn.length || args.some(arg => arg == null)) return; | |
return fn(...args); | |
}; | |
const once = fn => { | |
var done = false; | |
return (...args) => { | |
if (done) return; | |
done = true; | |
fn(...args); | |
}; | |
}; | |
const curry = fn => { | |
const arity = fn.length; | |
const curried = (...args) => | |
args.length < arity ? (...more) => curried(...args, ...more) : fn(...args); | |
return curried; | |
}; | |
const pipeline = (...funcs) => | |
value => funcs.reduce((a, b) => b(a), value); |
This comment has been minimized.
This comment has been minimized.
pratttttt
commented
Jan 12, 2015
Among the functions, not sure if "initial" makes sense semantically. dropLast or butLast or pop might be a better choice. |
This comment has been minimized.
This comment has been minimized.
nfroidure
commented
Jan 12, 2015
Added noop https://gist.github.com/nfroidure/00e0ef49433809abe13b it's often useful |
This comment has been minimized.
This comment has been minimized.
webbedspace
commented
Jan 13, 2015
|
This comment has been minimized.
This comment has been minimized.
@webbedspace: You're absolutely right, just updated the code. Thanks! |
This comment has been minimized.
This comment has been minimized.
mathiasbynens
commented
Jan 19, 2015
@nfroidure That’s the identity function. |
This comment has been minimized.
This comment has been minimized.
nfroidure
commented
Jan 19, 2015
@mathiasbynens right! thanks for the correction |
This comment has been minimized.
This comment has been minimized.
goatslacker
commented
May 15, 2015
Why is head: const head = arr
=> arr[0] and last not: const last = arr => arr[arr.length - 1] |
This comment has been minimized.
This comment has been minimized.
WebReflection
commented
May 15, 2015
FYI: assign should eventually be shimmed via |
This comment has been minimized.
This comment has been minimized.
o11c
commented
May 16, 2015
Most of these have horrible asymptotic performance. |
This comment has been minimized.
This comment has been minimized.
joeybaker
commented
May 16, 2015
const identity = x => x
const compact = arr => arr.filter(identity) Or even const compact = arr => arr.filter(Boolean) |
This comment has been minimized.
This comment has been minimized.
@joeybaker: Cleaner indeed, updated! Thanks :) |
This comment has been minimized.
This comment has been minimized.
VictorChatterji
commented
Oct 31, 2017
you sir ! are a legend. |
This comment has been minimized.
bendc commentedJan 12, 2015
combine(arrays)
Combine multiple arrays into one array.
compact(array)
Returns a copy of the array with all falsy values removed.
contains(array, value)
Returns true if the value is present in the array.
difference(array, others)
Similar to
without
, but returns the values from array that are not present in the other arrays.head(array)
Returns the first element of an array.
initial(array)
Returns everything but the last entry of the array.
intersection(arrays)
Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.
last(array)
Returns the last element of an array.
sortedIndex(array, value)
Determine the index at which the value should be inserted into the array in order to maintain the array's sorted order.
tail(array)
Returns everything but the first entry of the array.
toArray(arrayLike)
Returns a real Array. Useful for transmuting the arguments object.
union(arrays)
Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.
unique(array)
Produces a duplicate-free version of the array.
without(array, values)
Returns a copy of the array with all instances of the values removed.
getValues(object)
Returns an array with the object's values.
merge(objects)
Combine multiple objects into a new object.
toMap(object)
Convert an Object to a Map.
min(array)
Returns the minimum value in the array.
max(array)
Returns the maximum value in the array.
sum(array)
Returns the sum of all values in the array.
product(array)
Returns the product of all values in the array.
not(function)
Creates a new function returning the opposite of the function provided as its argument.
maybe(function)
Returns a new function that won't execute if not enough arguments are provided.
once(function)
Returns a new function that won't execute more than once.
curry(function)
Curries a function.
pipeline(functions)
Returns the composition of multiple functions from left to right.