Cellophane.js
A lightweight wrapper around the array.
API
Constructor
Methods
.any(fn)
.any(op, val)
.any(key, op, val)
.append(obj)
.compact()
.contains(obj [, opts])
.each(fn)
.every(fn)
.every(op, val)
.every(key, op, val)
.filter(fn)
.filter(op, val)
.filter(key, op, val)
.first([n])
.fold(fn, acc)
.foldRight(fn, acc)
.get(i)
.indexOf(obj [, opts])
.insert(obj, i)
.last([n])
.limit(n)
.map(fn)
.max([key])
.min([key])
.prepend(obj)
.remove(obj [, opts])
.removeAt(i)
.reverse()
.size()
.slice(i [, j])
.sort(comp)
.sort([opts])
.sortBy(key [, opts])
.unique([opts])
.unwrap()
--
cellophane([array])
Constructs a new Cellophane object. If array
was specified, sets the Cellophane object’s “internal” array to array
. Throws if array
is not an array.
var a = cellophane();
var b = cellophane([]);
var c = cellophane([1, 2, 3]);
↩
.any(fn)
Returns true
if the fn
iterator is truthy for any object in the array.
var c = cellophane([1, 2, 3]);
c.any(function(val, i, array) {
return val > 1;
});
c.any(function(val, i, array) {
return val > 3;
});
↩
.any(op, val)
Returns true
if the comparison with val
using the op
operator returns true
for any object in the array.
var c = cellophane([1, 2, 3]);
c.any('>', 1);
c.any('>', 3);
↩
.any(key, op, val)
Returns true
if the comparison with val
using the op
operator returns true
for the value at key
of any object in the array. key
can be a dot-delimited path.
var c = cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]);
c.any('foo', '>', 1);
c.any('foo', '>', 3);
↩
.append(obj)
Adds obj
to the end of the array, returning the result as a new Cellophane object.
Aliases: add
, push
cellophane([1, 2]).append(3);
↩
.compact()
Returns a new Cellophane object with falsy values removed.
cellophane([0, 1, 2, 3]).compact();
↩
.contains(obj [, opts])
Returns true
if the array contains obj
, with objects compared by value. For strict comparison, set opts.strict
to true
.
var c = cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]);
c.contains({ foo: 3 });
c.contains({ foo: 3 }, { strict: true });
↩
.each(fn)
Calls the fn
iterator on each object in the array, and returns the original Cellophane object. Return false
in the fn
iterator to break from the loop.
Alias: forEach
cellophane([1, 2, 3]).each(function(val, i, array) {
console.log(val, i, array);
if (val === 2) {
return false;
}
});
↩
.every(fn)
Returns true
if the fn
iterator is truthy for every object in the array.
var c = cellophane([1, 2, 3]);
c.every(function(val, i, array) {
return val < 4;
});
c.every(function(val, i, array) {
return val < 3;
});
↩
.every(op, val)
Returns true
if the comparison with val
using the op
operator returns true
for every object in the array.
var c = cellophane([1, 2, 3]);
c.every('<', 4);
c.every('<', 3);
↩
.every(key, op, val)
Returns true
if the comparison with val
using the op
operator returns true
for the value at key
of every object in the array. key
can be a dot-delimited path.
var c = cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]);
c.every('foo', '<', 4);
c.every('foo', '<', 3);
↩
.filter(fn)
Filters the array using the fn
iterator, returning the filtered result as a new Cellophane object.
cellophane([1, 2, 3]).filter(function(val, i, array) {
return val > 1;
});
↩
.filter(op, val)
Filters the array. Objects that return true
when compared with val
using the op
operator will be included in the filtered result. Returns the filtered result as a new Cellophane object.
cellophane([1, 2, 3]).filter('>', 1);
↩
.filter(key, op, val)
Filters the array. Objects whose value at key
returns true
when compared with val
using the op
operator will be included in the filtered result. Returns the filtered result as a new Cellophane object. key
can be a dot-delimited path.
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]).filter('foo', '>', 1);
↩
.first([n])
Returns the first object in the array. If n
is specified, returns the first n
objects of the array as a new Cellophane object.
var c = cellophane([1, 2, 3]);
c.first();
c.first(2);
↩
.fold(fn, acc)
Accumulates the objects in the array to a single value, with the accumulator value initialised to acc
. Returns the final value of acc
. If acc
is an array, returns it wrapped in a new Cellophane object. The fn
iterator takes 4 arguments:
acc
— The current value of the accumulator.val
— The current value of the array being iterated over.i
— The current index.array
— The Cellophane object’s internal array.
Aliases: foldLeft
, foldl
, reduce
, reduceLeft
var c = cellophane([1, 2, 3]);
c.fold(function(acc, val, i, array) {
return acc + val;
}, 0);
c.fold(function(acc, val, i, array) {
acc.push(val * 10);
return acc;
}, []);
↩
.foldRight(fn, acc)
Just like fold
, but the array is iterated over from right to left.
Aliases: foldr
, reduceRight
↩
.get(i)
Returns the object at index i
of the array, or undefined
if i
is an invalid index. If i
is negative, indexing is from the end of the array (ie. index -1
refers to the last object in the array, and so on).
var c = cellophane([1, 2, 3]);
c.get(0);
c.get(1);
c.get(2);
c.get(3);
c.get(-1);
c.get(-2);
c.get(-3);
↩
.indexOf(obj [, opts])
Returns the index of obj
in the array, with objects compared by value. For strict comparison, set opts.strict
to true
. Returns -1
if obj
is not found in the array.
var c = cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]);
c.indexOf({ foo: 3 });
c.indexOf({ foo: 3 }, { strict: true });
↩
.insert(obj, i)
Inserts obj
at index i
of the array, returning the result as a new Cellophane object. If i
is negative, indexing is from the end of the array (ie. index -1
refers to the last object in the array, and so on). If i
is beyond the bounds of the original array, the resulting array is “expanded” accordingly.
var c = cellophane([1, 2, 3]);
c.insert('foo', 0);
c.insert('foo', 3);
c.insert('foo', 4);
c.insert('foo', -1);
c.insert('foo', -4);
c.insert('foo', -5);
↩
.last([n])
Returns the last object in the array. If n
is specified, returns the last n
objects of the array as a new Cellophane object.
var c = cellophane([1, 2, 3]);
c.last();
c.last(2);
↩
.limit(n)
If n
is positive, returns the first n
objects of the array as a new Cellophane object. Else returns the last |n|
objects of the array as a new Cellophane object.
var c = cellophane([1, 2, 3]);
c.limit(2);
c.limit(-2);
↩
.map(fn)
Maps the array over the fn
iterator, returning the result as a new Cellophane object.
cellophane([1, 2, 3]).map(function(val, i, array) {
return val * 10;
});
↩
.max([key])
Returns the largest object in the array. If key
was specified, compares the objects based on the value corresponding to key
. key
can be a dot-delimited path.
Aliases: maximum
, largest
cellophane([1, 2, 3]).max();
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]).max('foo');
↩
.min([key])
Returns the smallest object in the array. If key
was specified, compares the objects based on the value corresponding to key
. key
can be a dot-delimited path.
Aliases: minimum
, smallest
cellophane([1, 2, 3]).min();
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]).min('foo');
↩
.prepend(obj)
Adds obj
to the start of the array, returning the result as a new Cellophane object.
cellophane([2, 3]).prepend(1);
↩
.remove(obj [, opts])
Removes all objects that are equal to obj
from the array, with objects compared by value. For strict comparison, set opts.strict
to true
. Returns the result as a new Cellophane object.
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 },
{ foo: 1 }
]).remove({ foo: 1 });
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 },
{ foo: 1 }
]).remove({ foo: 1 }, { strict: true });
↩
.removeAt(i)
Removes the object at index i
of the array, returning the result as a new Cellophane object.
cellophane([1, 2, 3]).removeAt(1);
↩
.reverse()
Returns a new Cellophane object with the ordering of its objects reversed.
cellophane([1, 2, 3]).reverse();
↩
.size()
Returns the array size.
Alias: length
cellophane([]).size();
cellophane([1, 2, 3]).size();
↩
.slice(i [, j])
Takes a slice of the array and returns it as a new Cellophane object.
Alias: subarray
cellophane([1, 2, 3]).slice(1, 3);
↩
.sort(comp)
Sorts the array using the comp
comparator function, returning the sorted result as a new Cellophane object. comp
takes two arguments a
and b
, and must return:
- a negative value if
a
is to be ordered before b
, or - a positive value if
a
is to be ordered after b
, or 0
if the ordering of a
and b
(with respect to each other) should be left unchanged.
cellophane([3, 1, 2]).sort(function(a, b) {
return a < b ? -1 : 1;
});
↩
.sort([opts])
Sorts the array in ascending order, returning the sorted result as a new Cellophane object. Set opts.order
to 'desc'
to sort in descending order.
cellophane([3, 1, 2]).sort();
cellophane([3, 1, 2]).sort({ order: 'desc' });
↩
.sortBy(key [, opts])
Sorts the array of objects in ascending order based on the value corresponding to key
of each object, returning the sorted result as a new Cellophane object. key
can be a dot-delimited path. To sort in descending order, set opts.order
to 'desc'
.
var c = cellophane([
{ foo: 3 },
{ foo: 1 },
{ foo: 2 },
]);
c.sort('foo');
c.sort('foo', { order: 'desc' });
↩
.unique([opts])
Removes duplicates removed from the array, with objects compared by value. For strict comparison, set opts.strict
to true
. Returns the result as a new Cellophane object.
var c = cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 },
{ foo: 1 }
]);
c.unique();
c.unique({ strict: true });
↩
.unwrap()
Returns a copy of the Cellophane object’s “internal” array.
cellophane([1, 2, 3]).unwrap();
↩
Usage notes
Accessing the “internal” array
We can get the Cellophane object’s “internal” array on its .array
attribute:
var original = cellophane([{ foo: 1 }, { foo: 2 }, { foo: 3 }]);
console.log(original.array);
Methods always return a copy
A Cellophane object is designed to be immutable. So, for all methods (apart from each
) that returns a Cellophane object, it is actually a copy of the original Cellophane object that is returned:
var filtered = original.filter('foo', '>', 1);
console.log(filtered === original);
By copy, we mean that while filtered.array
and original.array
are different arrays, and refer to different objects in memory…
console.log(filtered.array === original.array);
…they actually contain references to the same objects:
console.log(filtered.array[0] === original.array[1]);
console.log(filtered.array[1] === original.array[2]);
Iterator function signature
For all methods that take an iterator fn
(called over each object in the array), note that the iterator is passed 3 arguments:
val
— The current object of the array being iterated over.i
— The index of the current object.array
— The Cellophane object’s internal array.
The exceptions are the fold
and foldr
methods; their fn
iterator takes an additional acc
accumulator argument.
Compare by value
In the contains
, indexOf
, and remove
methods, the default behavior is to compare objects by value. For example:
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]).contains({ foo: 3 });
See deep-equal.
To use strict comparison (where objects a
and b
are considered as equal if and only if a === b
), set opts.strict
to true
.
var opts = { strict: true };
cellophane([
{ foo: 1 },
{ foo: 2 },
{ foo: 3 }
]).contains({ foo: 3 }, opts);
Operators for comparing values
In the any
, every
, and filter
methods, the op
argument must be one of the following strings:
'=='
'==='
'!='
'!=='
'<'
'>'
'<='
'>='
See Versus.js.
Accessing nested properties
In the any
, every
, filter
, and sortBy
methods, access a nested object property by specifying a dot-delimited path as the key
. For example:
cellophane([
{ foo: { bar: 1 } },
{ foo: { bar: 2 } },
{ foo: { bar: 3 } }
]).any('foo.bar', '>', 0);
See Jaunt.js.
Installation
Install via npm:
$ npm i --save cellophane
Changelog
License
MIT license