vendor/assets/javascripts/precompiled/development/array.js in sugar-rails-1.3.0 vs vendor/assets/javascripts/precompiled/development/array.js in sugar-rails-1.3.1

- old
+ new

@@ -1,8 +1,7 @@ - /*** * @package Array * @dependency core * @description Array manipulation and traversal, "fuzzy matching" against elements, alphanumeric sorting and collation, enumerable methods on Object. * @@ -25,13 +24,13 @@ iterateOverObject(match, function(key, value) { if(!multiMatch(el[key], match[key], scope, [el[key], el])) { result = false; } }); - return object.keys(match).length > 0 && result; + return result; } else { - return stringify(el) === stringify(match); + return isEqual(el, match); } } function transformArgument(el, map, context, mapArgs) { if(isUndefined(map)) { @@ -95,34 +94,33 @@ }, startIndex, loop); return returnIndex ? index : result; } function arrayUnique(arr, map) { - var result = [], o = {}, stringified, transformed; + var result = [], o = {}, transformed; arrayEach(arr, function(el, i) { transformed = map ? transformArgument(el, map, arr, [el, i, arr]) : el; - stringified = stringify(transformed); - if(!arrayObjectExists(o, stringified, el)) { - o[stringified] = transformed; + if(!checkForElementInHashAndSet(o, transformed)) { result.push(el); } }) return result; } function arrayIntersect(arr1, arr2, subtract) { var result = [], o = {}; arr2.each(function(el) { - o[stringify(el)] = el; + checkForElementInHashAndSet(o, el); }); arr1.each(function(el) { - var stringified = stringify(el), exists = arrayObjectExists(o, stringified, el); + var stringified = stringify(el), + isReference = !objectIsMatchedByValue(el); // Add the result to the array if: // 1. We're subtracting intersections or it doesn't already exist in the result and // 2. It exists in the compared array and we're adding, or it doesn't exist and we're removing. - if(exists != subtract) { - delete o[stringified]; + if(elementExistsInHash(o, stringified, el, isReference) != subtract) { + discardElementFromHash(o, stringified, el, isReference); result.push(el); } }); return result; } @@ -147,15 +145,49 @@ result = result.concat(arg); }); return result; } - function arrayObjectExists(hash, stringified, obj) { - return stringified in hash && (typeof obj !== 'function' || obj === hash[stringified]); + function elementExistsInHash(hash, key, element, isReference) { + var exists = key in hash; + if(isReference) { + if(!hash[key]) { + hash[key] = []; + } + exists = hash[key].indexOf(element) !== -1; + } + return exists; } + function checkForElementInHashAndSet(hash, element) { + var stringified = stringify(element), + isReference = !objectIsMatchedByValue(element), + exists = elementExistsInHash(hash, stringified, element, isReference); + if(isReference) { + hash[stringified].push(element); + } else { + hash[stringified] = element; + } + return exists; + } + function discardElementFromHash(hash, key, element, isReference) { + var arr, i = 0; + if(isReference) { + arr = hash[key]; + while(i < arr.length) { + if(arr[i] === element) { + arr.splice(i, 1); + } else { + i += 1; + } + } + } else { + delete hash[key]; + } + } + // Support methods function getMinOrMax(obj, map, which, all) { var edge, result = [], @@ -339,11 +371,14 @@ * ***/ 'create': function() { var result = [] multiArgs(arguments, function(a) { - if(a && a.callee) a = multiArgs(a); - result = result.concat(a); + if(isObjectPrimitive(a)) { + result = result.concat(array.prototype.slice.call(a)); + } else { + result.push(a); + } }); return result; } });