Chunks

A super duper, teeny tiny functional library.

Chunks

API

Collections

Every iterator returns the value first, then the key and lastly the object itself. The only exception to this rule is Chunks.reduce which returns the previous value first and then follows the same convention as the others.

Chunks.forEach / Chunks.each

Chunks.forEach(obj, iterator[, scope]);

Executes the iterator on every item in the obj. The scope is the value of this.

Examples
var results = [];

Chunks.forEach([1, 2, 3], function(v) {
  results.unshift(v);
});
// [3, 2, 1]
var results = [];

Chunks.forEach({one: 1, two: 2, three: 3}, function(v, k) {
  results.unshift(k);
});
// ['three', 'two', 'one']

Chunks.map

Chunks.map(obj, iterator[, scope]);

Returns a new and transformed array by executing the iterator on every item in the obj. The scope is the value of this.

Examples
Chunks.map([1, 2, 3], function(v) {
  return v + 1;
});
// [2, 3, 4]
Chunks.map({one: 1, two: 2, three: 3}, function(v, k) {
  return k + '!';
});
// ['one!', 'two!', 'three!']

Chunks.filter

Chunks.filter(obj, iterator[, scope]);

Returns a new and transformed array by executing the iterator on every item in the obj. Only the items that evalute to true in the iterator will be added to the array. The scope is the value of this.

Examples
Chunks.filter([1, 2, 3], function(v) {
  return v % 2;
});
// [1, 3]
Chunks.filter({one: 1, two: 2, three: 3}, function(v, k) {
  return (k.charAt(0) === 't');
});
// [2, 3]

Chunks.reduce

Chunks.reduce(obj, iterator, memo[, scope]);

Returns a single value by executing the iterator on every item in the obj and passing the previous value, memo, to the iterator. The scope is the value of this.

Examples
Chunks.reduce([1, 2, 3], function(m, v) {
  return m + v;
}, 0);
// 6
Chunks.reduce({one: 1, two: 2, three: 3}, function(m, v) {
  return m + v;
}, '');
// '123'

Chunks.some

Chunks.some(obj, iterator[, scope]);

Returns either true or false by executing the iterator on every item in the obj. It'll return true if one of the items in the obj matches the iterator. The scope is the value of this.

Examples
Chunks.some([1, 2, 3], function(v) {
  return v === 3;
});
// true
Chunks.some({one: 1, two: 2, three: 3}, function(v, k) {
  return k === 'two';
});
// true

Chunks.every

Chunks.every(obj, iterator[, scope]);

Returns either true or false by executing the iterator on every item in the obj. It'll return true if all of the items in the obj matches the iterator. The scope is the value of this.

Examples
Chunks.every([1, 2, 3], function(v) {
  return v > 2;
});
// false
Chunks.every({one: 1, two: 2, three: 3}, function(v, k) {
  return (k.charAt(0) === 't');
});
// false

Chunks.index

Chunks.index(obj, item);

Returns the index of the item in the obj. It returns the index of the item, or -1 if nothing is found.

It delegates to indexOf whenever it can, albeit without the option of choosing the start index (hence the different name).

Examples
Chunks.index([1, 2, 3], 2);
// 1
Chunks.index({one: 1, two: 2, three: 3}, 2);
// 'two'

Chunks.contains

Chunks.contains(obj, item);

Determines if the obj contains the item. It returns either true or false.

Examples
Chunks.contains([1, 2, 3], 2);
// true
Chunks.contains({one: 1, two: 2, three: 3}, 2);
// true

Chunks.size

Chunks.size(obj);

Returns the size of the obj.

Examples
Chunks.size(['a', 'b', 'c']);
// 3
Chunks.size({one: 1, two: 2, three: 3});
// 3

Chunks.clone

Chunks.clone(obj);

Returns a clone of the obj.

Examples
Chunks.clone([1, 2, 3]);
// [1, 2, 3]
Chunks.clone({one: 1, two: 2, three: 3});
// {one: 1, two: 2, three: 3}

Chunks.sort

Chunks.sort(obj[, comp]);

Returns a new array with the values from the obj sorted, either by lexicographical order or the comp function.

Examples
Chunks.sort(['a', 'd', 'b', 'e', 'c', 'f']);
// ['a', 'b', 'c', 'd', 'e', 'f']
Chunks.sort([1, 10, 2, 20, 3, 30], function(a, b) {
  return a - b;
});
// [1, 2, 3, 10, 20, 30]
Chunks.sort({one: 'a', four: 'd', two: 'b', five: 'e', three: 'c', six: 'f'});
// ['a', 'b', 'c', 'd', 'e', 'f']
Chunks.sort({one: 1, ten: 10, two: 2, twenty: 20, three: 3, thirty: 30}, function(a, b) {
  return a - b;
});

Types

Chunks.isArray

Chunks.isArray(obj);

Determines if the obj is an array. It returns either true or false.

Examples
Chunks.isArray([1, 2, 3]);
// true
Chunks.isArray({one: 1, two: 2, three: 3});
// false

Chunks.isObject

Chunks.isArray(obj);

Determines if the obj is an object. It returns either true or false.

Examples
Chunks.isObject({one: 1, two: 2, three: 3});
// true
Chunks.isObject([1, 2, 3]);
// false

Arrays

Chunks.first

Chunks.first(arr);

Returns the first item in the arr.

Example
Chunks.first([1, 2, 3]);
// 1

Chunks.last

Chunks.last(arr);

Returns the last item in the arr.

Example
Chunks.last([1, 2, 3]);
// 3

Chunks.flatten

Chunks.flatten(arr[, base]);

Returns a new and transformed array by flattening the nested arrays in the arr. If the base is provided it'll get used starting point for the new array.

Examples
Chunks.flatten([[1, 2, 3], [4, 5, 6]]);
// [1, 2, 3, 4, 5, 6]
Chunks.flatten([[2, 3, 4], [5, 6, 7]], [1]);
// [1, 2, 3, 4, 5, 6, 7]

Objects

Chunks.keys

Chunks.keys(obj);

Returns a new array with keys from the obj.

Example
Chunks.keys({one: 1, two: 2, three: 3});
// ['one', 'two', 'three']

Chunks.values

Chunks.values(obj);

Returns a new array with values from the obj.

Example
Chunks.values({one: 1, two: 2, three: 3});
// [1, 2, 3]

Chunks.has

Chunks.has(obj, key);

Determine if the obj has the key. It returns either true or false.

Example
Chunks.has({one: 1, two: 2, three: 3}, 'two');
// true

Chunks.extend

Chunks.extend(obj, src);

Merge the properties in the obj together with the properties in the src.

Example
Chunks.extend({one: 1, two: 3}, {two: 2, three: 3});
// {one: 1, two: 2, three: 3}