Comparing version 0.4.0 to 0.5.0
@@ -7,2 +7,3 @@ | ||
'append' : require('./array/append'), | ||
'collect' : require('./array/collect'), | ||
'combine' : require('./array/combine'), | ||
@@ -9,0 +10,0 @@ 'compact' : require('./array/compact'), |
@@ -8,6 +8,10 @@ | ||
function append(arr1, arr2) { | ||
if (arr2 == null) { | ||
return arr1; | ||
} | ||
var pad = arr1.length, | ||
i = -1, | ||
n = arr2.length; | ||
while (++i < n) { | ||
len = arr2.length; | ||
while (++i < len) { | ||
arr1[pad + i] = arr2[i]; | ||
@@ -14,0 +18,0 @@ } |
@@ -8,8 +8,10 @@ var indexOf = require('./indexOf'); | ||
function combine(arr1, arr2) { | ||
if (arr2 == null) { | ||
return arr1; | ||
} | ||
var x, length = arr2.length; | ||
for (x = 0; x < length; x++) { | ||
if (indexOf(arr1, arr2[x]) === -1) { | ||
arr1.push(arr2[x]); | ||
var i = -1, len = arr2.length; | ||
while (++i < len) { | ||
if (indexOf(arr1, arr2[i]) === -1) { | ||
arr1.push(arr2[i]); | ||
} | ||
@@ -16,0 +18,0 @@ } |
@@ -7,10 +7,13 @@ var makeIterator = require('../function/makeIterator_'); | ||
function every(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
var result = true, | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
callback = makeIterator(callback, thisObj); | ||
var result = true; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
if (!callback.call(thisObj, arr[i], i, arr) ) { | ||
if (!callback(arr[i], i, arr) ) { | ||
result = false; | ||
@@ -20,2 +23,3 @@ break; | ||
} | ||
return result; | ||
@@ -22,0 +26,0 @@ } |
@@ -8,9 +8,16 @@ var forEach = require('./forEach'); | ||
function filter(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
forEach(arr, function (val, i, arr) { | ||
if ( callback.call(thisObj, val, i, arr) ) { | ||
results.push(val); | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
if (callback(value, i, arr)) { | ||
results.push(value); | ||
} | ||
}); | ||
} | ||
return results; | ||
@@ -17,0 +24,0 @@ } |
@@ -7,6 +7,10 @@ var makeIterator = require('../function/makeIterator_'); | ||
function findIndex(arr, iterator, thisObj){ | ||
iterator = makeIterator(iterator); | ||
var i = -1, n = arr.length; | ||
while (++i < n) { | ||
if (iterator.call(thisObj, arr[i], i, arr)) { | ||
iterator = makeIterator(iterator, thisObj); | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
if (iterator(arr[i], i, arr)) { | ||
return i; | ||
@@ -13,0 +17,0 @@ } |
@@ -7,5 +7,7 @@ var isArray = require('../lang/isArray'); | ||
function flattenTo(arr, result, level) { | ||
if (level === 0) { | ||
if (arr == null) { | ||
return result; | ||
} else if (level === 0) { | ||
result.push.apply(result, arr); | ||
return; | ||
return result; | ||
} | ||
@@ -15,4 +17,4 @@ | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -19,0 +21,0 @@ if (isArray(value)) { |
@@ -11,4 +11,4 @@ | ||
var i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
@@ -15,0 +15,0 @@ // work properly on IE 7-8. see #64 |
@@ -8,5 +8,9 @@ | ||
fromIndex = fromIndex || 0; | ||
var n = arr.length, | ||
i = fromIndex < 0? n + fromIndex : fromIndex; | ||
while (i < n) { | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var len = arr.length, | ||
i = fromIndex < 0 ? len + fromIndex : fromIndex; | ||
while (i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
@@ -17,4 +21,6 @@ // work properly on IE 7-8. see #64 | ||
} | ||
i += 1; | ||
i++; | ||
} | ||
return -1; | ||
@@ -21,0 +27,0 @@ } |
@@ -8,6 +8,13 @@ var forEach = require('./forEach'); | ||
function invoke(arr, methodName, var_args){ | ||
if (arr == null) { | ||
return arr; | ||
} | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
forEach(arr, function(item){ | ||
item[methodName].apply(item, args); | ||
}); | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
value[methodName].apply(value, args); | ||
} | ||
return arr; | ||
@@ -14,0 +21,0 @@ } |
@@ -7,5 +7,10 @@ | ||
function lastIndexOf(arr, item, fromIndex) { | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var len = arr.length; | ||
fromIndex = (fromIndex == null || fromIndex >= len)? len - 1 : fromIndex; | ||
fromIndex = (fromIndex < 0)? len + fromIndex : fromIndex; | ||
while (fromIndex >= 0) { | ||
@@ -19,2 +24,3 @@ // we iterate over sparse items since there is no way to make it | ||
} | ||
return -1; | ||
@@ -21,0 +27,0 @@ } |
@@ -8,3 +8,3 @@ var forEach = require('./forEach'); | ||
function map(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
@@ -14,5 +14,8 @@ if (arr == null){ | ||
} | ||
forEach(arr, function (val, i, arr) { | ||
results[i] = callback.call(thisObj, val, i, arr); | ||
}); | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
results[i] = callback(arr[i], i, arr); | ||
} | ||
return results; | ||
@@ -19,0 +22,0 @@ } |
@@ -7,19 +7,24 @@ var forEach = require('./forEach'); | ||
*/ | ||
function max(arr, iterator){ | ||
if (arr.length && !iterator) { | ||
function max(arr, iterator, thisObj){ | ||
if (arr == null || !arr.length) { | ||
return Infinity; | ||
} else if (arr.length && !iterator) { | ||
return Math.max.apply(Math, arr); | ||
} else if (!arr.length) { | ||
return Infinity; | ||
} else { | ||
iterator = makeIterator(iterator); | ||
iterator = makeIterator(iterator, thisObj); | ||
var result, | ||
compare = -Infinity, | ||
tmp; | ||
forEach(arr, function(val, i, list){ | ||
tmp = iterator(val, i, list); | ||
if (tmp > compare) { | ||
compare = tmp; | ||
result = val; | ||
value, | ||
temp; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
temp = iterator(value, i, arr); | ||
if (temp > compare) { | ||
compare = temp; | ||
result = value; | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -26,0 +31,0 @@ } |
@@ -7,19 +7,24 @@ var forEach = require('./forEach'); | ||
*/ | ||
function min(arr, iterator){ | ||
if (arr.length && !iterator) { | ||
function min(arr, iterator, thisObj){ | ||
if (arr == null || !arr.length) { | ||
return -Infinity; | ||
} else if (arr.length && !iterator) { | ||
return Math.min.apply(Math, arr); | ||
} else if (!arr.length) { | ||
return -Infinity; | ||
} else { | ||
iterator = makeIterator(iterator); | ||
iterator = makeIterator(iterator, thisObj); | ||
var result, | ||
compare = Infinity, | ||
tmp; | ||
forEach(arr, function(val, i, list){ | ||
tmp = iterator(val, i, list); | ||
if (tmp < compare) { | ||
compare = tmp; | ||
result = val; | ||
value, | ||
temp; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
temp = iterator(value, i, arr); | ||
if (temp < compare) { | ||
compare = temp; | ||
result = value; | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -26,0 +31,0 @@ } |
@@ -7,3 +7,3 @@ var randInt = require('../random/randInt'); | ||
function pick(arr){ | ||
if (! arr.length) return; | ||
if (arr == null || !arr.length) return; | ||
var idx = randInt(0, arr.length - 1); | ||
@@ -10,0 +10,0 @@ return arr.splice(idx, 1)[0]; |
@@ -11,14 +11,19 @@ var forEach = require('./forEach'); | ||
if (!arr.length && !hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
if (arr == null || !arr.length) { | ||
if (!hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
} else { | ||
return initVal; | ||
} | ||
} | ||
forEach(arr, function (val, i, arr) { | ||
if (! hasInit) { | ||
result = val; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
if (!hasInit) { | ||
result = arr[i]; | ||
hasInit = true; | ||
} else { | ||
result = fn(result, val, i, arr); | ||
result = fn(result, arr[i], i, arr); | ||
} | ||
}); | ||
} | ||
@@ -25,0 +30,0 @@ return result; |
@@ -8,20 +8,22 @@ | ||
// check for args.length since initVal might be "undefined" see #gh-57 | ||
var hasInit = arguments.length > 2, | ||
result = initVal, | ||
i = arr.length, | ||
val; | ||
var hasInit = arguments.length > 2; | ||
if (!i && !hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
if (arr == null || !arr.length) { | ||
if (hasInit) { | ||
return initVal; | ||
} else { | ||
throw new Error('reduce of empty array with no initial value'); | ||
} | ||
} | ||
var i = arr.length, result = initVal, value; | ||
while (--i >= 0) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
val = arr[i]; | ||
if (! hasInit) { | ||
result = val; | ||
value = arr[i]; | ||
if (!hasInit) { | ||
result = value; | ||
hasInit = true; | ||
} else { | ||
result = fn(result, val, i, arr); | ||
result = fn(result, value, i, arr); | ||
} | ||
@@ -28,0 +30,0 @@ } |
@@ -8,9 +8,16 @@ var forEach = require('./forEach'); | ||
function reject(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
forEach(arr, function(val, i, arr) { | ||
if (!callback.call(thisObj, val, i, arr)) { | ||
results.push(val); | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
if (!callback(value, i, arr)) { | ||
results.push(value); | ||
} | ||
}); | ||
} | ||
return results; | ||
@@ -17,0 +24,0 @@ } |
@@ -8,14 +8,20 @@ var randInt = require('../random/randInt'); | ||
function shuffle(arr) { | ||
var result = [], | ||
var results = [], | ||
rnd; | ||
forEach(arr, function(val, i, arr){ | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
if (!i) { | ||
result[0] = val; | ||
results[0] = arr[0]; | ||
} else { | ||
rnd = randInt(0, i); | ||
result[i] = result[rnd]; | ||
result[rnd] = val; | ||
results[i] = results[rnd]; | ||
results[rnd] = arr[i]; | ||
} | ||
}); | ||
return result; | ||
} | ||
return results; | ||
} | ||
@@ -22,0 +28,0 @@ |
@@ -7,10 +7,13 @@ var makeIterator = require('../function/makeIterator_'); | ||
function some(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
var result = false, | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
callback = makeIterator(callback, thisObj); | ||
var result = false; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
if ( callback.call(thisObj, arr[i], i, arr) ) { | ||
if ( callback(arr[i], i, arr) ) { | ||
result = true; | ||
@@ -20,2 +23,3 @@ break; | ||
} | ||
return result; | ||
@@ -22,0 +26,0 @@ } |
@@ -7,3 +7,5 @@ | ||
function mergeSort(arr, compareFn) { | ||
if (arr.length < 2) { | ||
if (arr == null) { | ||
return []; | ||
} else if (arr.length < 2) { | ||
return arr; | ||
@@ -10,0 +12,0 @@ } |
@@ -8,20 +8,29 @@ | ||
segments = segments || 2; | ||
var results = []; | ||
if (array == null) { | ||
return results; | ||
} | ||
var output = [], | ||
segmentLength = Math.floor(array.length / segments), | ||
var minLength = Math.floor(array.length / segments), | ||
remainder = array.length % segments, | ||
start = 0, | ||
i = 0, | ||
n = array.length, | ||
len; | ||
len = array.length, | ||
segmentIndex = 0, | ||
segmentLength; | ||
while (start < n) { | ||
len = i++ < remainder ? segmentLength + 1 : segmentLength; | ||
output.push(array.slice(start, start + len)); | ||
start += len; | ||
while (i < len) { | ||
segmentLength = minLength; | ||
if (segmentIndex < remainder) { | ||
segmentLength++; | ||
} | ||
results.push(array.slice(i, i + segmentLength)); | ||
segmentIndex++; | ||
i += segmentLength; | ||
} | ||
return output; | ||
return results; | ||
} | ||
module.exports = split; | ||
@@ -5,14 +5,12 @@ var isFunction = require('../lang/isFunction'); | ||
* Creates an object that holds a lookup for the objects in the array. | ||
* The key for each value in `arr` is specified by the `key` parameter. | ||
* If `key` is a function, the function will be called with the value as | ||
* the parameter and the result will be used for the key. If `key` is a | ||
* string it will use the property specified by `key` as the key for each | ||
* value. | ||
*/ | ||
function toLookup(arr, key) { | ||
var result = {}, | ||
value, | ||
i = -1, n = arr.length; | ||
var result = {}; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length, value; | ||
if (isFunction(key)) { | ||
while (++i < n) { | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -22,3 +20,3 @@ result[key(value)] = value; | ||
} else { | ||
while (++i < n) { | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -28,2 +26,3 @@ result[value[key]] = value; | ||
} | ||
return result; | ||
@@ -30,0 +29,0 @@ } |
var unique = require('./unique'); | ||
var append = require('./append'); | ||
@@ -7,3 +8,9 @@ /** | ||
function union(arrs) { | ||
return unique(Array.prototype.concat.apply([], arguments)); | ||
var results = []; | ||
var i = -1, len = arguments.length; | ||
while (++i < len) { | ||
append(results, arguments[i]); | ||
} | ||
return unique(results); | ||
} | ||
@@ -10,0 +17,0 @@ |
var max = require('./max'); | ||
var pluck = require('./pluck'); | ||
var map = require('./map'); | ||
function getLength(arr) { | ||
return arr == null ? 0 : arr.length; | ||
} | ||
/** | ||
@@ -9,9 +14,13 @@ * Merges together the values of each of the arrays with the values at the | ||
function zip(arr){ | ||
var len = arr? max(pluck(arguments, 'length')) : 0, | ||
result = new Array(len), | ||
i = -1; | ||
while (++i < len){ | ||
result[i] = pluck(arguments, i); | ||
var len = arr ? max(map(arguments, getLength)) : 0, | ||
results = [], | ||
i = -1, | ||
item; | ||
while (++i < len) { | ||
results.push(map(arguments, function(item) { | ||
return item == null ? undefined : item[i]; | ||
})); | ||
} | ||
return result; | ||
return results; | ||
} | ||
@@ -18,0 +27,0 @@ |
mout changelog | ||
============== | ||
v0.5.0 (2013/04/04) | ||
------------------- | ||
- add `array/collect` | ||
- add `callback` parameter to `object/equals` and `object/deepEquals` to allow | ||
custom compare operations. | ||
- normalize behavior in `array/*` methods to treat `null` values as empty | ||
arrays when reading from array | ||
- add `date/parseIso` | ||
- add `date/isLeapYear` | ||
- add `date/totalDaysInMonth` | ||
- add `object/deepMatches` | ||
- change `function/makeIterator_` to use `deepMatches` (affects nearly all | ||
iteration methods) | ||
- Add `thisObj` parameter to `array/min` and `array/max` | ||
v0.4.0 (2013/02/26) | ||
@@ -5,0 +21,0 @@ ------------------- |
@@ -7,4 +7,4 @@ var forEach = require('./forEach'); | ||
*/ | ||
function filter(list, iterator, context) { | ||
iterator = makeIterator(iterator); | ||
function filter(list, iterator, thisObj) { | ||
iterator = makeIterator(iterator, thisObj); | ||
var results = []; | ||
@@ -15,3 +15,3 @@ if (!list) { | ||
forEach(list, function(value, index, list) { | ||
if (iterator.call(context, value, index, list)) { | ||
if (iterator(value, index, list)) { | ||
results[results.length] = value; | ||
@@ -18,0 +18,0 @@ } |
@@ -10,3 +10,3 @@ var isObject = require('../lang/isObject'); | ||
function map(list, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
// list.length to check array-like object, if not array-like | ||
@@ -18,3 +18,3 @@ // we simply map all the object values | ||
return arrMap(list, function (val, key, list) { | ||
return callback.call(thisObj, val, key, list); | ||
return callback(val, key, list); | ||
}); | ||
@@ -21,0 +21,0 @@ } |
@@ -7,7 +7,7 @@ var filter = require('./filter'); | ||
*/ | ||
function reject(list, iterator, context) { | ||
iterator = makeIterator(iterator); | ||
function reject(list, iterator, thisObj) { | ||
iterator = makeIterator(iterator, thisObj); | ||
return filter(list, function(value, index, list) { | ||
return !iterator.call(context, value, index, list); | ||
}, context); | ||
return !iterator(value, index, list); | ||
}, thisObj); | ||
} | ||
@@ -14,0 +14,0 @@ |
@@ -24,2 +24,27 @@ # array # | ||
## collect(arr, callback, [thisObj]):Array | ||
Maps the items in `arr` and concatenates the resulting arrays. | ||
See: [`map()`](#map) | ||
```js | ||
collect([1, 2, 3], function(val) { | ||
return [val, val % 2]; | ||
}); // [1, 1, 2, 0, 3, 1]; | ||
collect(['a', 'bb', ''], function(val) { | ||
return val.split(''); | ||
}); // ['a', 'b', 'b'] | ||
``` | ||
It also supports a shorthand syntax: | ||
```js | ||
var items = [{ a: [1] }, { b: 'foo' }, { a: [2, 3] }]; | ||
collect(items, 'a'); // [1, 2, 3]; | ||
``` | ||
## combine(arr1, arr2):Array | ||
@@ -352,2 +377,4 @@ | ||
See: [`collect()`](#collect) | ||
```js | ||
@@ -374,3 +401,3 @@ var nums = [1,2,3,4]; | ||
## max(arr[, iterator]):* | ||
## max(arr, [iterator], [thisObj]):* | ||
@@ -397,3 +424,3 @@ Returns maximum value inside array or use a custom iterator to define how items | ||
## min(arr[, iterator]):* | ||
## min(arr, [iterator], [thisObj]):* | ||
@@ -400,0 +427,0 @@ Returns minimum value inside array or use a custom iterator to define how items |
@@ -24,13 +24,13 @@ # object # | ||
## deepEquals(a, b):Boolean | ||
## deepEquals(a, b, [callback]):Boolean | ||
Recursively tests whether two objects contain the same keys and values. | ||
Recursively tests whether two objects contain the same keys and equal values. | ||
Tests whether the objects contain the same keys and equal values. If the | ||
values are both an object, it will recurse into the objects, checking if their | ||
keys/values are equal. | ||
`callback` specifies the equality comparison function used to compare | ||
non-object values. It defaults to using the strict equals (`===`) operator. | ||
It will only check the keys and values contained by the objects; it will not | ||
check the objects' prototypes. If the either of the values are not objects, | ||
they will be checked using the `===` operator. | ||
If the values are both an object, it will recurse into the objects, checking if | ||
their keys/values are equal. It will only check the keys and values contained | ||
by the objects; it will not check the objects' prototypes. If the either of | ||
the values are not objects, they will be checked using the `callback` function. | ||
@@ -46,2 +46,6 @@ Example: | ||
deepEquals(null, null); // true | ||
deepEquals( | ||
{ a: { b: 1 } }, | ||
{ a: { b: '1' } }, | ||
function(a, b) { return a == b; }); // true | ||
``` | ||
@@ -78,2 +82,29 @@ | ||
## deepMatches(target, pattern):Boolean | ||
Recursively checks if object contains all properties/value pairs. When both | ||
the target and pattern values are arrays, it checks that the target value | ||
contain matches for all the items in the pattern array (independent of order). | ||
```js | ||
var john = { | ||
name: 'John', | ||
age: 22, | ||
pets: [ | ||
{ type: 'cat', name: 'Grumpy Cat' }, | ||
{ type: 'dog', name: 'Hawk' } | ||
] | ||
}; | ||
deepMatches(john, { name: 'John' }); // true | ||
deepMatches(john, { age: 21 }); // false | ||
deepMatches(john, { pets: [ { type: 'cat' } ] }); // true | ||
deepMatches(john, { pets: [ { name: 'Hawk' } ] }); // true | ||
deepMatches(john, { pets: [ { name: 'Hairball' } ] }); // false | ||
``` | ||
See [`matches()`](#matches) | ||
## deepMixIn(target, ...objects):Object | ||
@@ -106,9 +137,12 @@ | ||
## equals(a, b):Boolean | ||
## equals(a, b, [callback]):Boolean | ||
Tests whether two objects contain the same keys and values. | ||
`callback` specifies the equality comparison function used to compare the | ||
values. It defaults to using the strict equals (`===`) operator. | ||
It will only check the keys and values contained by the objects; it will not | ||
check the objects' prototypes. If either of the values are not objects, they | ||
will be compared using the `===` operator. | ||
will be compared using the `callback` function. | ||
@@ -123,2 +157,3 @@ ```js | ||
equals(null, {}); // false | ||
equals({ a: 1 }, { a: '1' }, function(a, b) { return a == b; }); // true | ||
``` | ||
@@ -242,3 +277,3 @@ | ||
forOwn(obj, function(val, key, o){ | ||
forIn(obj, function(val, key, o){ | ||
result += val; | ||
@@ -431,4 +466,6 @@ keys.push(key); | ||
See [`deepMatches()`](#deepMatches) | ||
## merge(...objects):Object | ||
@@ -435,0 +472,0 @@ |
var prop = require('./prop'); | ||
var matches = require('../object/matches'); | ||
var deepMatches = require('../object/deepMatches'); | ||
@@ -9,3 +9,3 @@ /** | ||
*/ | ||
function makeIterator(src){ | ||
function makeIterator(src, thisObj){ | ||
switch(typeof src) { | ||
@@ -15,3 +15,3 @@ case 'object': | ||
return (src != null)? function(val, key, target){ | ||
return matches(val, src); | ||
return deepMatches(val, src); | ||
} : src; | ||
@@ -21,2 +21,10 @@ case 'string': | ||
return prop(src); | ||
case 'function': | ||
if (typeof thisObj === 'undefined') { | ||
return src; | ||
} else { | ||
return function(val, i, arr){ | ||
return src.call(thisObj, val, i, arr); | ||
}; | ||
} | ||
default: | ||
@@ -23,0 +31,0 @@ return src; |
/**@license | ||
* mout v0.4.0 | http://moutjs.com | MIT license | ||
* mout v0.5.0 | http://moutjs.com | MIT license | ||
*/ | ||
@@ -9,5 +9,6 @@ | ||
module.exports = { | ||
'VERSION' : '0.4.0', | ||
'VERSION' : '0.5.0', | ||
'array' : require('./array'), | ||
'collection' : require('./collection'), | ||
'date' : require('./date'), | ||
'function' : require('./function'), | ||
@@ -14,0 +15,0 @@ 'lang' : require('./lang'), |
@@ -9,2 +9,3 @@ | ||
'deepFillIn' : require('./object/deepFillIn'), | ||
'deepMatches' : require('./object/deepMatches'), | ||
'deepMixIn' : require('./object/deepMixIn'), | ||
@@ -11,0 +12,0 @@ 'equals' : require('./object/equals'), |
@@ -1,24 +0,23 @@ | ||
var hasOwn = require('./hasOwn'); | ||
var every = require('./every'); | ||
var isObject = require('../lang/isObject'); | ||
var equals = require('./equals'); | ||
function compareValues(value, key) { | ||
return hasOwn(this, key) && deepEquals(this[key], value); | ||
function defaultCompare(a, b) { | ||
return a === b; | ||
} | ||
function checkProperties(value, key) { | ||
return hasOwn(this, key); | ||
} | ||
/** | ||
* Recursively checks for same properties and values. | ||
*/ | ||
function deepEquals(a, b){ | ||
if (a === b) { | ||
return true; | ||
} else if (!isObject(a) || !isObject(b)) { | ||
return false; | ||
function deepEquals(a, b, callback){ | ||
callback = callback || defaultCompare; | ||
if (!isObject(a) || !isObject(b)) { | ||
return callback(a, b); | ||
} | ||
return every(a, compareValues, b) && every(b, checkProperties, a); | ||
function compare(a, b){ | ||
return deepEquals(a, b, callback); | ||
} | ||
return equals(a, b, compare); | ||
} | ||
@@ -25,0 +24,0 @@ |
@@ -5,6 +5,14 @@ var hasOwn = require('./hasOwn'); | ||
function compareValues(value, key) { | ||
return hasOwn(this, key) && this[key] === value; | ||
function defaultCompare(a, b) { | ||
return a === b; | ||
} | ||
// Makes a function to compare the object values from the specified compare | ||
// operation callback. | ||
function makeCompare(callback) { | ||
return function(value, key) { | ||
return hasOwn(this, key) && callback(value, this[key]); | ||
}; | ||
} | ||
function checkProperties(value, key) { | ||
@@ -17,10 +25,11 @@ return hasOwn(this, key); | ||
*/ | ||
function equals(a, b) { | ||
if (a === b) { | ||
return true; | ||
} else if (!isObject(a) || !isObject(b)) { | ||
return false; | ||
function equals(a, b, callback) { | ||
callback = callback || defaultCompare; | ||
if (!isObject(a) || !isObject(b)) { | ||
return callback(a, b); | ||
} | ||
return every(a, compareValues, b) && every(b, checkProperties, a); | ||
return (every(a, makeCompare(callback), b) && | ||
every(b, checkProperties, a)); | ||
} | ||
@@ -27,0 +36,0 @@ |
@@ -8,3 +8,3 @@ var forOwn = require('./forOwn'); | ||
function every(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result = true; | ||
@@ -14,3 +14,3 @@ forOwn(obj, function(val, key) { | ||
// syntax can be used to check property existence | ||
if (!callback.call(thisObj, val, key, obj)) { | ||
if (!callback(val, key, obj)) { | ||
result = false; | ||
@@ -17,0 +17,0 @@ return false; // break |
@@ -9,6 +9,6 @@ var forOwn = require('./forOwn'); | ||
function filterValues(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var output = {}; | ||
forOwn(obj, function(value, key, obj) { | ||
if (callback.call(thisObj, value, key, obj)) { | ||
if (callback(value, key, obj)) { | ||
output[key] = value; | ||
@@ -15,0 +15,0 @@ } |
@@ -8,6 +8,6 @@ var some = require('./some'); | ||
function find(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result; | ||
some(obj, function(value, key, obj) { | ||
if (callback.call(thisObj, value, key, obj)) { | ||
if (callback(value, key, obj)) { | ||
result = value; | ||
@@ -14,0 +14,0 @@ return true; //break |
@@ -9,6 +9,6 @@ var forOwn = require('./forOwn'); | ||
function mapValues(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var output = {}; | ||
forOwn(obj, function(val, key, obj) { | ||
output[key] = callback.call(thisObj, val, key, obj); | ||
output[key] = callback(val, key, obj); | ||
}); | ||
@@ -15,0 +15,0 @@ |
@@ -8,5 +8,5 @@ var filter = require('./filter'); | ||
function reject(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
return filter(obj, function(value, index, obj) { | ||
return !callback.call(thisObj, value, index, obj); | ||
return !callback(value, index, obj); | ||
}, thisObj); | ||
@@ -13,0 +13,0 @@ } |
@@ -8,6 +8,6 @@ var forOwn = require('./forOwn'); | ||
function some(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result = false; | ||
forOwn(obj, function(val, key) { | ||
if (callback.call(thisObj, val, key, obj)) { | ||
if (callback(val, key, obj)) { | ||
result = true; | ||
@@ -14,0 +14,0 @@ return false; // break |
{ | ||
"name": "mout", | ||
"description": "Modular Utilities", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"homepage": "http://moutjs.com/", | ||
@@ -52,3 +52,45 @@ "contributors": [ | ||
"rocambole": "~0.2.3" | ||
}, | ||
"testling": { | ||
"preprocess": "node build testling", | ||
"browsers": { | ||
"ie": [ | ||
7, | ||
8, | ||
9, | ||
10 | ||
], | ||
"firefox": [ | ||
17, | ||
"nightly" | ||
], | ||
"chrome": [ | ||
23, | ||
"canary" | ||
], | ||
"opera": [ | ||
12, | ||
"next" | ||
], | ||
"safari": [ | ||
5.1, | ||
6 | ||
], | ||
"iphone": [ | ||
6 | ||
], | ||
"ipad": [ | ||
6 | ||
] | ||
}, | ||
"scripts": [ | ||
"tests/lib/jasmine/jasmine.js", | ||
"tests/lib/jasmine/jasmine.async.js", | ||
"tests/lib/jasmine/jasmine-tap.js", | ||
"tests/lib/requirejs/require.js", | ||
"tests/testling/src.js", | ||
"tests/testling/specs.js", | ||
"tests/runner.js" | ||
] | ||
} | ||
} |
@@ -7,2 +7,3 @@ define(function(require){ | ||
'append' : require('./array/append'), | ||
'collect' : require('./array/collect'), | ||
'combine' : require('./array/combine'), | ||
@@ -9,0 +10,0 @@ 'compact' : require('./array/compact'), |
@@ -8,6 +8,10 @@ define(function () { | ||
function append(arr1, arr2) { | ||
if (arr2 == null) { | ||
return arr1; | ||
} | ||
var pad = arr1.length, | ||
i = -1, | ||
n = arr2.length; | ||
while (++i < n) { | ||
len = arr2.length; | ||
while (++i < len) { | ||
arr1[pad + i] = arr2[i]; | ||
@@ -14,0 +18,0 @@ } |
@@ -8,8 +8,10 @@ define(['./indexOf'], function (indexOf) { | ||
function combine(arr1, arr2) { | ||
if (arr2 == null) { | ||
return arr1; | ||
} | ||
var x, length = arr2.length; | ||
for (x = 0; x < length; x++) { | ||
if (indexOf(arr1, arr2[x]) === -1) { | ||
arr1.push(arr2[x]); | ||
var i = -1, len = arr2.length; | ||
while (++i < len) { | ||
if (indexOf(arr1, arr2[i]) === -1) { | ||
arr1.push(arr2[i]); | ||
} | ||
@@ -16,0 +18,0 @@ } |
@@ -7,10 +7,13 @@ define(['../function/makeIterator_'], function (makeIterator) { | ||
function every(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
var result = true, | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
callback = makeIterator(callback, thisObj); | ||
var result = true; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
if (!callback.call(thisObj, arr[i], i, arr) ) { | ||
if (!callback(arr[i], i, arr) ) { | ||
result = false; | ||
@@ -20,2 +23,3 @@ break; | ||
} | ||
return result; | ||
@@ -22,0 +26,0 @@ } |
@@ -7,9 +7,16 @@ define(['./forEach', '../function/makeIterator_'], function (forEach, makeIterator) { | ||
function filter(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
forEach(arr, function (val, i, arr) { | ||
if ( callback.call(thisObj, val, i, arr) ) { | ||
results.push(val); | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
if (callback(value, i, arr)) { | ||
results.push(value); | ||
} | ||
}); | ||
} | ||
return results; | ||
@@ -16,0 +23,0 @@ } |
@@ -7,6 +7,10 @@ define(['../function/makeIterator_'], function (makeIterator) { | ||
function findIndex(arr, iterator, thisObj){ | ||
iterator = makeIterator(iterator); | ||
var i = -1, n = arr.length; | ||
while (++i < n) { | ||
if (iterator.call(thisObj, arr[i], i, arr)) { | ||
iterator = makeIterator(iterator, thisObj); | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
if (iterator(arr[i], i, arr)) { | ||
return i; | ||
@@ -13,0 +17,0 @@ } |
@@ -7,5 +7,7 @@ define(['../lang/isArray'], function (isArray) { | ||
function flattenTo(arr, result, level) { | ||
if (level === 0) { | ||
if (arr == null) { | ||
return result; | ||
} else if (level === 0) { | ||
result.push.apply(result, arr); | ||
return; | ||
return result; | ||
} | ||
@@ -15,4 +17,4 @@ | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -19,0 +21,0 @@ if (isArray(value)) { |
@@ -11,4 +11,4 @@ define(function () { | ||
var i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
@@ -15,0 +15,0 @@ // work properly on IE 7-8. see #64 |
@@ -8,5 +8,9 @@ define(function () { | ||
fromIndex = fromIndex || 0; | ||
var n = arr.length, | ||
i = fromIndex < 0? n + fromIndex : fromIndex; | ||
while (i < n) { | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var len = arr.length, | ||
i = fromIndex < 0 ? len + fromIndex : fromIndex; | ||
while (i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
@@ -17,4 +21,6 @@ // work properly on IE 7-8. see #64 | ||
} | ||
i += 1; | ||
i++; | ||
} | ||
return -1; | ||
@@ -21,0 +27,0 @@ } |
@@ -8,6 +8,13 @@ define(['./forEach'], function (forEach) { | ||
function invoke(arr, methodName, var_args){ | ||
if (arr == null) { | ||
return arr; | ||
} | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
forEach(arr, function(item){ | ||
item[methodName].apply(item, args); | ||
}); | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
value[methodName].apply(value, args); | ||
} | ||
return arr; | ||
@@ -14,0 +21,0 @@ } |
@@ -7,5 +7,10 @@ define(function () { | ||
function lastIndexOf(arr, item, fromIndex) { | ||
if (arr == null) { | ||
return -1; | ||
} | ||
var len = arr.length; | ||
fromIndex = (fromIndex == null || fromIndex >= len)? len - 1 : fromIndex; | ||
fromIndex = (fromIndex < 0)? len + fromIndex : fromIndex; | ||
while (fromIndex >= 0) { | ||
@@ -19,2 +24,3 @@ // we iterate over sparse items since there is no way to make it | ||
} | ||
return -1; | ||
@@ -21,0 +27,0 @@ } |
@@ -7,3 +7,3 @@ define(['./forEach', '../function/makeIterator_'], function (forEach, makeIterator) { | ||
function map(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
@@ -13,5 +13,8 @@ if (arr == null){ | ||
} | ||
forEach(arr, function (val, i, arr) { | ||
results[i] = callback.call(thisObj, val, i, arr); | ||
}); | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
results[i] = callback(arr[i], i, arr); | ||
} | ||
return results; | ||
@@ -18,0 +21,0 @@ } |
@@ -6,19 +6,24 @@ define(['./forEach', '../function/makeIterator_'], function (forEach, makeIterator) { | ||
*/ | ||
function max(arr, iterator){ | ||
if (arr.length && !iterator) { | ||
function max(arr, iterator, thisObj){ | ||
if (arr == null || !arr.length) { | ||
return Infinity; | ||
} else if (arr.length && !iterator) { | ||
return Math.max.apply(Math, arr); | ||
} else if (!arr.length) { | ||
return Infinity; | ||
} else { | ||
iterator = makeIterator(iterator); | ||
iterator = makeIterator(iterator, thisObj); | ||
var result, | ||
compare = -Infinity, | ||
tmp; | ||
forEach(arr, function(val, i, list){ | ||
tmp = iterator(val, i, list); | ||
if (tmp > compare) { | ||
compare = tmp; | ||
result = val; | ||
value, | ||
temp; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
temp = iterator(value, i, arr); | ||
if (temp > compare) { | ||
compare = temp; | ||
result = value; | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -25,0 +30,0 @@ } |
@@ -6,19 +6,24 @@ define(['./forEach', '../function/makeIterator_'], function (forEach, makeIterator) { | ||
*/ | ||
function min(arr, iterator){ | ||
if (arr.length && !iterator) { | ||
function min(arr, iterator, thisObj){ | ||
if (arr == null || !arr.length) { | ||
return -Infinity; | ||
} else if (arr.length && !iterator) { | ||
return Math.min.apply(Math, arr); | ||
} else if (!arr.length) { | ||
return -Infinity; | ||
} else { | ||
iterator = makeIterator(iterator); | ||
iterator = makeIterator(iterator, thisObj); | ||
var result, | ||
compare = Infinity, | ||
tmp; | ||
forEach(arr, function(val, i, list){ | ||
tmp = iterator(val, i, list); | ||
if (tmp < compare) { | ||
compare = tmp; | ||
result = val; | ||
value, | ||
temp; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
value = arr[i]; | ||
temp = iterator(value, i, arr); | ||
if (temp < compare) { | ||
compare = temp; | ||
result = value; | ||
} | ||
}); | ||
} | ||
return result; | ||
@@ -25,0 +30,0 @@ } |
@@ -7,3 +7,3 @@ define(['../random/randInt'], function (randInt) { | ||
function pick(arr){ | ||
if (! arr.length) return; | ||
if (arr == null || !arr.length) return; | ||
var idx = randInt(0, arr.length - 1); | ||
@@ -10,0 +10,0 @@ return arr.splice(idx, 1)[0]; |
@@ -11,14 +11,19 @@ define(['./forEach'], function (forEach) { | ||
if (!arr.length && !hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
if (arr == null || !arr.length) { | ||
if (!hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
} else { | ||
return initVal; | ||
} | ||
} | ||
forEach(arr, function (val, i, arr) { | ||
if (! hasInit) { | ||
result = val; | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
if (!hasInit) { | ||
result = arr[i]; | ||
hasInit = true; | ||
} else { | ||
result = fn(result, val, i, arr); | ||
result = fn(result, arr[i], i, arr); | ||
} | ||
}); | ||
} | ||
@@ -25,0 +30,0 @@ return result; |
@@ -8,20 +8,22 @@ define(function () { | ||
// check for args.length since initVal might be "undefined" see #gh-57 | ||
var hasInit = arguments.length > 2, | ||
result = initVal, | ||
i = arr.length, | ||
val; | ||
var hasInit = arguments.length > 2; | ||
if (!i && !hasInit) { | ||
throw new Error('reduce of empty array with no initial value'); | ||
if (arr == null || !arr.length) { | ||
if (hasInit) { | ||
return initVal; | ||
} else { | ||
throw new Error('reduce of empty array with no initial value'); | ||
} | ||
} | ||
var i = arr.length, result = initVal, value; | ||
while (--i >= 0) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
val = arr[i]; | ||
if (! hasInit) { | ||
result = val; | ||
value = arr[i]; | ||
if (!hasInit) { | ||
result = value; | ||
hasInit = true; | ||
} else { | ||
result = fn(result, val, i, arr); | ||
result = fn(result, value, i, arr); | ||
} | ||
@@ -28,0 +30,0 @@ } |
@@ -7,9 +7,16 @@ define(['./forEach', '../function/makeIterator_'], function(forEach, makeIterator) { | ||
function reject(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var results = []; | ||
forEach(arr, function(val, i, arr) { | ||
if (!callback.call(thisObj, val, i, arr)) { | ||
results.push(val); | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
value = arr[i]; | ||
if (!callback(value, i, arr)) { | ||
results.push(value); | ||
} | ||
}); | ||
} | ||
return results; | ||
@@ -16,0 +23,0 @@ } |
@@ -7,14 +7,20 @@ define(['../random/randInt', './forEach'], function (randInt, forEach) { | ||
function shuffle(arr) { | ||
var result = [], | ||
var results = [], | ||
rnd; | ||
forEach(arr, function(val, i, arr){ | ||
if (arr == null) { | ||
return results; | ||
} | ||
var i = -1, len = arr.length, value; | ||
while (++i < len) { | ||
if (!i) { | ||
result[0] = val; | ||
results[0] = arr[0]; | ||
} else { | ||
rnd = randInt(0, i); | ||
result[i] = result[rnd]; | ||
result[rnd] = val; | ||
results[i] = results[rnd]; | ||
results[rnd] = arr[i]; | ||
} | ||
}); | ||
return result; | ||
} | ||
return results; | ||
} | ||
@@ -21,0 +27,0 @@ |
@@ -7,10 +7,13 @@ define(['../function/makeIterator_'], function (makeIterator) { | ||
function some(arr, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
var result = false, | ||
i = -1, | ||
n = arr.length; | ||
while (++i < n) { | ||
callback = makeIterator(callback, thisObj); | ||
var result = false; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length; | ||
while (++i < len) { | ||
// we iterate over sparse items since there is no way to make it | ||
// work properly on IE 7-8. see #64 | ||
if ( callback.call(thisObj, arr[i], i, arr) ) { | ||
if ( callback(arr[i], i, arr) ) { | ||
result = true; | ||
@@ -20,2 +23,3 @@ break; | ||
} | ||
return result; | ||
@@ -22,0 +26,0 @@ } |
@@ -7,3 +7,5 @@ define(function () { | ||
function mergeSort(arr, compareFn) { | ||
if (arr.length < 2) { | ||
if (arr == null) { | ||
return []; | ||
} else if (arr.length < 2) { | ||
return arr; | ||
@@ -10,0 +12,0 @@ } |
@@ -8,20 +8,29 @@ define(function() { | ||
segments = segments || 2; | ||
var results = []; | ||
if (array == null) { | ||
return results; | ||
} | ||
var output = [], | ||
segmentLength = Math.floor(array.length / segments), | ||
var minLength = Math.floor(array.length / segments), | ||
remainder = array.length % segments, | ||
start = 0, | ||
i = 0, | ||
n = array.length, | ||
len; | ||
len = array.length, | ||
segmentIndex = 0, | ||
segmentLength; | ||
while (start < n) { | ||
len = i++ < remainder ? segmentLength + 1 : segmentLength; | ||
output.push(array.slice(start, start + len)); | ||
start += len; | ||
while (i < len) { | ||
segmentLength = minLength; | ||
if (segmentIndex < remainder) { | ||
segmentLength++; | ||
} | ||
results.push(array.slice(i, i + segmentLength)); | ||
segmentIndex++; | ||
i += segmentLength; | ||
} | ||
return output; | ||
return results; | ||
} | ||
return split; | ||
}); |
@@ -5,14 +5,12 @@ define(['../lang/isFunction'], function (isFunction) { | ||
* Creates an object that holds a lookup for the objects in the array. | ||
* The key for each value in `arr` is specified by the `key` parameter. | ||
* If `key` is a function, the function will be called with the value as | ||
* the parameter and the result will be used for the key. If `key` is a | ||
* string it will use the property specified by `key` as the key for each | ||
* value. | ||
*/ | ||
function toLookup(arr, key) { | ||
var result = {}, | ||
value, | ||
i = -1, n = arr.length; | ||
var result = {}; | ||
if (arr == null) { | ||
return result; | ||
} | ||
var i = -1, len = arr.length, value; | ||
if (isFunction(key)) { | ||
while (++i < n) { | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -22,3 +20,3 @@ result[key(value)] = value; | ||
} else { | ||
while (++i < n) { | ||
while (++i < len) { | ||
value = arr[i]; | ||
@@ -28,2 +26,3 @@ result[value[key]] = value; | ||
} | ||
return result; | ||
@@ -30,0 +29,0 @@ } |
@@ -1,2 +0,2 @@ | ||
define(['./unique'], function (unique) { | ||
define(['./unique', './append'], function (unique, append) { | ||
@@ -7,3 +7,9 @@ /** | ||
function union(arrs) { | ||
return unique(Array.prototype.concat.apply([], arguments)); | ||
var results = []; | ||
var i = -1, len = arguments.length; | ||
while (++i < len) { | ||
append(results, arguments[i]); | ||
} | ||
return unique(results); | ||
} | ||
@@ -10,0 +16,0 @@ |
@@ -1,3 +0,7 @@ | ||
define(['./max', './pluck'], function (max, pluck) { | ||
define(['./max', './pluck', './map'], function (max, pluck, map) { | ||
function getLength(arr) { | ||
return arr == null ? 0 : arr.length; | ||
} | ||
/** | ||
@@ -8,9 +12,13 @@ * Merges together the values of each of the arrays with the values at the | ||
function zip(arr){ | ||
var len = arr? max(pluck(arguments, 'length')) : 0, | ||
result = new Array(len), | ||
i = -1; | ||
while (++i < len){ | ||
result[i] = pluck(arguments, i); | ||
var len = arr ? max(map(arguments, getLength)) : 0, | ||
results = [], | ||
i = -1, | ||
item; | ||
while (++i < len) { | ||
results.push(map(arguments, function(item) { | ||
return item == null ? undefined : item[i]; | ||
})); | ||
} | ||
return result; | ||
return results; | ||
} | ||
@@ -17,0 +25,0 @@ |
@@ -6,4 +6,4 @@ define(['./forEach', '../function/makeIterator_'], function (forEach, makeIterator) { | ||
*/ | ||
function filter(list, iterator, context) { | ||
iterator = makeIterator(iterator); | ||
function filter(list, iterator, thisObj) { | ||
iterator = makeIterator(iterator, thisObj); | ||
var results = []; | ||
@@ -14,3 +14,3 @@ if (!list) { | ||
forEach(list, function(value, index, list) { | ||
if (iterator.call(context, value, index, list)) { | ||
if (iterator(value, index, list)) { | ||
results[results.length] = value; | ||
@@ -17,0 +17,0 @@ } |
@@ -7,3 +7,3 @@ define(['../lang/isObject', '../object/values', '../array/map', '../function/makeIterator_'], function (isObject, values, arrMap, makeIterator) { | ||
function map(list, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
// list.length to check array-like object, if not array-like | ||
@@ -15,3 +15,3 @@ // we simply map all the object values | ||
return arrMap(list, function (val, key, list) { | ||
return callback.call(thisObj, val, key, list); | ||
return callback(val, key, list); | ||
}); | ||
@@ -18,0 +18,0 @@ } |
@@ -6,7 +6,7 @@ define(['./filter', '../function/makeIterator_'], function (filter, makeIterator) { | ||
*/ | ||
function reject(list, iterator, context) { | ||
iterator = makeIterator(iterator); | ||
function reject(list, iterator, thisObj) { | ||
iterator = makeIterator(iterator, thisObj); | ||
return filter(list, function(value, index, list) { | ||
return !iterator.call(context, value, index, list); | ||
}, context); | ||
return !iterator(value, index, list); | ||
}, thisObj); | ||
} | ||
@@ -13,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
define(['./prop', '../object/matches'], function(prop, matches) { | ||
define(['./prop', '../object/deepMatches'], function(prop, deepMatches) { | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
function makeIterator(src){ | ||
function makeIterator(src, thisObj){ | ||
switch(typeof src) { | ||
@@ -14,3 +14,3 @@ case 'object': | ||
return (src != null)? function(val, key, target){ | ||
return matches(val, src); | ||
return deepMatches(val, src); | ||
} : src; | ||
@@ -20,2 +20,10 @@ case 'string': | ||
return prop(src); | ||
case 'function': | ||
if (typeof thisObj === 'undefined') { | ||
return src; | ||
} else { | ||
return function(val, i, arr){ | ||
return src.call(thisObj, val, i, arr); | ||
}; | ||
} | ||
default: | ||
@@ -22,0 +30,0 @@ return src; |
/**@license | ||
* mout v0.4.0 | http://moutjs.com | MIT license | ||
* mout v0.5.0 | http://moutjs.com | MIT license | ||
*/ | ||
@@ -9,5 +9,6 @@ define(function(require){ | ||
return { | ||
'VERSION' : '0.4.0', | ||
'VERSION' : '0.5.0', | ||
'array' : require('./array'), | ||
'collection' : require('./collection'), | ||
'date' : require('./date'), | ||
'function' : require('./function'), | ||
@@ -14,0 +15,0 @@ 'lang' : require('./lang'), |
@@ -9,2 +9,3 @@ define(function(require){ | ||
'deepFillIn' : require('./object/deepFillIn'), | ||
'deepMatches' : require('./object/deepMatches'), | ||
'deepMixIn' : require('./object/deepMixIn'), | ||
@@ -11,0 +12,0 @@ 'equals' : require('./object/equals'), |
@@ -1,22 +0,22 @@ | ||
define(['./hasOwn', './every', '../lang/isObject'], function (hasOwn, every, isObject) { | ||
define(['../lang/isObject', './equals'], function (isObject, equals) { | ||
function compareValues(value, key) { | ||
return hasOwn(this, key) && deepEquals(this[key], value); | ||
function defaultCompare(a, b) { | ||
return a === b; | ||
} | ||
function checkProperties(value, key) { | ||
return hasOwn(this, key); | ||
} | ||
/** | ||
* Recursively checks for same properties and values. | ||
*/ | ||
function deepEquals(a, b){ | ||
if (a === b) { | ||
return true; | ||
} else if (!isObject(a) || !isObject(b)) { | ||
return false; | ||
function deepEquals(a, b, callback){ | ||
callback = callback || defaultCompare; | ||
if (!isObject(a) || !isObject(b)) { | ||
return callback(a, b); | ||
} | ||
return every(a, compareValues, b) && every(b, checkProperties, a); | ||
function compare(a, b){ | ||
return deepEquals(a, b, callback); | ||
} | ||
return equals(a, b, compare); | ||
} | ||
@@ -23,0 +23,0 @@ |
define(['./hasOwn', './every', '../lang/isObject'], function(hasOwn, every, isObject) { | ||
function compareValues(value, key) { | ||
return hasOwn(this, key) && this[key] === value; | ||
function defaultCompare(a, b) { | ||
return a === b; | ||
} | ||
// Makes a function to compare the object values from the specified compare | ||
// operation callback. | ||
function makeCompare(callback) { | ||
return function(value, key) { | ||
return hasOwn(this, key) && callback(value, this[key]); | ||
}; | ||
} | ||
function checkProperties(value, key) { | ||
@@ -14,10 +22,11 @@ return hasOwn(this, key); | ||
*/ | ||
function equals(a, b) { | ||
if (a === b) { | ||
return true; | ||
} else if (!isObject(a) || !isObject(b)) { | ||
return false; | ||
function equals(a, b, callback) { | ||
callback = callback || defaultCompare; | ||
if (!isObject(a) || !isObject(b)) { | ||
return callback(a, b); | ||
} | ||
return every(a, compareValues, b) && every(b, checkProperties, a); | ||
return (every(a, makeCompare(callback), b) && | ||
every(b, checkProperties, a)); | ||
} | ||
@@ -24,0 +33,0 @@ |
@@ -7,3 +7,3 @@ define(['./forOwn', '../function/makeIterator_'], function(forOwn, makeIterator) { | ||
function every(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result = true; | ||
@@ -13,3 +13,3 @@ forOwn(obj, function(val, key) { | ||
// syntax can be used to check property existence | ||
if (!callback.call(thisObj, val, key, obj)) { | ||
if (!callback(val, key, obj)) { | ||
result = false; | ||
@@ -16,0 +16,0 @@ return false; // break |
@@ -8,6 +8,6 @@ define(['./forOwn', '../function/makeIterator_'], function(forOwn, makeIterator) { | ||
function filterValues(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var output = {}; | ||
forOwn(obj, function(value, key, obj) { | ||
if (callback.call(thisObj, value, key, obj)) { | ||
if (callback(value, key, obj)) { | ||
output[key] = value; | ||
@@ -14,0 +14,0 @@ } |
@@ -7,6 +7,6 @@ define(['./some', '../function/makeIterator_'], function(some, makeIterator) { | ||
function find(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result; | ||
some(obj, function(value, key, obj) { | ||
if (callback.call(thisObj, value, key, obj)) { | ||
if (callback(value, key, obj)) { | ||
result = value; | ||
@@ -13,0 +13,0 @@ return true; //break |
@@ -8,6 +8,6 @@ define(['./forOwn', '../function/makeIterator_'], function(forOwn, makeIterator) { | ||
function mapValues(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var output = {}; | ||
forOwn(obj, function(val, key, obj) { | ||
output[key] = callback.call(thisObj, val, key, obj); | ||
output[key] = callback(val, key, obj); | ||
}); | ||
@@ -14,0 +14,0 @@ |
@@ -7,5 +7,5 @@ define(['./filter', '../function/makeIterator_'], function (filter, makeIterator) { | ||
function reject(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
return filter(obj, function(value, index, obj) { | ||
return !callback.call(thisObj, value, index, obj); | ||
return !callback(value, index, obj); | ||
}, thisObj); | ||
@@ -12,0 +12,0 @@ } |
@@ -7,6 +7,6 @@ define(['./forOwn', '../function/makeIterator_'], function(forOwn, makeIterator) { | ||
function some(obj, callback, thisObj) { | ||
callback = makeIterator(callback); | ||
callback = makeIterator(callback, thisObj); | ||
var result = false; | ||
forOwn(obj, function(val, key) { | ||
if (callback.call(thisObj, val, key, obj)) { | ||
if (callback(val, key, obj)) { | ||
result = true; | ||
@@ -13,0 +13,0 @@ return false; // break |
Sorry, the diff of this file is not supported yet
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
6916
314318
453