Comparing version 0.0.0 to 0.1.0
212
index.js
@@ -6,6 +6,8 @@ 'use strict'; | ||
var versus = require('versus'); | ||
var isArray = require('cheque').isArray; | ||
var Cellophane = function(array) { | ||
if (!Array.isArray(array)) { | ||
throw new Error('expecting an array'); | ||
array = array || []; | ||
if (!isArray(array)) { | ||
throw new Error('need an array'); | ||
} | ||
@@ -18,2 +20,22 @@ if (!(this instanceof Cellophane)) { | ||
var indexOf = function(array, obj) { | ||
var i = array.length; | ||
while (i--) { | ||
if (deepEqual(array[i], obj, { strict: true })) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
var indexOfStrict = function(array, obj) { | ||
var i = array.length; | ||
while (i--) { | ||
if (array[i] === obj) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
Cellophane.prototype.any = function(a, b, c) { | ||
@@ -53,2 +75,16 @@ var result = false; | ||
Cellophane.prototype.append = | ||
Cellophane.prototype.add = | ||
Cellophane.prototype.push = function(obj) { | ||
var result = this.array.slice(); | ||
result.push(obj); | ||
return new Cellophane(result); | ||
}; | ||
Cellophane.prototype.compact = function() { | ||
return this.filter(function(val) { | ||
return val; | ||
}); | ||
}; | ||
Cellophane.prototype.contains = function(obj, opts) { | ||
@@ -58,3 +94,4 @@ return this.indexOf(obj, opts) !== -1; | ||
Cellophane.prototype.each = Cellophane.prototype.forEach = function(fn) { | ||
Cellophane.prototype.each = | ||
Cellophane.prototype.forEach = function(fn) { | ||
var array = this.array; | ||
@@ -136,9 +173,31 @@ var i = -1; | ||
Cellophane.prototype.fold = Cellophane.prototype.reduce = function(fn, acc) { | ||
Cellophane.prototype.first = function(n) { | ||
if (n == null) { | ||
return this.array[0]; | ||
} | ||
return new Cellophane(this.array.slice(0, n)); | ||
}; | ||
Cellophane.prototype.fold = | ||
Cellophane.prototype.foldLeft = | ||
Cellophane.prototype.foldl = | ||
Cellophane.prototype.reduce = | ||
Cellophane.prototype.reduceLeft = function(fn, acc) { | ||
this.each(function(val, i, array) { | ||
acc = fn(acc, array[i], i, array); | ||
}); | ||
return Array.isArray(acc) ? new Cellophane(acc) : acc; | ||
return isArray(acc) ? new Cellophane(acc) : acc; | ||
}; | ||
Cellophane.prototype.foldRight = | ||
Cellophane.prototype.foldr = | ||
Cellophane.prototype.reduceRight = function(fn, acc) { | ||
var array = this.array; | ||
var i = array.length; | ||
while (i--) { | ||
acc = fn(acc, array[i], i, array); | ||
} | ||
return isArray(acc) ? new Cellophane(acc) : acc; | ||
}; | ||
Cellophane.prototype.get = function(i) { | ||
@@ -153,21 +212,35 @@ var array = this.array; | ||
Cellophane.prototype.indexOf = function(obj, opts) { | ||
var result = -1; | ||
if (opts && opts.strict === false) { | ||
this.each(function(val, i) { | ||
if (deepEqual(val, obj)) { | ||
result = i; | ||
return false; | ||
} | ||
}); | ||
return (opts && opts.strict === true ? indexOfStrict : indexOf)(this.array, obj); | ||
}; | ||
Cellophane.prototype.insert = function(obj, i) { | ||
var array = this.array; | ||
var len = array.length; | ||
var result; | ||
if (i < 0) { | ||
if (i < (len * -1) - 1) { | ||
result = [].concat(obj, new Array((i + len + 1) * -1), array); | ||
} else { | ||
result = array.slice(); | ||
result.splice(i + len + 1, 0, obj); | ||
} | ||
} else { | ||
this.each(function(val, i) { | ||
if (val === obj) { | ||
result = i; | ||
return false; | ||
} | ||
}); | ||
if (i > len) { | ||
result = [].concat(array, new Array(i - len), obj); | ||
} else { | ||
result = array.slice(); | ||
result.splice(i, 0, obj); | ||
} | ||
} | ||
return result; | ||
return new Cellophane(result); | ||
}; | ||
Cellophane.prototype.last = function(n) { | ||
var len = this.array.length; | ||
if (n == null) { | ||
return this.array[len-1]; | ||
} | ||
return new Cellophane(this.array.slice(len - n)); | ||
}; | ||
Cellophane.prototype.limit = function(n) { | ||
@@ -185,3 +258,5 @@ return new Cellophane(n < 0 ? this.array.slice(n) : this.array.slice(0, n)); | ||
Cellophane.prototype.max = Cellophane.prototype.maximum = function(key) { | ||
Cellophane.prototype.max = | ||
Cellophane.prototype.maximum = | ||
Cellophane.prototype.largest = function(key) { | ||
var array = this.array; | ||
@@ -215,3 +290,5 @@ var i = 0; | ||
Cellophane.prototype.min = Cellophane.prototype.minimum = function(key) { | ||
Cellophane.prototype.min = | ||
Cellophane.prototype.minimum = | ||
Cellophane.prototype.smallest = function(key) { | ||
var array = this.array; | ||
@@ -245,2 +322,20 @@ var i = 0; | ||
Cellophane.prototype.prepend = function(obj) { | ||
var result = this.array.slice(); | ||
result.unshift(obj); | ||
return new Cellophane(result); | ||
}; | ||
Cellophane.prototype.remove = function(obj, opts) { | ||
return this.filter(opts && opts.strict ? '!==' : '!=', obj); | ||
}; | ||
Cellophane.prototype.removeAt = function(i) { | ||
var result = this.array.slice(); | ||
if (i >= this.array.length * -1) { | ||
result.splice(i, 1); | ||
} | ||
return new Cellophane(result); | ||
}; | ||
Cellophane.prototype.reverse = function() { | ||
@@ -256,31 +351,56 @@ var array = this.array; | ||
Cellophane.prototype.size = Cellophane.prototype.length = function() { | ||
Cellophane.prototype.size = | ||
Cellophane.prototype.length = function() { | ||
return this.array.length; | ||
}; | ||
Cellophane.prototype.slice = Cellophane.prototype.subarray = function(i, j) { | ||
Cellophane.prototype.slice = | ||
Cellophane.prototype.subarray = function(i, j) { | ||
return new Cellophane(this.array.slice(i, j)); | ||
}; | ||
Cellophane.prototype.sort = function(a, b) { | ||
var result = this.array.slice(0); | ||
if (typeof a === 'function') { | ||
// sort(fn) | ||
result.sort(a); | ||
} else { | ||
// sort(key, order) | ||
result.sort(function(x, y) { | ||
x = get(x, a); | ||
y = get(y, a); | ||
if (b === 'desc') { | ||
var temp = x; | ||
x = y; | ||
y = temp; | ||
} | ||
if (typeof x === 'string' && typeof y === 'string') { | ||
return x.localeCompare(y); | ||
} | ||
return x > y ? 1 : -1; | ||
}); | ||
} | ||
Cellophane.prototype.sort = (function() { | ||
var ascFn = function(a, b) { | ||
return a < b ? -1 : 1; | ||
}; | ||
var descFn = function(a, b) { | ||
return a > b ? -1 : 1; | ||
}; | ||
return function(a) { | ||
var result = this.array.slice(); | ||
if (typeof a === 'function') { | ||
// sort(fn) | ||
result.sort(a); | ||
} else { | ||
// sort(opts) | ||
result.sort(a && a.order === 'desc' ? descFn : ascFn); | ||
} | ||
return new Cellophane(result); | ||
}; | ||
})(); | ||
Cellophane.prototype.sortBy = function(key, opts) { | ||
return new Cellophane(this.array.slice().sort(function(a, b) { | ||
a = get(a, key); | ||
b = get(b, key); | ||
if (opts && opts.order === 'desc') { | ||
var temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
if (typeof a === 'string' && typeof b === 'string') { | ||
return a.localeCompare(b); | ||
} | ||
return a < b ? -1 : 1; | ||
})); | ||
}; | ||
Cellophane.prototype.unique = function(opts) { | ||
var result = []; | ||
var fn = opts && opts.strict ? indexOfStrict : indexOf; | ||
this.each(function(val) { | ||
if (fn(result, val) === -1) { | ||
result.push(val); | ||
} | ||
}); | ||
return new Cellophane(result); | ||
@@ -290,5 +410,5 @@ }; | ||
Cellophane.prototype.unwrap = function() { | ||
return this.array.slice(0); | ||
return this.array.slice(); | ||
}; | ||
module.exports = Cellophane; |
{ | ||
"name": "cellophane", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "A lightweight wrapper around the array.", | ||
@@ -12,2 +12,3 @@ "author": "Lim Yuan Qing", | ||
"dependencies": { | ||
"cheque": "^0.2.1", | ||
"deep-equal": "^0.2.1", | ||
@@ -14,0 +15,0 @@ "jaunt": "^1.3.0", |
714
README.md
@@ -5,5 +5,712 @@ # Cellophane.js [![npm Version](http://img.shields.io/npm/v/cellophane.svg?style=flat)](https://www.npmjs.org/package/cellophane) [![Build Status](https://img.shields.io/travis/yuanqing/cellophane.svg?style=flat)](https://travis-ci.org/yuanqing/cellophane) [![Coverage Status](https://img.shields.io/coveralls/yuanqing/cellophane.svg?style=flat)](https://coveralls.io/r/yuanqing/cellophane) | ||
## API | ||
### Constructor | ||
- [`cellophane([array])`](#cellophanearray) | ||
### Methods | ||
- [`.any(fn)`](#anyfn) | ||
- [`.any(op, val)`](#anyop-val) | ||
- [`.any(key, op, val)`](#anykey-op-val) | ||
- [`.append(obj)`](#appendobj) | ||
- [`.compact()`](#compact) | ||
- [`.contains(obj [, opts])`](#containsobj--opts) | ||
- [`.each(fn)`](#eachfn) | ||
- [`.every(fn)`](#everyfn) | ||
- [`.every(op, val)`](#everyop-val) | ||
- [`.every(key, op, val)`](#everykey-op-val) | ||
- [`.filter(fn)`](#filterfn) | ||
- [`.filter(op, val)`](#filterop-val) | ||
- [`.filter(key, op, val)`](#filterkey-op-val) | ||
- [`.first([n])`](#firstn) | ||
- [`.fold(fn, acc)`](#foldfn-acc) | ||
- [`.foldRight(fn, acc)`](#foldrightfn-acc) | ||
- [`.get(i)`](#geti) | ||
- [`.indexOf(obj [, opts])`](#indexofobj--opts) | ||
- [`.insert(obj, i)`](#insertobj-i) | ||
- [`.last([n])`](#lastn) | ||
- [`.limit(n)`](#limitn) | ||
- [`.map(fn)`](#mapfn) | ||
- [`.max([key])`](#maxkey) | ||
- [`.min([key])`](#minkey) | ||
- [`.prepend(obj)`](#prependobj) | ||
- [`.remove(obj [, opts])`](#removeobj--opts) | ||
- [`.removeAt(i)`](#removeati) | ||
- [`.reverse()`](#reverse) | ||
- [`.size()`](#size) | ||
- [`.slice(i [, j])`](#slicei--j) | ||
- [`.sort(comp)`](#sortcomp) | ||
- [`.sort([opts])`](#sortopts) | ||
- [`.sortBy(key [, opts])`](#sortbykey--opts) | ||
- [`.unique([opts])`](#uniqueopts) | ||
- [`.unwrap()`](#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. | ||
```js | ||
var a = cellophane(); | ||
var b = cellophane([]); | ||
var c = cellophane([1, 2, 3]); | ||
``` | ||
<sup>[↩](#constructor)</sup> | ||
### .any(fn) | ||
Returns `true` if the [`fn` iterator](#iterator-function-signature) is truthy for any object in the array. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.any(function(val, i, array) { | ||
return val > 1; | ||
}); //=> true | ||
c.any(function(val, i, array) { | ||
return val > 3; | ||
}); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .any(op, val) | ||
Returns `true` if the comparison with `val` using the [`op` operator](#operators-for-comparing-values) returns `true` for any object in the array. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.any('>', 1); //=> true | ||
c.any('>', 3); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .any(key, op, val) | ||
Returns `true` if the comparison with `val` using the [`op` operator](#operators-for-comparing-values) returns `true` for the value at `key` of any object in the array. `key` can be a [dot-delimited path](#accessing-nested-properties). | ||
```js | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
c.any('foo', '>', 1); //=> true | ||
c.any('foo', '>', 3); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .append(obj) | ||
Adds `obj` to the end of the array, returning the result as a new Cellophane object. | ||
*Aliases:* `add`, `push` | ||
```js | ||
cellophane([1, 2]).append(3); //=> cellophane([1, 2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .compact() | ||
Returns a new Cellophane object with falsy values removed. | ||
```js | ||
cellophane([0, 1, 2, 3]).compact(); //=> cellophane([1, 2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .contains(obj [, opts]) | ||
Returns `true` if the array contains `obj`, with objects [compared by value](#compare-by-value). For strict comparison, set `opts.strict` to `true`. | ||
```js | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
c.contains({ foo: 3 }); //=> true | ||
c.contains({ foo: 3 }, { strict: true }); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .each(fn) | ||
Calls the [`fn` iterator](#iterator-function-signature) 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` | ||
```js | ||
cellophane([1, 2, 3]).each(function(val, i, array) { | ||
console.log(val, i, array); | ||
//=> 1, 0, [1, 2, 3] | ||
//=> 2, 1, [1, 2, 3] | ||
if (val === 2) { | ||
return false; | ||
} | ||
}); | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .every(fn) | ||
Returns `true` if the [`fn` iterator](#iterator-function-signature) is truthy for every object in the array. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.every(function(val, i, array) { | ||
return val < 4; | ||
}); //=> true | ||
c.every(function(val, i, array) { | ||
return val < 3; | ||
}); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .every(op, val) | ||
Returns `true` if the comparison with `val` using the [`op` operator](#operators-for-comparing-values) returns `true` for every object in the array. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.every('<', 4); //=> true | ||
c.every('<', 3); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .every(key, op, val) | ||
Returns `true` if the comparison with `val` using the [`op` operator](#operators-for-comparing-values) returns `true` for the value at `key` of every object in the array. `key` can be a [dot-delimited path](#accessing-nested-properties). | ||
```js | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
c.every('foo', '<', 4); //=> true | ||
c.every('foo', '<', 3); //=> false | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .filter(fn) | ||
Filters the array using the [`fn` iterator](#iterator-function-signature), returning the filtered result as a new Cellophane object. | ||
```js | ||
cellophane([1, 2, 3]).filter(function(val, i, array) { | ||
return val > 1; | ||
}); //=> cellophane([2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .filter(op, val) | ||
Filters the array. Objects that return `true` when compared with `val` using the [`op` operator](#operators-for-comparing-values) will be included in the filtered result. Returns the filtered result as a new Cellophane object. | ||
```js | ||
cellophane([1, 2, 3]).filter('>', 1); //=> cellophane([2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .filter(key, op, val) | ||
Filters the array. Objects whose value at `key` returns `true` when compared with `val` using the [`op` operator](#operators-for-comparing-values) will be included in the filtered result. Returns the filtered result as a new Cellophane object. `key` can be a [dot-delimited path](#accessing-nested-properties). | ||
```js | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]).filter('foo', '>', 1); | ||
//=> cellophane([ | ||
// { foo: 2 }, | ||
// { foo: 3 } | ||
// ]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.first(); //=> 1 | ||
c.first(2); //=> cellophane([1, 2]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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` | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.fold(function(acc, val, i, array) { | ||
return acc + val; | ||
}, 0); //=> 6 | ||
c.fold(function(acc, val, i, array) { | ||
acc.push(val * 10); | ||
return acc; | ||
}, []); //=> cellophane([10, 20, 30]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .foldRight(fn, acc) | ||
Just like [`fold`](#foldfn-acc), but the array is iterated over from right to left. | ||
*Aliases:* `foldr`, `reduceRight` | ||
<sup>[↩](#methods)</sup> | ||
### .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). | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.get(0); //=> 1 | ||
c.get(1); //=> 2 | ||
c.get(2); //=> 3 | ||
c.get(3); //=> undefined | ||
c.get(-1); //=> 3 | ||
c.get(-2); //=> 2 | ||
c.get(-3); //=> 1 | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .indexOf(obj [, opts]) | ||
Returns the index of `obj` in the array, with objects [compared by value](#compare-by-value). For strict comparison, set `opts.strict` to `true`. Returns `-1` if `obj` is not found in the array. | ||
```js | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
c.indexOf({ foo: 3 }); //=> 2 | ||
c.indexOf({ foo: 3 }, { strict: true }); //=> -1 | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.insert('foo', 0); //=> cellophane(['foo', 1, 2, 3]) | ||
c.insert('foo', 3); //=> cellophane([1, 2, 3, 'foo']) | ||
c.insert('foo', 4); //=> cellophane([1, 2, 3, undefined, 'foo']) | ||
c.insert('foo', -1); //=> cellophane([1, 2, 3, 'foo']) | ||
c.insert('foo', -4); //=> cellophane(['foo', 1, 2, 3]) | ||
c.insert('foo', -5); //=> cellophane(['foo', undefined, 1, 2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.last(); //=> 3 | ||
c.last(2); //=> cellophane([2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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. | ||
```js | ||
var c = cellophane([1, 2, 3]); | ||
c.limit(2); //=> cellophane([1, 2]) | ||
c.limit(-2); //=> cellophane([2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .map(fn) | ||
Maps the array over the [`fn` iterator](#iterator-function-signature), returning the result as a new Cellophane object. | ||
```js | ||
cellophane([1, 2, 3]).map(function(val, i, array) { | ||
return val * 10; | ||
}); //=> cellophane([10, 20, 30]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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](#accessing-nested-properties). | ||
*Aliases:* `maximum`, `largest` | ||
```js | ||
cellophane([1, 2, 3]).max(); //=> 3 | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]).max('foo'); //=> { foo: 3 } | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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](#accessing-nested-properties). | ||
*Aliases:* `minimum`, `smallest` | ||
```js | ||
cellophane([1, 2, 3]).min(); //=> 1 | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]).min('foo'); //=> { foo: 1 } | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .prepend(obj) | ||
Adds `obj` to the start of the array, returning the result as a new Cellophane object. | ||
```js | ||
cellophane([2, 3]).prepend(1); //=> cellophane([1, 2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .remove(obj [, opts]) | ||
Removes all objects that are equal to `obj` from the array, with objects [compared by value](https://github.com/substack/node-deep-equal). For strict comparison, set `opts.strict` to `true`. Returns the result as a new Cellophane object. | ||
```js | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 }, | ||
{ foo: 1 } | ||
]).remove({ foo: 1 }); | ||
//=> cellophane([ | ||
// { foo: 2 }, | ||
// { foo: 3 } | ||
// ]) | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 }, | ||
{ foo: 1 } | ||
]).remove({ foo: 1 }, { strict: true }); | ||
//=> cellophane([ | ||
// { foo: 1 }, | ||
// { foo: 2 }, | ||
// { foo: 3 }, | ||
// { foo: 1 } | ||
// ]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .removeAt(i) | ||
Removes the object at index `i` of the array, returning the result as a new Cellophane object. | ||
```js | ||
cellophane([1, 2, 3]).removeAt(1); //=> cellophane([1, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .reverse() | ||
Returns a new Cellophane object with the ordering of its objects reversed. | ||
```js | ||
cellophane([1, 2, 3]).reverse(); //=> cellophane([3, 2, 1]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .size() | ||
Returns the array size. | ||
*Alias:* `length` | ||
```js | ||
cellophane([]).size(); //=> 0 | ||
cellophane([1, 2, 3]).size(); //=> 3 | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .slice(i [, j]) | ||
Takes a [slice](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) of the array and returns it as a new Cellophane object. | ||
*Alias:* `subarray` | ||
```js | ||
cellophane([1, 2, 3]).slice(1, 3); //=> cellophane([2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .sort(comp) | ||
[Sorts](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) 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. | ||
```js | ||
cellophane([3, 1, 2]).sort(function(a, b) { | ||
return a < b ? -1 : 1; | ||
}); //=> cellophane([1, 2, 3]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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. | ||
```js | ||
cellophane([3, 1, 2]).sort(); //=> cellophane([1, 2, 3]) | ||
cellophane([3, 1, 2]).sort({ order: 'desc' }); //=> cellophane([3, 2, 1]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .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](#accessing-nested-properties). To sort in descending order, set `opts.order` to `'desc'`. | ||
```js | ||
var c = cellophane([ | ||
{ foo: 3 }, | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
]); | ||
c.sort('foo'); | ||
//=> cellophane([ | ||
// { foo: 1 }, | ||
// { foo: 2 }, | ||
// { foo: 3 } | ||
// ]) | ||
c.sort('foo', { order: 'desc' }); | ||
//=> cellophane([ | ||
// { foo: 3 }, | ||
// { foo: 2 }, | ||
// { foo: 1 } | ||
// ]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .unique([opts]) | ||
Removes duplicates removed from the array, with objects [compared by value](https://github.com/substack/node-deep-equal). For strict comparison, set `opts.strict` to `true`. Returns the result as a new Cellophane object. | ||
```js | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 }, | ||
{ foo: 1 } | ||
]); | ||
c.unique(); | ||
//=> cellophane([ | ||
// { foo: 1 }, | ||
// { foo: 2 }, | ||
// { foo: 3 } | ||
// ]) | ||
c.unique({ strict: true }); | ||
//=> cellophane([ | ||
// { foo: 1 }, | ||
// { foo: 2 }, | ||
// { foo: 3 }, | ||
// { foo: 1 } | ||
// ]) | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
### .unwrap() | ||
Returns a *copy* of the Cellophane object’s “internal” array. | ||
```js | ||
cellophane([1, 2, 3]).unwrap(); //=> [1, 2, 3] | ||
``` | ||
<sup>[↩](#methods)</sup> | ||
## Usage notes | ||
### Accessing the “internal” array | ||
We can get the Cellophane object’s “internal” array on its `.array` attribute: | ||
```js | ||
var original = cellophane([{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
console.log(original.array); //=> [{ foo: 1 }, { foo: 2 }, { foo: 3 }] | ||
``` | ||
### Methods always return a *copy* | ||
A Cellophane object is designed to be *immutable*. So, for all methods (apart from [`each`](#eachfn)) that returns a Cellophane object, it is actually a *copy* of the original Cellophane object that is returned: | ||
```js | ||
var filtered = original.filter('foo', '>', 1); | ||
console.log(filtered === original); //=> false | ||
``` | ||
By *copy*, we mean that while `filtered.array` and `original.array` are different arrays, and refer to *different* objects in memory… | ||
```js | ||
console.log(filtered.array === original.array); //=> false | ||
``` | ||
…they actually contain references to the *same* objects: | ||
```js | ||
console.log(filtered.array[0] === original.array[1]); //=> true | ||
console.log(filtered.array[1] === original.array[2]); //=> true | ||
``` | ||
### 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`](#foldfn-acc) and [`foldr`](#foldrfn-acc) methods; their `fn` iterator takes an additional `acc` accumulator argument. | ||
### Compare by value | ||
In the [`contains`](#containsobj--opts), [`indexOf`](#indexofobj--opts), and [`remove`](#removeobj--opts) methods, the default behavior is to compare objects by value. For example: | ||
```js | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]).contains({ foo: 3 }); //=> true | ||
``` | ||
See [deep-equal](https://github.com/substack/node-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`. | ||
```js | ||
var opts = { strict: true }; | ||
cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]).contains({ foo: 3 }, opts); //=> false | ||
``` | ||
### Operators for comparing values | ||
In the [`any`](#anyop-val), [`every`](#everyop-val), and [`filter`](#filterop-val) methods, the `op` argument must be one of the following strings: | ||
- `'=='` | ||
- `'==='` | ||
- `'!='` | ||
- `'!=='` | ||
- `'<'` | ||
- `'>'` | ||
- `'<='` | ||
- `'>='` | ||
See [Versus.js](https://github.com/yuanqing/versus#usage). | ||
### Accessing nested properties | ||
In the [`any`](#anykey-op-val), [`every`](#everykey-op-val), [`filter`](#filterkey-op-val), and [`sortBy`](#sortbykey--opts) methods, access a nested object property by specifying a dot-delimited path as the `key`. For example: | ||
```js | ||
cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]).any('foo.bar', '>', 0); //=> true | ||
``` | ||
See [Jaunt.js](https://github.com/yuanqing/jaunt#jauntgetobj-path). | ||
## Installation | ||
Install via [npm](https://www.npmjs.org/): | ||
Install via [npm](https://npmjs.com/): | ||
@@ -14,4 +721,9 @@ ```bash | ||
## Changelog | ||
- 0.1.0 | ||
- Initial release | ||
## License | ||
[MIT license](https://github.com/yuanqing/cellophane/blob/master/LICENSE) |
@@ -11,5 +11,3 @@ 'use strict'; | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.any(function(val, i, array) { | ||
var result = cellophane().any(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -20,4 +18,2 @@ return val > 1; | ||
t.equal(result, false); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.end(); | ||
@@ -28,5 +24,3 @@ }); | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.any(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).any(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -41,4 +35,2 @@ return val > 3; | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.end(); | ||
@@ -49,5 +41,3 @@ }); | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.any(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).any(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -61,9 +51,5 @@ return val > 1; | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -74,8 +60,3 @@ | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.any('>', 1); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().any('>', 1), false); | ||
t.end(); | ||
@@ -85,8 +66,3 @@ }); | ||
t.test('false result', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.any('>', 3); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.equal(cellophane([1, 2, 3]).any('>', 3), false); | ||
t.end(); | ||
@@ -96,13 +72,6 @@ }); | ||
t.test('true result', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.any('>', 1); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.equal(cellophane([1, 2, 3]).any('>', 1), true); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -113,8 +82,3 @@ | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.any('foo.bar', '>', 1); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().any('foo.bar', '>', 1), false); | ||
t.end(); | ||
@@ -124,16 +88,7 @@ }); | ||
t.test('false result', function(t) { | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.any('foo.bar', '>', 3); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).any('foo.bar', '>', 3), false); | ||
t.end(); | ||
@@ -143,21 +98,10 @@ }); | ||
t.test('true result', function(t) { | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.any('foo.bar', '>', 1); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).any('foo.bar', '>', 1), true); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -6,19 +6,11 @@ 'use strict'; | ||
test('new cellophane(array)', function(t) { | ||
test('cellophane()', function(t) { | ||
t.test('creates a cellophane object', function(t) { | ||
var c = new cellophane([]); | ||
var c = cellophane(); | ||
t.true(c instanceof cellophane); | ||
t.looseEqual(c.array, []); | ||
t.end(); | ||
}); | ||
t.test('throws if not passed an array', function(t) { | ||
t.throws(function() { | ||
new cellophane({}); | ||
}); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -28,9 +20,11 @@ | ||
t.test('creates a cellophane object', function(t) { | ||
var c = cellophane([]); | ||
t.test('sets the "internal" array to `array`', function(t) { | ||
var array = [{ foo: 1 }]; | ||
var c = cellophane(array); | ||
t.true(c instanceof cellophane); | ||
t.equal(c.array, array); | ||
t.end(); | ||
}); | ||
t.test('throws if not passed an array', function(t) { | ||
t.test('throws if `array` is not an array', function(t) { | ||
t.throws(function() { | ||
@@ -42,4 +36,2 @@ cellophane({}); | ||
t.end(); | ||
}); |
@@ -9,37 +9,30 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
t.equal(original.contains({ foo: 1 }), false); | ||
t.equal(original.contains({ foo: 1 }, { strict: false }), false); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().contains({ foo: 3 }), false); | ||
t.end(); | ||
}); | ||
t.test('defaults to strict comparison', function(t) { | ||
var obj = { foo: 2 }; | ||
var array = [{ foo: 1 }, obj, { foo: 3 }]; | ||
var original = cellophane(array); | ||
t.equal(original.contains('bar'), false); | ||
t.equal(original.contains({ foo: 2 }), false); | ||
t.equal(original.contains(obj), true); | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array, array); | ||
t.test('defaults to comparing by object value', function(t) { | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
t.equal(c.contains({ foo: 3 }), true); | ||
t.equal(c.contains({ foo: 4 }), false); | ||
t.end(); | ||
}); | ||
t.test('coercive comparison if `opts.strict` is `false`', function(t) { | ||
var obj = { foo: 2 }; | ||
var array = [{ foo: 1 }, obj, { foo: 3 }]; | ||
var original = cellophane(array); | ||
t.equal(original.contains('bar', { strict: false }), false); | ||
t.equal(original.contains({ foo: 2 }, { strict: false }), true); | ||
t.equal(original.contains(obj, { strict: false }), true); | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array, array); | ||
t.test('use strict comparison by setting `opts.strict` to `true`', function(t) { | ||
var obj = { foo: 3 }; | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
obj | ||
]); | ||
t.equal(c.contains(obj, { strict: true }), true); | ||
t.equal(c.contains({ foo: 3 }, { strict: true }), false); | ||
t.equal(c.contains({ foo: 4 }, { strict: true }), false); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -9,12 +9,14 @@ 'use strict'; | ||
t.test('returns the original object', function(t) { | ||
var original = cellophane([1, 2, 3]); | ||
t.equal(original, original.each(function() {})); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.each(function(val, i, array) { | ||
cellophane().each(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
}); | ||
t.looseEqual(args, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original, result); | ||
t.end(); | ||
@@ -25,4 +27,3 @@ }); | ||
var args = []; | ||
var original = cellophane([1, 2, 3]); | ||
var result = original.each(function(val, i, array) { | ||
cellophane([1, 2, 3]).each(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -35,11 +36,8 @@ }); | ||
]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original, result); | ||
t.end(); | ||
}); | ||
t.test('breaks on returning `false`', function(t) { | ||
t.test('breaks from the loop if `fn` returns `false`', function(t) { | ||
var args = []; | ||
var original = cellophane([1, 2, 3]); | ||
var result = original.each(function(val, i, array) { | ||
cellophane([1, 2, 3]).each(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -54,9 +52,5 @@ if (val === 2) { | ||
]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original, result); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -11,12 +11,8 @@ 'use strict'; | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.every(function(val, i, array) { | ||
var result = cellophane().every(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
return val < 3; | ||
return val < 4; | ||
}); | ||
t.looseEqual(args, []); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.end(); | ||
@@ -27,5 +23,3 @@ }); | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.every(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).every(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -40,4 +34,2 @@ return val < 3; | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.end(); | ||
@@ -48,7 +40,5 @@ }); | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.every(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).every(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
return val > 0; | ||
return val < 4; | ||
}); | ||
@@ -61,9 +51,5 @@ t.looseEqual(args, [ | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -74,8 +60,3 @@ | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.every('<', 3); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().every('<', 4), true); | ||
t.end(); | ||
@@ -85,8 +66,3 @@ }); | ||
t.test('false result', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.every('<', 3); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.equal(cellophane([1, 2, 3]).every('<', 3), false); | ||
t.end(); | ||
@@ -96,13 +72,6 @@ }); | ||
t.test('true result', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.every('>', 0); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.equal(cellophane([1, 2, 3]).every('<', 4), true); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -113,8 +82,3 @@ | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.every('foo.bar', '<', 3); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().every('foo.bar', '<', 4), true); | ||
t.end(); | ||
@@ -124,16 +88,7 @@ }); | ||
t.test('false result', function(t) { | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.every('foo.bar', '<', 3); | ||
t.equal(result, false); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).every('foo.bar', '<', 3), false); | ||
t.end(); | ||
@@ -143,21 +98,10 @@ }); | ||
t.test('true result', function(t) { | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.every('foo.bar', '>', 0); | ||
t.equal(result, true); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).every('foo.bar', '<', 4), true); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -9,24 +9,40 @@ 'use strict'; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.filter(function(val) { | ||
return val.foo > 1; | ||
}); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 2 }, { foo: 3 }]); | ||
t.equal(result.array[0], y); | ||
t.equal(result.array[1], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.filter(function(val, i, array) { | ||
var result = cellophane().filter(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
return val > 1; | ||
return val > 0; | ||
}); | ||
t.looseEqual(args, []); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('filter using `fn`', function(t) { | ||
t.test('non-empty array', function(t) { | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.filter(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).filter(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -41,11 +57,5 @@ return val > 1; | ||
t.looseEqual(result.array, [2, 3]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -55,9 +65,10 @@ | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
t.test('returns a copy', function(t) { | ||
var original = cellophane([1, 2, 3]); | ||
var result = original.filter('>', 1); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
// original | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
// result | ||
t.looseEqual(result.array, [2, 3]); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -68,15 +79,11 @@ t.notEqual(original.array, result.array); | ||
t.test('filter on `val`', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.filter('>', 1); | ||
t.looseEqual(result.array, [2, 3]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().filter('>', 1).array, []); | ||
t.end(); | ||
}); | ||
t.end(); | ||
t.test('non-empty array', function(t) { | ||
t.looseEqual(cellophane([1, 2, 3]).filter('>', 1).array, [2, 3]); | ||
t.end(); | ||
}); | ||
@@ -87,9 +94,19 @@ }); | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.filter('foo.bar', '>', 1); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
var result = original.filter('foo', '>', 1); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 2 }, { foo: 3 }]); | ||
t.equal(result.array[0], y); | ||
t.equal(result.array[1], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -100,10 +117,13 @@ t.notEqual(original.array, result.array); | ||
t.test('filter on `val` corresponding to `key`', function(t) { | ||
var array = [ | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().filter('foo.bar', '>', 1).array, []); | ||
t.end(); | ||
}); | ||
t.test('non-empty array', function(t) { | ||
var result = cellophane([ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.filter('foo.bar', '>', 1); | ||
]).filter('foo.bar', '>', 1); | ||
t.looseEqual(result.array, [ | ||
@@ -113,15 +133,5 @@ { foo: { bar: 2 } }, | ||
]); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -13,5 +13,3 @@ 'use strict'; | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.fold(function(acc, val, i, array) { | ||
var result = cellophane().fold(function(acc, val, i, array) { | ||
args.push(clone([acc, val, i, array])); | ||
@@ -22,12 +20,8 @@ return acc + val; | ||
t.looseEqual(result, 0); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.end(); | ||
}); | ||
t.test('returns the accumulated value', function(t) { | ||
t.test('non-empty array', function(t) { | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.fold(function(acc, val, i, array) { | ||
var result = cellophane([1, 2, 3]).fold(function(acc, val, i, array) { | ||
args.push(clone([acc, val, i, array])); | ||
@@ -42,9 +36,5 @@ return acc + val; | ||
t.looseEqual(result, 6); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -54,33 +44,31 @@ | ||
t.test('returns `acc` as a Cellophane object', function(t) { | ||
var acc = []; | ||
var result = cellophane([1, 2, 3]).fold(function(acc) { | ||
return acc; | ||
}, acc); | ||
t.true(result instanceof cellophane); | ||
t.equal(result.array, acc); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var acc = []; | ||
var result = original.fold(function(acc, val, i, array) { | ||
var result = cellophane().fold(function(acc, val, i, array) { | ||
args.push(clone([acc, val, i, array])); | ||
acc.push(val * 10); | ||
return acc; | ||
}, acc); | ||
}, []); | ||
t.looseEqual(args, []); | ||
t.true(result instanceof cellophane); | ||
t.looseEqual(result.array, []); | ||
t.equal(result.array, acc); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('returns a cellophane object', function(t) { | ||
t.test('non-empty array', function(t) { | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var acc = []; | ||
var result = original.fold(function(acc, val, i, array) { | ||
var result = cellophane([1, 2, 3]).fold(function(acc, val, i, array) { | ||
args.push(clone([acc, val, i, array])); | ||
acc.push(val * 10); | ||
return acc; | ||
}, acc); | ||
}, []); | ||
t.looseEqual(args, [ | ||
@@ -91,18 +79,8 @@ [[], 1, 0, [1, 2, 3]], | ||
]); | ||
t.true(result instanceof cellophane); | ||
t.looseEqual(result.array, [10, 20, 30]); | ||
t.equal(result.array, acc); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -9,36 +9,27 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
t.equal(original.get(0), undefined); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
var c = cellophane(); | ||
t.looseEqual(c.get(-1), undefined); | ||
t.looseEqual(c.get(0), undefined); | ||
t.looseEqual(c.get(1), undefined); | ||
t.end(); | ||
}); | ||
t.test('get from start of array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
t.equal(original.get(0), 1); | ||
t.equal(original.get(1), 2); | ||
t.equal(original.get(2), 3); | ||
t.equal(original.get(3), undefined); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.test('positive `i`', function(t) { | ||
var c = cellophane([1, 2, 3]); | ||
t.equal(c.get(0), 1); | ||
t.equal(c.get(1), 2); | ||
t.equal(c.get(2), 3); | ||
t.equal(c.get(3), undefined); | ||
t.end(); | ||
}); | ||
t.test('get from end of array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
t.equal(original.get(-1), 3); | ||
t.equal(original.get(-2), 2); | ||
t.equal(original.get(-3), 1); | ||
t.equal(original.get(-4), undefined); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.test('negative `i`', function(t) { | ||
var c = cellophane([1, 2, 3]); | ||
t.equal(c.get(-1), 3); | ||
t.equal(c.get(-2), 2); | ||
t.equal(c.get(-3), 1); | ||
t.equal(c.get(-4), undefined); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -9,37 +9,30 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
t.equal(original.indexOf({ foo: 1 }), -1); | ||
t.equal(original.indexOf({ foo: 1 }, { strict: false }), -1); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.equal(cellophane().indexOf({ foo: 3 }), -1); | ||
t.end(); | ||
}); | ||
t.test('defaults to strict comparison', function(t) { | ||
var obj = { foo: 2 }; | ||
var array = [{ foo: 1 }, obj, { foo: 3 }]; | ||
var original = cellophane(array); | ||
t.equal(original.indexOf('bar'), -1); | ||
t.equal(original.indexOf({ foo: 2 }), -1); | ||
t.equal(original.indexOf(obj), 1); | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array, array); | ||
t.test('defaults to comparing by object value', function(t) { | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
{ foo: 3 } | ||
]); | ||
t.equal(c.indexOf({ foo: 3 }), 2); | ||
t.equal(c.indexOf({ foo: 4 }), -1); | ||
t.end(); | ||
}); | ||
t.test('coercive comparison if `opts.strict` is `false`', function(t) { | ||
var obj = { foo: 2 }; | ||
var array = [{ foo: 1 }, obj, { foo: 3 }]; | ||
var original = cellophane(array); | ||
t.equal(original.indexOf('bar', { strict: false }), -1); | ||
t.equal(original.indexOf({ foo: 2 }, { strict: false }), 1); | ||
t.equal(original.indexOf(obj, { strict: false }), 1); | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array, array); | ||
t.test('use strict comparison by setting `opts.strict` to `true`', function(t) { | ||
var obj = { foo: 3 }; | ||
var c = cellophane([ | ||
{ foo: 1 }, | ||
{ foo: 2 }, | ||
obj | ||
]); | ||
t.equal(c.indexOf(obj, { strict: true }), 2); | ||
t.equal(c.indexOf({ foo: 3 }, { strict: true }), -1); | ||
t.equal(c.indexOf({ foo: 4 }, { strict: true }), -1); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,9 +8,19 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.limit(5); | ||
t.looseEqual(original.array, []); | ||
t.looseEqual(result.array, []); | ||
t.equal(original.array, array); | ||
var result = original.limit(2); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 1 }, { foo: 2 }]); | ||
t.equal(result.array[0], x); | ||
t.equal(result.array[1], y); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -21,64 +31,18 @@ t.notEqual(original.array, result.array); | ||
t.test('limit from start of the array', function(t) { | ||
t.test('limit is smaller than the array size', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.limit(2); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.looseEqual(result.array, [1, 2]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('limit is larger than the array size', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.limit(10); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.looseEqual(result.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().limit(2).array, []); | ||
t.looseEqual(cellophane().limit(4).array, []); | ||
t.looseEqual(cellophane().limit(-2).array, []); | ||
t.looseEqual(cellophane().limit(-4).array, []); | ||
t.end(); | ||
}); | ||
t.test('limit from end of the array', function(t) { | ||
t.test('limit is smaller than the array size', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.limit(-2); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.looseEqual(result.array, [2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('limit is larger than the array size', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.limit(-10); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.looseEqual(result.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('non-empty array', function(t) { | ||
t.looseEqual(cellophane([1, 2, 3]).limit(2).array, [1, 2]); | ||
t.looseEqual(cellophane([1, 2, 3]).limit(4).array, [1, 2, 3]); | ||
t.looseEqual(cellophane([1, 2, 3]).limit(-2).array, [2, 3]); | ||
t.looseEqual(cellophane([1, 2, 3]).limit(-4).array, [1, 2, 3]); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -9,7 +9,30 @@ 'use strict'; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.map(function(val) { | ||
return val; | ||
}); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(result.array[0], x); | ||
t.equal(result.array[1], y); | ||
t.equal(result.array[2], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
var args = []; | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.map(function(val, i, array) { | ||
var result = cellophane().map(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -20,6 +43,2 @@ return val * 10; | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
@@ -30,5 +49,3 @@ }); | ||
var args = []; | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.map(function(val, i, array) { | ||
var result = cellophane([1, 2, 3]).map(function(val, i, array) { | ||
args.push(clone([val, i, array])); | ||
@@ -43,11 +60,5 @@ return val * 10; | ||
t.looseEqual(result.array, [10, 20, 30]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,24 +8,12 @@ 'use strict'; | ||
test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.max(); | ||
t.looseEqual(result, undefined); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().max(), undefined); | ||
t.end(); | ||
}); | ||
test('non-empty array', function(t) { | ||
var array = [2, 1, 3]; | ||
var original = cellophane(array); | ||
var result = original.max(); | ||
t.looseEqual(result, 3); | ||
t.looseEqual(original.array, [2, 1, 3]); | ||
t.equal(original.array, array); | ||
t.test('non-empty array', function(t) { | ||
t.looseEqual(cellophane([2, 1, 3]).max(), 3); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -35,33 +23,17 @@ | ||
test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.max('foo.bar'); | ||
t.looseEqual(result, undefined); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().max('foo.bar'), undefined); | ||
t.end(); | ||
}); | ||
test('non-empty array', function(t) { | ||
t.test('non-empty array', function(t) { | ||
var obj = { foo: { bar: 3 } }; | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 1 } }, | ||
obj | ||
]; | ||
var original = cellophane(array); | ||
var result = original.max('foo.bar'); | ||
t.looseEqual(result, obj); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).max('foo.bar'), obj); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,24 +8,12 @@ 'use strict'; | ||
test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.min(); | ||
t.looseEqual(result, undefined); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().min(), undefined); | ||
t.end(); | ||
}); | ||
test('non-empty array', function(t) { | ||
var array = [2, 1, 3]; | ||
var original = cellophane(array); | ||
var result = original.min(); | ||
t.looseEqual(result, 1); | ||
t.looseEqual(original.array, [2, 1, 3]); | ||
t.equal(original.array, array); | ||
t.test('non-empty array', function(t) { | ||
t.looseEqual(cellophane([2, 1, 3]).min(), 1); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
@@ -35,33 +23,17 @@ | ||
test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.min('foo.bar'); | ||
t.looseEqual(result, undefined); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().min('foo.bar'), undefined); | ||
t.end(); | ||
}); | ||
test('non-empty array', function(t) { | ||
t.test('non-empty array', function(t) { | ||
var obj = { foo: { bar: 1 } }; | ||
var array = [ | ||
t.equal(cellophane([ | ||
{ foo: { bar: 2 } }, | ||
obj, | ||
{ foo: { bar: 3 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.min('foo.bar'); | ||
t.looseEqual(result, obj); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.equal(original.array, array); | ||
]).min('foo.bar'), obj); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,9 +8,20 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.reverse(); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 3 }, { foo: 2 }, { foo: 1 }]); | ||
t.equal(result.array[0], z); | ||
t.equal(result.array[1], y); | ||
t.equal(result.array[2], x); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -21,11 +32,10 @@ t.notEqual(original.array, result.array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().reverse().array, []); | ||
t.end(); | ||
}); | ||
t.test('non-empty array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.reverse(); | ||
t.looseEqual(result.array, [3, 2, 1]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.looseEqual(cellophane([1]).reverse().array, [1]); | ||
t.looseEqual(cellophane([1, 2, 3]).reverse().array, [3, 2, 1]); | ||
t.end(); | ||
@@ -32,0 +42,0 @@ }); |
@@ -9,7 +9,3 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
t.equal(original.size(), 0); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.looseEqual(cellophane().size(), 0); | ||
t.end(); | ||
@@ -19,12 +15,6 @@ }); | ||
t.test('non-empty array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
t.equal(original.size(), 3); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.equal(original.array, array); | ||
t.looseEqual(cellophane([1, 2, 3]).size(), 3); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,9 +8,19 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.slice(1, 2); | ||
t.looseEqual(original.array, []); | ||
t.looseEqual(result.array, []); | ||
t.equal(original.array, array); | ||
var result = original.slice(1, 3); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 2 }, { foo: 3 }]); | ||
t.equal(result.array[0], y); | ||
t.equal(result.array[1], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -21,16 +31,12 @@ t.notEqual(original.array, result.array); | ||
t.test('slices the array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.slice(1, 2); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.looseEqual(result.array, [2]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().slice(1, 3).array, []); | ||
t.end(); | ||
}); | ||
t.end(); | ||
t.test('non-empty array', function(t) { | ||
t.looseEqual(cellophane([1, 2, 3]).slice(1, 3).array, [2, 3]); | ||
t.end(); | ||
}); | ||
}); |
175
test/sort.js
@@ -8,11 +8,22 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [z, x, y]; | ||
var original = cellophane(array); | ||
var result = original.sort(function(x, y) { | ||
return x > y; | ||
var result = original.sort(function(a, b) { | ||
return a.foo < b.foo ? -1 : 1; | ||
}); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 3 }, { foo: 1 }, { foo: 2 }]); | ||
t.equal(original.array[0], z); | ||
t.equal(original.array[1], x); | ||
t.equal(original.array[2], y); | ||
// result | ||
t.looseEqual(result.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(result.array[0], x); | ||
t.equal(result.array[1], y); | ||
t.equal(result.array[2], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
@@ -23,13 +34,15 @@ t.notEqual(original.array, result.array); | ||
t.test('empty array', function(t) { | ||
var result = cellophane().sort(function(a, b) { | ||
return a < b ? -1 : 1; | ||
}); | ||
t.looseEqual(result.array, []); | ||
t.end(); | ||
}); | ||
t.test('ascending order', function(t) { | ||
var array = [3, 1, 2]; | ||
var original = cellophane(array); | ||
var result = original.sort(function(x, y) { | ||
return x > y; | ||
var result = cellophane([3, 1, 2]).sort(function(a, b) { | ||
return a < b ? -1 : 1; | ||
}); | ||
t.looseEqual(result.array, [1, 2, 3]); | ||
t.looseEqual(original.array, [3, 1, 2]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
@@ -39,143 +52,31 @@ }); | ||
t.test('descending order', function(t) { | ||
var array = [3, 1, 2]; | ||
var original = cellophane(array); | ||
var result = original.sort(function(x, y) { | ||
return y > x; | ||
var result = cellophane([3, 1, 2]).sort(function(a, b) { | ||
return a < b ? 1 : -1; | ||
}); | ||
t.looseEqual(result.array, [3, 2, 1]); | ||
t.looseEqual(original.array, [3, 1, 2]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); | ||
test('sort(key, order)', function(t) { | ||
test('sort(opts)', function(t) { | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
var original = cellophane(array); | ||
var result = original.sort('foo.bar'); | ||
var result = cellophane().sort(); | ||
t.looseEqual(result.array, []); | ||
t.looseEqual(original.array, []); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('sort on a numeric value', function(t) { | ||
t.test('ascending order', function(t) { | ||
var array = [ | ||
{ foo: { bar: 3 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.sort('foo.bar'); | ||
t.looseEqual(result.array, [ | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 3 } } | ||
]); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 3 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } } | ||
]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('descending order', function(t) { | ||
var array = [ | ||
{ foo: { bar: 3 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.sort('foo.bar', 'desc'); | ||
t.looseEqual(result.array, [ | ||
{ foo: { bar: 3 } }, | ||
{ foo: { bar: 2 } }, | ||
{ foo: { bar: 1 } } | ||
]); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 3 } }, | ||
{ foo: { bar: 1 } }, | ||
{ foo: { bar: 2 } } | ||
]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('ascending order', function(t) { | ||
var result = cellophane([3, 1, 2]).sort(); | ||
t.looseEqual(result.array, [1, 2, 3]); | ||
t.end(); | ||
}); | ||
t.test('sort on a string value', function(t) { | ||
t.test('ascending order', function(t) { | ||
var array = [ | ||
{ foo: { bar: 'c' } }, | ||
{ foo: { bar: 'a' } }, | ||
{ foo: { bar: 'b' } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.sort('foo.bar'); | ||
t.looseEqual(result.array, [ | ||
{ foo: { bar: 'a' } }, | ||
{ foo: { bar: 'b' } }, | ||
{ foo: { bar: 'c' } } | ||
]); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 'c' } }, | ||
{ foo: { bar: 'a' } }, | ||
{ foo: { bar: 'b' } } | ||
]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('descending order', function(t) { | ||
var array = [ | ||
{ foo: { bar: 'c' } }, | ||
{ foo: { bar: 'a' } }, | ||
{ foo: { bar: 'b' } } | ||
]; | ||
var original = cellophane(array); | ||
var result = original.sort('foo.bar', 'desc'); | ||
t.looseEqual(result.array, [ | ||
{ foo: { bar: 'c' } }, | ||
{ foo: { bar: 'b' } }, | ||
{ foo: { bar: 'a' } } | ||
]); | ||
t.looseEqual(original.array, [ | ||
{ foo: { bar: 'c' } }, | ||
{ foo: { bar: 'a' } }, | ||
{ foo: { bar: 'b' } } | ||
]); | ||
t.equal(original.array, array); | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('descending order', function(t) { | ||
var result = cellophane([3, 1, 2]).sort({ order: 'desc' }); | ||
t.looseEqual(result.array, [3, 2, 1]); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -8,26 +8,35 @@ 'use strict'; | ||
t.test('empty array', function(t) { | ||
var array = []; | ||
t.test('returns a copy of the internal array', function(t) { | ||
var x = { foo: 1 }; | ||
var y = { foo: 2 }; | ||
var z = { foo: 3 }; | ||
var array = [x, y, z]; | ||
var original = cellophane(array); | ||
var result = original.unwrap(); | ||
t.looseEqual(result, []); | ||
t.looseEqual(original.array, []); | ||
t.notEqual(original.array, result); | ||
t.equal(original.array, array); | ||
// original | ||
t.looseEqual(original.array, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(original.array[0], x); | ||
t.equal(original.array[1], y); | ||
t.equal(original.array[2], z); | ||
// result | ||
t.looseEqual(result, [{ foo: 1 }, { foo: 2 }, { foo: 3 }]); | ||
t.equal(result[0], x); | ||
t.equal(result[1], y); | ||
t.equal(result[2], z); | ||
// original !== result | ||
t.notEqual(original, result); | ||
t.notEqual(original.array, result.array); | ||
t.end(); | ||
}); | ||
t.test('empty array', function(t) { | ||
t.looseEqual(cellophane().unwrap(), []); | ||
t.end(); | ||
}); | ||
t.test('non-empty array', function(t) { | ||
var array = [1, 2, 3]; | ||
var original = cellophane(array); | ||
var result = original.unwrap(); | ||
t.looseEqual(result, [1, 2, 3]); | ||
t.looseEqual(original.array, [1, 2, 3]); | ||
t.notEqual(original.array, result); | ||
t.equal(original.array, array); | ||
t.looseEqual(cellophane([1, 2, 3]).unwrap(), [1, 2, 3]); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
70929
37
1796
728
4
1
+ Addedcheque@^0.2.1