mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-15 13:17:50 +00:00
Fix unit tests and reduce object/array references.
Former-commit-id: 25aaabd506cb4515cf833ebb104ba0146e4354dd
This commit is contained in:
89
lodash.js
89
lodash.js
@@ -16,12 +16,9 @@
|
|||||||
window = freeGlobal;
|
window = freeGlobal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Native prototype shortcuts */
|
/** Used to access array and object methods */
|
||||||
var ArrayProto = Array.prototype,
|
var arrayRef = [],
|
||||||
BoolProto = Boolean.prototype,
|
objectRef = {};
|
||||||
ObjectProto = Object.prototype,
|
|
||||||
NumberProto = Number.prototype,
|
|
||||||
StringProto = String.prototype;
|
|
||||||
|
|
||||||
/** Used to generate unique IDs */
|
/** Used to generate unique IDs */
|
||||||
var idCounter = 0;
|
var idCounter = 0;
|
||||||
@@ -54,7 +51,7 @@
|
|||||||
|
|
||||||
/** Used to detect if a method is native */
|
/** Used to detect if a method is native */
|
||||||
var reNative = RegExp('^' +
|
var reNative = RegExp('^' +
|
||||||
(ObjectProto.valueOf + '')
|
(objectRef.valueOf + '')
|
||||||
.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
|
.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
|
||||||
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
|
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
|
||||||
);
|
);
|
||||||
@@ -79,14 +76,14 @@
|
|||||||
|
|
||||||
/** Native method shortcuts */
|
/** Native method shortcuts */
|
||||||
var ceil = Math.ceil,
|
var ceil = Math.ceil,
|
||||||
concat = ArrayProto.concat,
|
concat = arrayRef.concat,
|
||||||
floor = Math.floor,
|
floor = Math.floor,
|
||||||
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
||||||
hasOwnProperty = ObjectProto.hasOwnProperty,
|
hasOwnProperty = objectRef.hasOwnProperty,
|
||||||
push = ArrayProto.push,
|
push = arrayRef.push,
|
||||||
propertyIsEnumerable = ObjectProto.propertyIsEnumerable,
|
propertyIsEnumerable = objectRef.propertyIsEnumerable,
|
||||||
slice = ArrayProto.slice,
|
slice = arrayRef.slice,
|
||||||
toString = ObjectProto.toString;
|
toString = objectRef.toString;
|
||||||
|
|
||||||
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
||||||
var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
|
var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
|
||||||
@@ -130,7 +127,7 @@
|
|||||||
* is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
|
* is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
|
||||||
*/
|
*/
|
||||||
var hasObjectSpliceBug = (hasObjectSpliceBug = { '0': 1, 'length': 1 },
|
var hasObjectSpliceBug = (hasObjectSpliceBug = { '0': 1, 'length': 1 },
|
||||||
ArrayProto.splice.call(hasObjectSpliceBug, 0, 1), hasObjectSpliceBug[0]);
|
arrayRef.splice.call(hasObjectSpliceBug, 0, 1), hasObjectSpliceBug[0]);
|
||||||
|
|
||||||
/** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
|
/** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
|
||||||
var noArgsEnum = true;
|
var noArgsEnum = true;
|
||||||
@@ -677,6 +674,32 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is an `arguments` object.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Objects
|
||||||
|
* @param {Mixed} value The value to check.
|
||||||
|
* @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* (function() { return _.isArguments(arguments); })(1, 2, 3);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isArguments([1, 2, 3]);
|
||||||
|
* // => false
|
||||||
|
*/
|
||||||
|
function isArguments(value) {
|
||||||
|
return toString.call(value) == argsClass;
|
||||||
|
}
|
||||||
|
// fallback for browsers that can't detect `arguments` objects by [[Class]]
|
||||||
|
if (noArgsClass) {
|
||||||
|
isArguments = function(value) {
|
||||||
|
return value ? hasOwnProperty.call(value, 'callee') : false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterates over `object`'s own and inherited enumerable properties, executing
|
* Iterates over `object`'s own and inherited enumerable properties, executing
|
||||||
* the `callback` for each property. The `callback` is bound to `thisArg` and
|
* the `callback` for each property. The `callback` is bound to `thisArg` and
|
||||||
@@ -731,32 +754,6 @@
|
|||||||
*/
|
*/
|
||||||
var forOwn = createIterator(forEachIteratorOptions, forOwnIteratorOptions);
|
var forOwn = createIterator(forEachIteratorOptions, forOwnIteratorOptions);
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if `value` is an `arguments` object.
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @memberOf _
|
|
||||||
* @category Objects
|
|
||||||
* @param {Mixed} value The value to check.
|
|
||||||
* @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* (function() { return _.isArguments(arguments); })(1, 2, 3);
|
|
||||||
* // => true
|
|
||||||
*
|
|
||||||
* _.isArguments([1, 2, 3]);
|
|
||||||
* // => false
|
|
||||||
*/
|
|
||||||
function isArguments(value) {
|
|
||||||
return toString.call(value) == argsClass;
|
|
||||||
}
|
|
||||||
// fallback for browsers that can't detect `arguments` objects by [[Class]]
|
|
||||||
if (noArgsClass) {
|
|
||||||
isArguments = function(value) {
|
|
||||||
return value ? hasOwnProperty.call(value, 'callee') : false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A fallback implementation of `isPlainObject` that checks if a given `value`
|
* A fallback implementation of `isPlainObject` that checks if a given `value`
|
||||||
* is an object created by the `Object` constructor, assuming objects created
|
* is an object created by the `Object` constructor, assuming objects created
|
||||||
@@ -1685,7 +1682,7 @@
|
|||||||
if (isFunc) {
|
if (isFunc) {
|
||||||
callback = createCallback(callback, thisArg);
|
callback = createCallback(callback, thisArg);
|
||||||
} else {
|
} else {
|
||||||
var props = concat.apply(ArrayProto, arguments);
|
var props = concat.apply(arrayRef, arguments);
|
||||||
}
|
}
|
||||||
forIn(object, function(value, key, object) {
|
forIn(object, function(value, key, object) {
|
||||||
if (isFunc
|
if (isFunc
|
||||||
@@ -1749,7 +1746,7 @@
|
|||||||
var result = {};
|
var result = {};
|
||||||
if (typeof callback != 'function') {
|
if (typeof callback != 'function') {
|
||||||
var index = 0,
|
var index = 0,
|
||||||
props = concat.apply(ArrayProto, arguments),
|
props = concat.apply(arrayRef, arguments),
|
||||||
length = props.length;
|
length = props.length;
|
||||||
|
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
@@ -2528,7 +2525,7 @@
|
|||||||
function difference(array) {
|
function difference(array) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array ? array.length : 0,
|
length = array ? array.length : 0,
|
||||||
flattened = concat.apply(ArrayProto, arguments),
|
flattened = concat.apply(arrayRef, arguments),
|
||||||
contains = cachedContains(flattened, length),
|
contains = cachedContains(flattened, length),
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
@@ -2939,7 +2936,7 @@
|
|||||||
*/
|
*/
|
||||||
function union() {
|
function union() {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
flattened = concat.apply(ArrayProto, arguments),
|
flattened = concat.apply(arrayRef, arguments),
|
||||||
length = flattened.length,
|
length = flattened.length,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
@@ -4112,7 +4109,7 @@
|
|||||||
|
|
||||||
// add all mutator Array functions to the wrapper.
|
// add all mutator Array functions to the wrapper.
|
||||||
forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
||||||
var func = ArrayProto[methodName];
|
var func = arrayRef[methodName];
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
lodash.prototype[methodName] = function() {
|
||||||
var value = this.__wrapped__;
|
var value = this.__wrapped__;
|
||||||
@@ -4133,7 +4130,7 @@
|
|||||||
|
|
||||||
// add all accessor Array functions to the wrapper.
|
// add all accessor Array functions to the wrapper.
|
||||||
forEach(['concat', 'join', 'slice'], function(methodName) {
|
forEach(['concat', 'join', 'slice'], function(methodName) {
|
||||||
var func = ArrayProto[methodName];
|
var func = arrayRef[methodName];
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
lodash.prototype[methodName] = function() {
|
||||||
var value = this.__wrapped__,
|
var value = this.__wrapped__,
|
||||||
|
|||||||
@@ -42,7 +42,12 @@
|
|||||||
var freeze = Object.freeze;
|
var freeze = Object.freeze;
|
||||||
|
|
||||||
/** Used to set property descriptors */
|
/** Used to set property descriptors */
|
||||||
var setDescriptor = Object.defineProperty;
|
var setDescriptor = (function(fn) {
|
||||||
|
try {
|
||||||
|
var o = {};
|
||||||
|
return fn(o, o, o) && fn;
|
||||||
|
} catch(e) { }
|
||||||
|
}(Object.defineProperty));
|
||||||
|
|
||||||
/** Shortcut used to convert array-like objects to arrays */
|
/** Shortcut used to convert array-like objects to arrays */
|
||||||
var slice = [].slice;
|
var slice = [].slice;
|
||||||
@@ -1810,7 +1815,6 @@
|
|||||||
var funcs = _.without.apply(_, [_.functions(_)].concat([
|
var funcs = _.without.apply(_, [_.functions(_)].concat([
|
||||||
'_',
|
'_',
|
||||||
'_iteratorTemplate',
|
'_iteratorTemplate',
|
||||||
'_shimKeys',
|
|
||||||
'after',
|
'after',
|
||||||
'bind',
|
'bind',
|
||||||
'bindAll',
|
'bindAll',
|
||||||
|
|||||||
Reference in New Issue
Block a user