mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Add support in _.isEmpty and _.size for jQuery/MooTools DOM query collections.
Former-commit-id: dc834256d09d7a3f2797ba65690961aad00717bf
This commit is contained in:
@@ -43,6 +43,7 @@
|
|||||||
'ArrayProto',
|
'ArrayProto',
|
||||||
'bind',
|
'bind',
|
||||||
'callee',
|
'callee',
|
||||||
|
'className',
|
||||||
'compareAscending',
|
'compareAscending',
|
||||||
'destValue',
|
'destValue',
|
||||||
'forIn',
|
'forIn',
|
||||||
@@ -57,6 +58,7 @@
|
|||||||
'isPlainObject',
|
'isPlainObject',
|
||||||
'methodName',
|
'methodName',
|
||||||
'noaccum',
|
'noaccum',
|
||||||
|
'objectClass',
|
||||||
'objectTypes',
|
'objectTypes',
|
||||||
'pass',
|
'pass',
|
||||||
'properties',
|
'properties',
|
||||||
|
|||||||
36
lodash.js
36
lodash.js
@@ -663,8 +663,8 @@
|
|||||||
var factory = Function(
|
var factory = Function(
|
||||||
'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn, ' +
|
'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn, ' +
|
||||||
'funcClass, hasOwnProperty, identity, indexOf, isArguments, isArray, ' +
|
'funcClass, hasOwnProperty, identity, indexOf, isArguments, isArray, ' +
|
||||||
'isPlainObject, iteratorBind, objectTypes, nativeKeys, propertyIsEnumerable, ' +
|
'isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys, ' +
|
||||||
'slice, stringClass, toString',
|
'propertyIsEnumerable, slice, stringClass, toString',
|
||||||
'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
|
'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
|
||||||
'return callee'
|
'return callee'
|
||||||
);
|
);
|
||||||
@@ -672,8 +672,8 @@
|
|||||||
return factory(
|
return factory(
|
||||||
arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn,
|
arrayLikeClasses, ArrayProto, bind, compareAscending, concat, forIn,
|
||||||
funcClass, hasOwnProperty, identity, indexOf, isArguments, isArray,
|
funcClass, hasOwnProperty, identity, indexOf, isArguments, isArray,
|
||||||
isPlainObject, iteratorBind, objectTypes, nativeKeys, propertyIsEnumerable,
|
isPlainObject, iteratorBind, objectClass, objectTypes, nativeKeys,
|
||||||
slice, stringClass, toString
|
propertyIsEnumerable, slice, stringClass, toString
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1259,9 +1259,12 @@
|
|||||||
'args': 'value',
|
'args': 'value',
|
||||||
'init': 'true',
|
'init': 'true',
|
||||||
'top':
|
'top':
|
||||||
'if (arrayLikeClasses[toString.call(value)]' +
|
'var className = toString.call(value), length = value.length;\n' +
|
||||||
(noArgsClass ? ' || isArguments(value)' : '') +
|
'if (arrayLikeClasses[className]' +
|
||||||
') return !value.length',
|
(noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
|
||||||
|
' (className == objectClass && length > -1 && length === length >>> 0 &&\n' +
|
||||||
|
' toString.call(value.splice) == funcClass)' +
|
||||||
|
') return !length',
|
||||||
'inLoop': {
|
'inLoop': {
|
||||||
'object': 'return false'
|
'object': 'return false'
|
||||||
}
|
}
|
||||||
@@ -1726,9 +1729,19 @@
|
|||||||
if (!value) {
|
if (!value) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return arrayLikeClasses[toString.call(value)] || (noArgsClass && isArguments(value))
|
var className = toString.call(value),
|
||||||
? value.length
|
length = value.length;
|
||||||
: keys(value).length;
|
|
||||||
|
// return `value.length` for `arguments` objects, arrays, strings, and DOM
|
||||||
|
// query collections of libraries like jQuery and MooTools
|
||||||
|
// http://code.google.com/p/fbug/source/browse/branches/firebug1.9/content/firebug/chrome/reps.js?r=12614#653
|
||||||
|
// http://trac.webkit.org/browser/trunk/Source/WebCore/inspector/InjectedScriptSource.js?rev=125186#L609
|
||||||
|
if (arrayLikeClasses[className] || (noArgsClass && isArguments(value)) ||
|
||||||
|
(className == objectClass && length > -1 && length === length >>> 0 &&
|
||||||
|
toString.call(value.splice) == funcClass)) {
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
return keys(value).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1961,8 +1974,7 @@
|
|||||||
' isFunc = typeof methodName == \'function\'',
|
' isFunc = typeof methodName == \'function\'',
|
||||||
'inLoop': {
|
'inLoop': {
|
||||||
'array':
|
'array':
|
||||||
'result[index] = (isFunc ? methodName : value[methodName])' +
|
'result[index] = (isFunc ? methodName : value[methodName]).apply(value, args)',
|
||||||
'.apply(value, args)',
|
|
||||||
'object':
|
'object':
|
||||||
'result' + (isKeysFast ? '[ownIndex] = ' : '.push') +
|
'result' + (isKeysFast ? '[ownIndex] = ' : '.push') +
|
||||||
'((isFunc ? methodName : value[methodName]).apply(value, args))'
|
'((isFunc ? methodName : value[methodName]).apply(value, args))'
|
||||||
|
|||||||
14
test/test.js
14
test/test.js
@@ -713,6 +713,13 @@
|
|||||||
equal(_.isEmpty({ 'length': 0 }), false);
|
equal(_.isEmpty({ 'length': 0 }), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should work with array-like collections', function() {
|
||||||
|
function Foo(elements) { Array.prototype.push.apply(this, elements); }
|
||||||
|
Foo.prototype = { 'splice': Array.prototype.splice };
|
||||||
|
|
||||||
|
equal(_.isEmpty(new Foo([])), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('should work with `arguments` objects (test in IE < 9)', function() {
|
test('should work with `arguments` objects (test in IE < 9)', function() {
|
||||||
equal(_.isEmpty(args), false);
|
equal(_.isEmpty(args), false);
|
||||||
});
|
});
|
||||||
@@ -1084,6 +1091,13 @@
|
|||||||
equal(_.size({ 'length': 3 }), 1);
|
equal(_.size({ 'length': 3 }), 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should work with array-like collections', function() {
|
||||||
|
function Foo(elements) { Array.prototype.push.apply(this, elements); }
|
||||||
|
Foo.prototype = { 'splice': Array.prototype.splice };
|
||||||
|
|
||||||
|
equal(_.size(new Foo([1, 2, 3])), 3);
|
||||||
|
});
|
||||||
|
|
||||||
test('should work with `arguments` objects (test in IE < 9)', function() {
|
test('should work with `arguments` objects (test in IE < 9)', function() {
|
||||||
equal(_.size(args), 3);
|
equal(_.size(args), 3);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user