whisk
Whisk is a collection of the functional operation helpers (underscore-like)
for working with map, filter, reduce, etc.
chain
Create a function that will create the combined result of calling all
the provided functions in the provided order.
var chain = require('whisk/chain');
function add1(value) {
return value + 1;
}
function add5(value) {
return value + 5;
}
console.log(chain(add1, add5)(0));
flatten
Flatten an array using [].reduce
var flatten = require('whisk/flatten');
console.log([1, [2, 3], 4, [5]].reduce(flatten));
length
Get the length of the target.
var length = require('whisk/length');
console.log(length([1, 2, 3]));
console.log(length('Hello'));
console.log(['Hi', 'there'].map(length));
match
Given a set of properties (A), test whether another object (B) has properties
that match the values specified in A. Additional properties in B are not
accounted for when determining the match.
var match = require('whisk/match');
var isH1 = match({ type: 'heading', level: 1 });
console.log(isH1({ type: 'code' }));
console.log(isH1({ type: 'heading', level: 2 }));
console.log(isH1({ type: 'heading', level: 1 }));
console.log(isH1({ type: 'heading', level: 1, color: 'red' }));
not
Designed to be used in combination with an [].filter
the not
function
can be used to exlude items from an array.
var not = require('whisk/not');
var items = ['a', 'b', 'c', 'd', 'e'];
console.log(items.filter(not('a')));
console.log(items.filter(not(['a', 'b'])));
console.log(items.filter(not('a', 'b')));
nub
Return only the unique elements of the list.
var nub = require('whisk/nub');
console.log(nub([ 1, 2, 3, 2, 3, 4 ]));
console.log(nub([ 'red', 'blue', 'red' ]));
console.log(nub([ 4, 6, 6, 8, 3, 4, 1 ]));
pluck
Extract targeted properties from a source object. When a single property
value is requested, then just that value is returned.
In the case where multiple properties are requested (in a varargs calling
style) a new object will be created with the requested properties copied
across.
NOTE: In the second form extraction of nested properties is
not supported.
var pluck = require('whisk/pluck');
var people = [
{ name: 'Bob', age: 35, address: { country: 'Australia' } },
{ name: 'Thelma', age: 32, address: { country: 'New Zealand' } },
{ name: 'Roger', age: 50, address: { country: 'Fiji' } }
];
console.log(people.map(pluck('name')));
console.log(people.map(pluck('address.country')));
console.log(people.map(pluck('name', 'age')));
range
Create an array of elements from x -> y (inclusive)
var pluck = require('whisk/range');
console.log(range(0, 3));
sum
Sum an input array of values
var sum = require('whisk/sum');
var range = require('whisk/range');
console.log(sum([1, 2]));
console.log(sum(range(1, 10)));
console.log([1, 2].reduce(sum));
console.log(range(1, 10).reduce(sum));
times
Create an element of arrays that can be iterated over n times:
var times = require('whisk/times');
times(3).forEach(function() {
console.log('hello world');
});
within
Check whether a value is within a specified array of values:
var within = require('whisk/within');
var isKnownFruit = within(['apple', 'orange', 'banana']);
console.log(isKnownFruit('apple'));
console.log(isKnownFruit('grape'));
console.log(['apple', 'grape', 'banana'].filter(isKnownFruit));
zip
zip one array with other arrays
var zip = require('whisk/zip');
console.log([1, 2, 3].map(zip(['a', 'b', 'c'])));
console.log([1, 2, 3].map(zip(['a', 'b', 'c'], ['x', 'y', 'z'])));
zip todo
License(s)
MIT
Copyright (c) 2015 Damon Oehlman damon.oehlman@gmail.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.