ø( ^_^ )ø
- Provides a minimal, yet powerful set of utility functions
- Never mutates data
- Functions are automatically curried
- Only 6.8kb minified
Methods include:
If there's something you'd like to see added, open an issue or create a pull request on GitHub.
###Installation:
For use with Node/Browserify/webpack:
$ npm install hanuman-js
const H = require('hanuman-js');
For use in the Browser:
$ bower install hanuman-js
<script src="../bower_components/hanuman-js/dist/hanuman.min.js"></script>
###Usage:
Since all methods are automatically curried, they may be used in the following manners:
H.get('a', {a: 44});
H.get('a')({a: 44});
const getA = H.get('a');
getA({a: 44});
###Methods:
clone
Creates a deep copy of the supplied input for all types except functions,
for which a reference is returned.
######*
→ *
const numbers = [1, 2, 3, 4, 5];
const numbersCopy = H.clone(numbers);
numbers === numbersCopy;
const user = {id: '28jd2', name: {first: 'Albert' , last: 'King' }, age: 55};
const userCopy = H.clone(user);
user === userCopy;
user.id === userCopy.id
user.name === userCopy.name
user.age === userCopy.age
const add = (a,b,c) => a + b + c;
H.clone(add) === add; \\ returns true
H.clone(44);
H.clone('copy');
H.clone(true);
H.clone(null);
H.clone(undefined);
contains
Returns true if the supplied list contains the target item. Equality is determined using equals.
######*
→ array
→ boolean
const containsTwo = H.contains(2);
containsTwo([1, 2, 3, 4, 5]);
containsTwo([1, 3, 5, 7, 9]);
const containsTim = H.contains({name: 'tim', age: 54});
const users = [{name: 'lili' , age: 33 }, {name: 'tim', age: 54}];
containsTim(users);
const obj = {a: 44, b: 55};
H.contains('a', Object.keys(obj));
curry
Returns a curried version of the supplied function.
######function
→ function
const add = (a,b,c) => a + b + c;
const addTen = H.curry(add)(10);
addTen(2,3);
addTen(2)(3);
const addFifteen = addTen(5);
addFifteen(5);
equals
Returns true if the supplied items are equivalent, i.e. deep equal
.
######*
→ *
→ boolean
H.equals(1, 1);
H.equals(1, '1');
H.equals(undefined, null);
H.equals([1, 2, 3], [1, 2, 3]);
H.equals({a: 44, b: {c: 55}}, {a: 44, b: {c: 55}});
filter
Applies a predicate function to a list of values and returns a new list of values which pass the test
######function
→ array
→ array
const isEven= (a) => a % 2 === 0;
const numbers = [1, 2, 3, 4, 5];
const getEvens = H.filter(isEven);
getEvens(numbers);
find
Returns the first item in the list that matches the predicate, or undefined if not found
######function
→ array
→ * | undefined
const isEven= a => a % 2 === 0;
const numbers = [1, 2, 3, 4, 5];
const odds = [1, 3, 5, 7, 9];
H.find(isEven, numbers);
H.find(isEven, odds);
const users = [
{ name: 'lili' , age: 33 },
{ name: 'tim', age: 54 }
];
const isTim = H.get('name') === 'tim';
H.find(isTim, users);
forEach
Applies a function to each item in the collection. If the collection is an array, the iterator function will receive the value, index, and array. If the collection is an object, the iterator function will receive the value, key, and object.
######function
→ array
→ *
const log = (v, i, list) => console.log(v);
const numbers = [1, 2, 3, 4, 5];
const logEach = H.curry(log);
logEach(numbers);
const logObject = (v, k, obj) => console.log(`${k}: ${v}`);
const user = {id: '28jd2', name: {first: 'Albert' , last: 'King' }, age: 55};
H.forEach(logObject, user)
forEachBreak
Identical to forEach, except a predicate function is taken as the second parameter to allow for early termination of the iteration process. The predicate function accepts the same parameters as the iterator function. Since the order of object keys cannot be guaranteed, it is not possible to determine when termination will occur for objects.
######function
→ array
→ *
let result = [];
const numbers = [1, 2, 3, 4, 5];
const updateResult = v => result.push(v);
const greaterThanTwo = v => v > 2;
H.forEachBreak(updateResult, greaterThanTwo, numbers);
console.log(result);
result = {};
const copyToResult = (v, k, obj) => result[key] = v;
const isName = (v, k) => k === 'name';
const user = {id: '28jd2', name: {first: 'Albert' , last: 'King' }, age: 55};
H.forEachBreak(copyToResult, isName, user);
console.log(result.name);
get
Returns a property from an object, or undefined if it doesn't exist. To retrieve a nested property, a period-delimited string or an array of keys may be passed as the first argument
######string | array
→ object
→ * | undefined
const user = {id: '28jd2', name: {first: 'Albert' , last: 'King' }, age: 55};
H.get('id', user);
const getFirstName = H.get('name.first');
getFirstName(user);
H.get('name.middle', user);
H.get(['name', 'last'], user);
getOr
Returns a property from an object, or a specified default if it doesn't exist. To retrieve a nested property, a period-delimited string or an array of keys may be passed as the second argument
######*
→ string | array
→ object
→ *
const user = {
id: '28jd2',
name: {first: 'Albert' , last: 'King' },
age: 55
};
const getOrUnknown = H.getOr('unknown');
const getMiddleName = getOrUnknown('name.middle');
getOrUnknown('age', user);
getMiddleName(user);
isEmpty
######* cannot be curried *
Returns a boolean indicating whether or not the given input is empty
######string | array | object
→ boolean
H.isEmpty('');
H.isEmpty('empty');
H.isEmpty([]);
H.isEmpty([1, 2, 3]);
H.isEmpty({});
H.isEmpty({a: 'empty'});
H.isEmpty(0);
H.isEmpty(44);
H.isEmpty(null);
H.isEmpty(undefined);
map
Creates a new array or object by applying a function to each value in the array or object property
######function
→ array | object
→ array | object
const square = (a) => a * a;
const numbers = [1, 2, 3, 4, 5];
H.map(square, numbers);
const double = x => x * 2;
H.map(double, { a: 1, b: 2, c: 3 });
omit
Returns a copy of the supplied object containing all keys except those specified to be omitted. The opposite of pick.
######array
→ object
→ object
const fruit = {
a: 'apple',
b: 'banana',
c: 'cherry',
d: 'date',
e: 'elderberry'
};
H.omit(['a', 'b', 'd'], fruit)
pick
Returns a new object by copying properties from the supplied object. Undefined properties are not copied to the new object.
######array
→ object
→ object
const fruit = {
a: 'apple',
b: 'banana',
c: 'cherry',
d: 'date',
e: 'elderberry'
};
H.pick(['a', 'b', 'd'], fruit)
H.pick(['a', 'f'], fruit)
pickAll
Returns a new object by copying properties from the supplied object. Undefined properties are copied to the new object.
######array
→ object
→ object
const fruit = {
a: 'apple',
b: 'banana',
c: 'cherry',
d: 'date',
e: 'elderberry'
};
H.pickAll(['a', 'b', 'd'], fruit)
H.pickAll(['a', 'f'], fruit)
pipe
######* cannot be curried *
Creates a composed function by chaining the provided functions from left to right. The first function in the chain may accept any number of arguments. The remaining functions may only accept a single argument.
######...function
→ function
const double = x => x * 2;
const subtractTen = x => x - 10;
H.pipe(subtractTen, double)(22)
const evens = [2, 4, 6, 8, 10, 12];
const addTwo = (a,b) => a + b;
const doubleFirstPlus44 = H.pipe(H.get(0), double, H.curry(addTwo)(44));
doubleFirstPlus44(evens)
range
Returns a list of sequential numbers
######number
→ number
→ array
H.range(1,5);
H.range(15,15);
H.range(1,0);
const startAtTwelve = H.range(12);
startAtTwelve(13);
startAtTwelve(17);
reduce
Applies an iterator function to an accumulator and the current value of the list, successively returning a single value
######function
→ *
→ array
→ *
const add = (a, b) => a + b;
const numbers = [1, 2, 3, 4, 5];
H.reduce(add, 0, numbers);
H.reduce(add, 10, numbers);
const isEven= (a) => a % 2 === 0;
const evenSquares = (acc, v) => {
if ( isEven(v) ) {
acc[v] = v * v;
}
return acc;
}
H.reduce(evenSquares, {}, numbers);
reject
Applies a predicate function to a list of values and returns a new list containing the values that do not pass the test
######function
→ array
→ array
const isEven= a => a % 2 === 0;
const numbers = [1, 2, 3, 4, 5];
H.reject(isEven, numbers);
scan
Applies an iterator function to an accumulator and each value in a a list, returning a list of successively reduced values
######function
→ *
→ array
→ array
const add = (a, b) => a + b;
const numbers = [1, 2, 3, 4, 5];
H.scan(add, 0, []);
H.scan(add, 0, numbers);
H.scan(add, '', ['a', 'b', 'c']);