Comparing version 0.3.2 to 0.4.0
@@ -5,3 +5,3 @@ { | ||
"description": "a better array", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"keywords": [], | ||
@@ -8,0 +8,0 @@ "dependencies": { |
0.4.0 / 2013-11-29 | ||
================== | ||
* added custom .get(obj) mapping. | ||
* filter, reject, etc. no longer destructive. fixes regression in 0.3.2. | ||
* added unique(fn|str) | ||
* npm install array.js => array. thanks to @enricomarino :-) | ||
0.3.2 / 2013-11-26 | ||
@@ -3,0 +11,0 @@ ================== |
@@ -38,2 +38,4 @@ /** | ||
} else if ('object' == typeof arr) { | ||
arr._ctx = this._ctx = arr; | ||
arr.length = this.length = 0; | ||
// mixin to another object | ||
@@ -158,2 +160,24 @@ for(var key in array.prototype) arr[key] = array.prototype[key]; | ||
/** | ||
* Static: get the array item | ||
* | ||
* @param {Mixed} obj | ||
* @return {Mixed} | ||
* @api public | ||
*/ | ||
array.get = function(obj) { | ||
return obj; | ||
}; | ||
/** | ||
* Get the array item | ||
* | ||
* @param {Number} i | ||
* @return {Mixed} | ||
* @api public | ||
*/ | ||
array.prototype.get = array.get; | ||
/** | ||
* Attach the rest of the array methods | ||
@@ -177,7 +201,8 @@ */ | ||
array.prototype._remake = function(arr) { | ||
var i = this.length; | ||
while(~i) delete this[i--]; | ||
this.length = 0; | ||
proto.push.apply(this, arr); | ||
return this; | ||
} | ||
var clone = array(this._ctx || []); | ||
var i = clone.length; | ||
while(~i) delete clone[i--]; | ||
clone.get = this.get || array.get; | ||
proto.push.apply(clone, arr); | ||
return clone; | ||
}; |
@@ -73,3 +73,3 @@ /** | ||
for (var i = 0; i < len; ++i) { | ||
arr[i] = fn(arr[i], i); | ||
arr[i] = fn(arr.get(arr[i]), i); | ||
} | ||
@@ -105,4 +105,4 @@ | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
if (fn(val, i)) out.push(val); | ||
val = arr.get(arr[i]); | ||
if (fn(val, i)) out.push(arr[i]); | ||
} | ||
@@ -118,2 +118,3 @@ | ||
* | ||
* @param {Function|String} fn | ||
* @return {Enumerable} | ||
@@ -123,4 +124,5 @@ * @api public | ||
enumerable.unique = function(){ | ||
enumerable.unique = function(fn){ | ||
var out = [], | ||
vals = [], | ||
arr = this, | ||
@@ -130,6 +132,9 @@ len = arr.length, | ||
fn = (fn) ? toFunction(fn) : function(o) { return o; }; | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
if (~out.indexOf(val)) continue; | ||
out.push(val); | ||
val = fn(arr.get(arr[i])); | ||
if (~vals.indexOf(val)) continue; | ||
vals.push(val); | ||
out.push(arr[i]); | ||
} | ||
@@ -172,9 +177,9 @@ | ||
for (i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
if (!fn(arr[i], i)) out.push(val); | ||
val = arr.get(arr[i]); | ||
if (!fn(val, i)) out.push(arr[i]); | ||
} | ||
} else { | ||
for (i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
if (val != fn) out.push(val); | ||
val = arr.get(arr[i]); | ||
if (val != fn) out.push(arr[i]); | ||
} | ||
@@ -225,4 +230,4 @@ } | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
if (fn(val, i)) return val; | ||
val = arr.get(arr[i]); | ||
if (fn(val, i)) return arr[i]; | ||
} | ||
@@ -249,3 +254,3 @@ }; | ||
while(i--) if (fn(arr[i], i)) return arr[i]; | ||
while(i--) if (fn(arr.get(arr[i]), i)) return arr[i]; | ||
}; | ||
@@ -276,3 +281,3 @@ | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (!fn(val, i)) return false; | ||
@@ -304,3 +309,3 @@ } | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (fn(val, i)) return false; | ||
@@ -332,3 +337,3 @@ } | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (fn(val, i)) return true; | ||
@@ -361,3 +366,3 @@ } | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (fn(val, i)) ++n; | ||
@@ -382,3 +387,3 @@ } | ||
for (var i = 0; i < len; ++i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (val === obj) return i; | ||
@@ -404,3 +409,3 @@ } | ||
for (var i = --len; i >= 0; --i) { | ||
val = arr[i]; | ||
val = arr.get(arr[i]); | ||
if (val === obj) return i; | ||
@@ -446,3 +451,3 @@ } | ||
for (; i < len; ++i) { | ||
val = fn(val, arr[i], i); | ||
val = fn(val, arr.get(arr[i]), i); | ||
} | ||
@@ -486,3 +491,3 @@ | ||
for (i = 0; i < len; ++i) { | ||
n = fn(arr[i], i); | ||
n = fn(arr.get(arr[i]), i); | ||
max = n > max ? n : max; | ||
@@ -492,3 +497,3 @@ } | ||
for (i = 0; i < len; ++i) { | ||
n = arr[i]; | ||
n = arr.get(arr[i]); | ||
max = n > max ? n : max; | ||
@@ -533,3 +538,3 @@ } | ||
for (i = 0; i < len; ++i) { | ||
n = fn(arr[i], i); | ||
n = fn(arr.get(arr[i]), i); | ||
min = n < min ? n : min; | ||
@@ -539,3 +544,3 @@ } | ||
for (i = 0; i < len; ++i) { | ||
n = arr[i]; | ||
n = arr.get(arr[i]); | ||
min = n < min ? n : min; | ||
@@ -579,7 +584,7 @@ } | ||
for (i = 0; i < len; ++i) { | ||
n += fn(arr[i], i); | ||
n += fn(arr.get(arr[i]), i); | ||
} | ||
} else { | ||
for (i = 0; i < len; ++i) { | ||
n += arr[i]; | ||
n += arr.get(arr[i]); | ||
} | ||
@@ -623,7 +628,7 @@ } | ||
for (i = 0; i < len; ++i) { | ||
n += fn(arr[i], i); | ||
n += fn(arr.get(arr[i]), i); | ||
} | ||
} else { | ||
for (i = 0; i < len; ++i) { | ||
n += arr[i]; | ||
n += arr.get(arr[i]); | ||
} | ||
@@ -700,4 +705,4 @@ } | ||
for (var i = 0, len = arr.length; i < len; i++) { | ||
key = arr[i][str]; | ||
// TODO: assess, maybe we want out[i] = arr[i] | ||
key = arr.get(arr[i])[str]; | ||
// TODO: assess, maybe we want out[i] = arr.get(i) | ||
if(!key) continue; | ||
@@ -733,2 +738,3 @@ out[key] = arr[i]; | ||
var self = this; | ||
fn = toFunction(fn); | ||
@@ -745,3 +751,3 @@ | ||
function compare(a, b) { | ||
a = fn(a), b = fn(b); | ||
a = fn(self.get(a)), b = fn(self.get(b)); | ||
if(a < b) return -(dir); | ||
@@ -753,2 +759,2 @@ else if(a > b) return dir; | ||
return sort.call(this, compare); | ||
} | ||
}; |
{ | ||
"name": "array", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "a vocal, functional array", | ||
@@ -5,0 +5,0 @@ "main": "array.js", |
@@ -11,3 +11,3 @@ # array [![Build Status](https://secure.travis-ci.org/MatthewMueller/array.png?branch=master)](http://travis-ci.org/MatthewMueller/array) [![Build Status](https://david-dm.org/MatthewMueller/array.png)](https://david-dm.org/MatthewMueller/array) [![NPM version](https://badge.fury.io/js/array.js.png)](http://badge.fury.io/js/array.js) | ||
npm install array.js | ||
npm install array | ||
@@ -193,3 +193,3 @@ ### Browser with component | ||
#### `.unique()` | ||
#### `.unique(fn|str)` | ||
@@ -202,2 +202,6 @@ Select all unique values. | ||
```js | ||
users.unique('age') | ||
``` | ||
#### `.reject(fn|str|mixed)` | ||
@@ -204,0 +208,0 @@ |
@@ -189,2 +189,17 @@ /** | ||
}); | ||
describe('array.get', function() { | ||
it ('custom getter', function() { | ||
var nums = array([ { n: 1 }, { n: 3 }, { n: 5}, { n: 8}, { n: 20 } ]); | ||
nums.get = function(obj) { | ||
return obj.n; | ||
}; | ||
var out = nums.filter('> 4'); | ||
assert(3 == out.length); | ||
assert(5 == out[0].n); | ||
assert(8 == out[1].n); | ||
assert(20 == out[2].n); | ||
}); | ||
}); | ||
}); |
@@ -16,5 +16,13 @@ /** | ||
var fruits; | ||
var views; | ||
beforeEach(function() { | ||
fruits = array(fixture); | ||
views = array(fixture).map(function(fruit) { | ||
return { model: fruit }; | ||
}); | ||
views.get = function(obj) { | ||
return obj.model; | ||
}; | ||
}); | ||
@@ -55,2 +63,10 @@ | ||
}); | ||
it('should work with a custom array.get', function() { | ||
var names = views.map('name'); | ||
assert(3 == names.length); | ||
assert('apple' == names[0]); | ||
assert('pear' == names[1]); | ||
assert('grape' == names[2]); | ||
}); | ||
}); | ||
@@ -84,2 +100,18 @@ | ||
}); | ||
it('should work with custom array.get', function() { | ||
var out = views.filter('calories > 50'); | ||
assert(2 == out.length); | ||
assert('apple' == out[0].model.name); | ||
assert('pear' == out[1].model.name); | ||
}); | ||
it('shouldn\'t mutate the original array', function() { | ||
var len = fruits.length; | ||
var out = fruits.filter('calories < 50'); | ||
assert(len == fruits.length); | ||
assert(len > out.length); | ||
assert('apple' == fruits[0].name); | ||
assert('grape' == out[0].name); | ||
}) | ||
}); | ||
@@ -107,2 +139,21 @@ | ||
}); | ||
it('should uniquify based on fields', function() { | ||
fruits.push({ name: 'orange', calories: 100 }); | ||
var names = fruits.unique('calories').map('name'); | ||
assert(3 == names.length); | ||
assert('apple' === names[0]); | ||
assert('pear' === names[1]); | ||
assert('grape' === names[2]); | ||
}); | ||
it('should work with a custom get', function() { | ||
views.push({ model: { name: 'orange', calories: 100 }}); | ||
var names = views.unique('calories').map('name'); | ||
assert(3 == names.length); | ||
assert('apple' === names[0]); | ||
assert('pear' === names[1]); | ||
assert('grape' === names[2]); | ||
}); | ||
}); | ||
@@ -134,6 +185,19 @@ | ||
}); | ||
it('should work with a custom array.get', function() { | ||
var out = views.reject('calories > 50'); | ||
assert(1 == out.length); | ||
assert('grape' == out[0].model.name); | ||
}); | ||
it('shouldn\'t mutate the original array', function() { | ||
var len = fruits.length; | ||
var out = fruits.reject('calories > 50'); | ||
assert(len == fruits.length); | ||
assert(len > out.length); | ||
}) | ||
}); | ||
describe('find', function() { | ||
it('should find strings', function(){ | ||
it('should work with objects', function(){ | ||
fruits.push({ name : 'grape', color : 'red'}); | ||
@@ -274,2 +338,9 @@ var fruit = fruits.find({ name : 'grape'}); | ||
it('should work with custom get', function() { | ||
views.sort('calories'); | ||
assert('grape' === views[0].model.name); | ||
assert('apple' === views[1].model.name); | ||
assert('pear' === views[2].model.name); | ||
}) | ||
describe('descending direction', function () { | ||
@@ -276,0 +347,0 @@ it('should support numbers', function(){ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83973
2628
449