mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Update Underscore/Backbone tests and make them passable.
This commit is contained in:
274
vendor/underscore/test/objects.js
vendored
274
vendor/underscore/test/objects.js
vendored
@@ -6,7 +6,7 @@
|
||||
var testElement = typeof document === 'object' ? document.createElement('div') : void 0;
|
||||
|
||||
test('keys', function() {
|
||||
deepEqual(_.keys({one : 1, two : 2}), ['one', 'two'], 'can extract the keys from an object');
|
||||
deepEqual(_.keys({one: 1, two: 2}), ['one', 'two'], 'can extract the keys from an object');
|
||||
// the test above is not safe because it relies on for-in enumeration order
|
||||
var a = []; a[1] = 0;
|
||||
deepEqual(_.keys(a), ['1'], 'is not fooled by sparse arrays; see issue #95');
|
||||
@@ -18,17 +18,17 @@
|
||||
|
||||
// keys that may be missed if the implementation isn't careful
|
||||
var trouble = {
|
||||
'constructor': Object,
|
||||
'valueOf': _.noop,
|
||||
'hasOwnProperty': null,
|
||||
'toString': 5,
|
||||
'toLocaleString': undefined,
|
||||
'propertyIsEnumerable': /a/,
|
||||
'isPrototypeOf': this,
|
||||
'__defineGetter__': Boolean,
|
||||
'__defineSetter__': {},
|
||||
'__lookupSetter__': false,
|
||||
'__lookupGetter__': []
|
||||
constructor: Object,
|
||||
valueOf: _.noop,
|
||||
hasOwnProperty: null,
|
||||
toString: 5,
|
||||
toLocaleString: void 0,
|
||||
propertyIsEnumerable: /a/,
|
||||
isPrototypeOf: this,
|
||||
__defineGetter__: Boolean,
|
||||
__defineSetter__: {},
|
||||
__lookupSetter__: false,
|
||||
__lookupGetter__: []
|
||||
};
|
||||
var troubleKeys = ['constructor', 'valueOf', 'hasOwnProperty', 'toString', 'toLocaleString', 'propertyIsEnumerable',
|
||||
'isPrototypeOf', '__defineGetter__', '__defineSetter__', '__lookupSetter__', '__lookupGetter__'].sort();
|
||||
@@ -36,7 +36,7 @@
|
||||
});
|
||||
|
||||
test('allKeys', function() {
|
||||
deepEqual(_.allKeys({one : 1, two : 2}), ['one', 'two'], 'can extract the allKeys from an object');
|
||||
deepEqual(_.allKeys({one: 1, two: 2}), ['one', 'two'], 'can extract the allKeys from an object');
|
||||
// the test above is not safe because it relies on for-in enumeration order
|
||||
var a = []; a[1] = 0;
|
||||
deepEqual(_.allKeys(a), ['1'], 'is not fooled by sparse arrays; see issue #95');
|
||||
@@ -54,7 +54,7 @@
|
||||
valueOf: _.noop,
|
||||
hasOwnProperty: null,
|
||||
toString: 5,
|
||||
toLocaleString: undefined,
|
||||
toLocaleString: void 0,
|
||||
propertyIsEnumerable: /a/,
|
||||
isPrototypeOf: this
|
||||
};
|
||||
@@ -93,7 +93,7 @@
|
||||
});
|
||||
|
||||
test('functions', function() {
|
||||
var obj = {a : 'dash', b : _.map, c : /yo/, d : _.reduce};
|
||||
var obj = {a: 'dash', b: _.map, c: /yo/, d: _.reduce};
|
||||
deepEqual(['b', 'd'], _.functions(obj), 'can grab the function names of any passed-in object');
|
||||
|
||||
var Animal = function(){};
|
||||
@@ -127,13 +127,13 @@
|
||||
|
||||
try {
|
||||
result = {};
|
||||
_.extend(result, null, undefined, {a: 1});
|
||||
} catch(ex) {}
|
||||
_.extend(result, null, void 0, {a: 1});
|
||||
} catch(e) { /* ignored */ }
|
||||
|
||||
equal(result.a, 1, 'should not error on `null` or `undefined` sources');
|
||||
|
||||
strictEqual(_.extend(null, {a: 1}), null, 'extending null results in null');
|
||||
strictEqual(_.extend(undefined, {a: 1}), undefined, 'extending undefined results in undefined');
|
||||
strictEqual(_.extend(void 0, {a: 1}), void 0, 'extending undefined results in undefined');
|
||||
});
|
||||
|
||||
test('extendOwn', function() {
|
||||
@@ -154,13 +154,13 @@
|
||||
deepEqual(_.extendOwn({}, subObj), {c: 'd'}, 'assign copies own properties from source');
|
||||
|
||||
result = {};
|
||||
deepEqual(_.assign(result, null, undefined, {a: 1}), {a: 1}, 'should not error on `null` or `undefined` sources');
|
||||
deepEqual(_.assign(result, null, void 0, {a: 1}), {a: 1}, 'should not error on `null` or `undefined` sources');
|
||||
|
||||
_.each(['a', 5, null, false], function(val) {
|
||||
strictEqual(_.assign(val, {a: 1}), val, 'assigning non-objects results in returning the non-object value');
|
||||
});
|
||||
|
||||
strictEqual(_.extendOwn(undefined, {a: 1}), undefined, 'assigning undefined results in undefined');
|
||||
strictEqual(_.extendOwn(void 0, {a: 1}), void 0, 'assigning undefined results in undefined');
|
||||
|
||||
result = _.extendOwn({a: 1, 0: 2, 1: '5', length: 6}, {0: 1, 1: 2, length: 2});
|
||||
deepEqual(result, {a: 1, 0: 1, 1: 2, length: 2}, 'assign should treat array-like objects like normal objects');
|
||||
@@ -219,7 +219,7 @@
|
||||
deepEqual(result, {1: 'b'}, 'can omit numeric properties');
|
||||
|
||||
deepEqual(_.omit(null, 'a', 'b'), {}, 'non objects return empty object');
|
||||
deepEqual(_.omit(undefined, 'toString'), {}, 'null/undefined return empty object');
|
||||
deepEqual(_.omit(void 0, 'toString'), {}, 'null/undefined return empty object');
|
||||
deepEqual(_.omit(5, 'toString', 'b'), {}, 'returns empty object for primitives');
|
||||
|
||||
var data = {a: 1, b: 2, c: 3};
|
||||
@@ -257,17 +257,17 @@
|
||||
|
||||
try {
|
||||
options = {};
|
||||
_.defaults(options, null, undefined, {a: 1});
|
||||
} catch(ex) {}
|
||||
_.defaults(options, null, void 0, {a: 1});
|
||||
} catch(e) { /* ignored */ }
|
||||
|
||||
equal(options.a, 1, 'should not error on `null` or `undefined` sources');
|
||||
|
||||
strictEqual(_.defaults(null, {a: 1}), null, 'result is null if destination is null');
|
||||
strictEqual(_.defaults(undefined, {a: 1}), undefined, 'result is undefined if destination is undefined');
|
||||
deepEqual(_.defaults(null, {a: 1}), {a: 1}, 'defaults skips nulls');
|
||||
deepEqual(_.defaults(void 0, {a: 1}), {a: 1}, 'defaults skips undefined');
|
||||
});
|
||||
|
||||
test('clone', function() {
|
||||
var moe = {name : 'moe', lucky : [13, 27, 34]};
|
||||
var moe = {name: 'moe', lucky: [13, 27, 34]};
|
||||
var clone = _.clone(moe);
|
||||
equal(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||
|
||||
@@ -277,7 +277,7 @@
|
||||
clone.lucky.push(101);
|
||||
equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
|
||||
|
||||
equal(_.clone(undefined), void 0, 'non objects should not be changed by clone');
|
||||
equal(_.clone(void 0), void 0, 'non objects should not be changed by clone');
|
||||
equal(_.clone(1), 1, 'non objects should not be changed by clone');
|
||||
equal(_.clone(null), null, 'non objects should not be changed by clone');
|
||||
});
|
||||
@@ -286,7 +286,7 @@
|
||||
var Parent = function() {};
|
||||
Parent.prototype = {foo: function() {}, bar: 2};
|
||||
|
||||
_.each(['foo', null, undefined, 1], function(val) {
|
||||
_.each(['foo', null, void 0, 1], function(val) {
|
||||
deepEqual(_.create(val), {}, 'should return empty object when a non-object is provided');
|
||||
});
|
||||
|
||||
@@ -324,8 +324,8 @@
|
||||
|
||||
ok(!_.isEqual(0, -0), '`0` is not equal to `-0`');
|
||||
ok(!_.isEqual(-0, 0), 'Commutative equality is implemented for `0` and `-0`');
|
||||
ok(!_.isEqual(null, undefined), '`null` is not equal to `undefined`');
|
||||
ok(!_.isEqual(undefined, null), 'Commutative equality is implemented for `null` and `undefined`');
|
||||
ok(!_.isEqual(null, void 0), '`null` is not equal to `undefined`');
|
||||
ok(!_.isEqual(void 0, null), 'Commutative equality is implemented for `null` and `undefined`');
|
||||
|
||||
// String object and primitive comparisons.
|
||||
ok(_.isEqual('Curly', 'Curly'), 'Identical string primitives are equal');
|
||||
@@ -350,7 +350,7 @@
|
||||
|
||||
// Comparisons involving `NaN`.
|
||||
ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`');
|
||||
ok(_.isEqual(new Object(NaN), NaN), 'Object(`NaN`) is equal to `NaN`');
|
||||
ok(_.isEqual(new Number(NaN), NaN), 'Object(`NaN`) is equal to `NaN`');
|
||||
ok(!_.isEqual(61, NaN), 'A number primitive is not equal to `NaN`');
|
||||
ok(!_.isEqual(new Number(79), NaN), 'A number object is not equal to `NaN`');
|
||||
ok(!_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`');
|
||||
@@ -431,7 +431,7 @@
|
||||
|
||||
var sparse = [];
|
||||
sparse[1] = 5;
|
||||
ok(_.isEqual(sparse, [undefined, 5]), 'Handles sparse arrays as dense');
|
||||
ok(_.isEqual(sparse, [void 0, 5]), 'Handles sparse arrays as dense');
|
||||
|
||||
// Simple objects.
|
||||
ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true}), 'Objects containing identical primitives are equal');
|
||||
@@ -440,7 +440,7 @@
|
||||
ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), 'Objects of identical sizes with different property names are not equal');
|
||||
ok(!_.isEqual({a: 1, b: 2}, {a: 1}), 'Objects of different sizes are not equal');
|
||||
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), 'Commutative equality is implemented for objects');
|
||||
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), 'Objects with identical keys and different values are not equivalent');
|
||||
ok(!_.isEqual({x: 1, y: void 0}, {x: 1, z: 2}), 'Objects with identical keys and different values are not equivalent');
|
||||
|
||||
// `A` contains nested objects and arrays.
|
||||
a = {
|
||||
@@ -536,7 +536,7 @@
|
||||
ok(_.isEqual(a, b), 'Cyclic structures with nested and identically-named properties are equal');
|
||||
|
||||
// Chaining.
|
||||
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal');
|
||||
ok(!_.isEqual(_({x: 1, y: void 0}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal');
|
||||
|
||||
a = _({x: 1, y: 2}).chain();
|
||||
b = _({x: 1, y: 2}).chain();
|
||||
@@ -544,9 +544,9 @@
|
||||
|
||||
// Objects without a `constructor` property
|
||||
if (Object.create) {
|
||||
a = Object.create(null, {x: {value: 1, enumerable: true}});
|
||||
b = {x: 1};
|
||||
ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. from Object.create');
|
||||
a = Object.create(null, {x: {value: 1, enumerable: true}});
|
||||
b = {x: 1};
|
||||
ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. from Object.create');
|
||||
}
|
||||
|
||||
function Foo() { this.a = 1; }
|
||||
@@ -554,12 +554,19 @@
|
||||
|
||||
var other = {a: 1};
|
||||
strictEqual(_.isEqual(new Foo, other), false, 'Objects from different constructors are not equal');
|
||||
|
||||
|
||||
// Tricky object cases val comparisions
|
||||
equal(_.isEqual([0], [-0]), false);
|
||||
equal(_.isEqual({a: 0}, {a: -0}), false);
|
||||
equal(_.isEqual([NaN], [NaN]), true);
|
||||
equal(_.isEqual({a: NaN}, {a: NaN}), true);
|
||||
});
|
||||
|
||||
test('isEmpty', function() {
|
||||
ok(!_([1]).isEmpty(), '[1] is not empty');
|
||||
ok(_.isEmpty([]), '[] is empty');
|
||||
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
|
||||
ok(!_.isEmpty({one: 1}), '{one: 1} is not empty');
|
||||
ok(_.isEmpty({}), '{} is empty');
|
||||
ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty');
|
||||
ok(_.isEmpty(null), 'null is empty');
|
||||
@@ -567,7 +574,7 @@
|
||||
ok(_.isEmpty(''), 'the empty string is empty');
|
||||
ok(!_.isEmpty('moe'), 'but other strings are not');
|
||||
|
||||
var obj = {one : 1};
|
||||
var obj = {one: 1};
|
||||
delete obj.one;
|
||||
ok(_.isEmpty(obj), 'deleting all the keys from an object empties it');
|
||||
|
||||
@@ -576,7 +583,7 @@
|
||||
ok(!_.isEmpty(args('')), 'non-empty arguments object is not empty');
|
||||
|
||||
// covers collecting non-enumerable properties in IE < 9
|
||||
var nonEnumProp = {'toString': 5};
|
||||
var nonEnumProp = {toString: 5};
|
||||
ok(!_.isEmpty(nonEnumProp), 'non-enumerable property is not empty');
|
||||
});
|
||||
|
||||
@@ -602,9 +609,9 @@
|
||||
if (testElement) {
|
||||
ok(_.isObject(testElement), 'and DOM element');
|
||||
}
|
||||
ok(_.isObject(function () {}), 'and functions');
|
||||
ok(_.isObject(function() {}), 'and functions');
|
||||
ok(!_.isObject(null), 'but not null');
|
||||
ok(!_.isObject(undefined), 'and not undefined');
|
||||
ok(!_.isObject(void 0), 'and not undefined');
|
||||
ok(!_.isObject('string'), 'and not string');
|
||||
ok(!_.isObject(12), 'and not number');
|
||||
ok(!_.isObject(true), 'and not boolean');
|
||||
@@ -612,7 +619,7 @@
|
||||
});
|
||||
|
||||
test('isArray', function() {
|
||||
ok(!_.isArray(undefined), 'undefined vars are not arrays');
|
||||
ok(!_.isArray(void 0), 'undefined vars are not arrays');
|
||||
ok(!_.isArray(arguments), 'the arguments object is not an array');
|
||||
ok(_.isArray([1, 2, 3]), 'but arrays are');
|
||||
});
|
||||
@@ -631,7 +638,7 @@
|
||||
test('isNumber', function() {
|
||||
ok(!_.isNumber('string'), 'a string is not a number');
|
||||
ok(!_.isNumber(arguments), 'the arguments object is not a number');
|
||||
ok(!_.isNumber(undefined), 'undefined is not a number');
|
||||
ok(!_.isNumber(void 0), 'undefined is not a number');
|
||||
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
|
||||
ok(_.isNumber(NaN), 'NaN *is* a number');
|
||||
ok(_.isNumber(Infinity), 'Infinity is a number');
|
||||
@@ -644,7 +651,7 @@
|
||||
ok(!_.isBoolean('false'), 'the string "false" is not a boolean');
|
||||
ok(!_.isBoolean('true'), 'the string "true" is not a boolean');
|
||||
ok(!_.isBoolean(arguments), 'the arguments object is not a boolean');
|
||||
ok(!_.isBoolean(undefined), 'undefined is not a boolean');
|
||||
ok(!_.isBoolean(void 0), 'undefined is not a boolean');
|
||||
ok(!_.isBoolean(NaN), 'NaN is not a boolean');
|
||||
ok(!_.isBoolean(null), 'null is not a boolean');
|
||||
ok(_.isBoolean(true), 'but true is');
|
||||
@@ -652,7 +659,7 @@
|
||||
});
|
||||
|
||||
test('isFunction', function() {
|
||||
ok(!_.isFunction(undefined), 'undefined vars are not functions');
|
||||
ok(!_.isFunction(void 0), 'undefined vars are not functions');
|
||||
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
|
||||
ok(!_.isFunction('moe'), 'strings are not functions');
|
||||
ok(_.isFunction(_.isFunction), 'but functions are');
|
||||
@@ -661,6 +668,11 @@
|
||||
if (testElement) {
|
||||
ok(!_.isFunction(testElement), 'elements are not functions');
|
||||
}
|
||||
|
||||
var nodelist = typeof document != 'undefined' && document.childNodes;
|
||||
if (nodelist) {
|
||||
ok(!_.isFunction(nodelist));
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof Int8Array !== 'undefined') {
|
||||
@@ -669,9 +681,9 @@
|
||||
.map(_.propertyOf(typeof GLOBAL != 'undefined' ? GLOBAL : window))
|
||||
.compact()
|
||||
.each(function(TypedArray) {
|
||||
// PhantomJS reports `typeof UInt8Array == 'object'` and doesn't report toString TypeArray
|
||||
// as a function
|
||||
strictEqual(_.isFunction(TypedArray), Object.prototype.toString.call(TypedArray) === '[object Function]');
|
||||
// PhantomJS reports `typeof UInt8Array == 'object'` and doesn't report toString TypeArray
|
||||
// as a function
|
||||
strictEqual(_.isFunction(TypedArray), Object.prototype.toString.call(TypedArray) === '[object Function]');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -688,7 +700,7 @@
|
||||
});
|
||||
|
||||
test('isFinite', function() {
|
||||
ok(!_.isFinite(undefined), 'undefined is not finite');
|
||||
ok(!_.isFinite(void 0), 'undefined is not finite');
|
||||
ok(!_.isFinite(null), 'null is not finite');
|
||||
ok(!_.isFinite(NaN), 'NaN is not finite');
|
||||
ok(!_.isFinite(Infinity), 'Infinity is not finite');
|
||||
@@ -704,15 +716,16 @@
|
||||
});
|
||||
|
||||
test('isNaN', function() {
|
||||
ok(!_.isNaN(undefined), 'undefined is not NaN');
|
||||
ok(!_.isNaN(void 0), 'undefined is not NaN');
|
||||
ok(!_.isNaN(null), 'null is not NaN');
|
||||
ok(!_.isNaN(0), '0 is not NaN');
|
||||
ok(!_.isNaN(new Number(0)), 'wrapped 0 is not NaN');
|
||||
ok(_.isNaN(NaN), 'but NaN is');
|
||||
ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN');
|
||||
});
|
||||
|
||||
test('isNull', function() {
|
||||
ok(!_.isNull(undefined), 'undefined is not null');
|
||||
ok(!_.isNull(void 0), 'undefined is not null');
|
||||
ok(!_.isNull(NaN), 'NaN is not null');
|
||||
ok(_.isNull(null), 'but null is');
|
||||
});
|
||||
@@ -723,7 +736,7 @@
|
||||
ok(!_.isUndefined(false), 'false is defined');
|
||||
ok(!_.isUndefined(NaN), 'NaN is defined');
|
||||
ok(_.isUndefined(), 'nothing is undefined');
|
||||
ok(_.isUndefined(undefined), 'undefined is undefined');
|
||||
ok(_.isUndefined(void 0), 'undefined is undefined');
|
||||
});
|
||||
|
||||
test('isError', function() {
|
||||
@@ -755,7 +768,7 @@
|
||||
equal(intercepted, returned, 'can use tapped objects in a chain');
|
||||
});
|
||||
|
||||
test('has', function () {
|
||||
test('has', function() {
|
||||
var obj = {foo: 'bar', func: function(){}};
|
||||
ok(_.has(obj, 'foo'), 'has() checks that the object has a property.');
|
||||
ok(!_.has(obj, 'baz'), "has() returns false if the object doesn't have the property.");
|
||||
@@ -766,7 +779,7 @@
|
||||
child.prototype = obj;
|
||||
ok(!_.has(child, 'foo'), 'has() does not check the prototype chain for a property.');
|
||||
strictEqual(_.has(null, 'foo'), false, 'has() returns false for null');
|
||||
strictEqual(_.has(undefined, 'foo'), false, 'has() returns false for undefined');
|
||||
strictEqual(_.has(void 0, 'foo'), false, 'has() returns false for undefined');
|
||||
});
|
||||
|
||||
test('isMatch', function() {
|
||||
@@ -776,17 +789,17 @@
|
||||
equal(_.isMatch(moe, {hair: true}), true, 'Returns a boolean');
|
||||
equal(_.isMatch(curly, {hair: true}), false, 'Returns a boolean');
|
||||
|
||||
equal(_.isMatch(5, {__x__: undefined}), false, 'can match undefined props on primitives');
|
||||
equal(_.isMatch({__x__: undefined}, {__x__: undefined}), true, 'can match undefined props');
|
||||
equal(_.isMatch(5, {__x__: void 0}), false, 'can match undefined props on primitives');
|
||||
equal(_.isMatch({__x__: void 0}, {__x__: void 0}), true, 'can match undefined props');
|
||||
|
||||
equal(_.isMatch(null, {}), true, 'Empty spec called with null object returns true');
|
||||
equal(_.isMatch(null, {a: 1}), false, 'Non-empty spec called with null object returns false');
|
||||
|
||||
_.each([null, undefined], function(item) { strictEqual(_.isMatch(item, null), true, 'null matches null'); });
|
||||
_.each([null, undefined], function(item) { strictEqual(_.isMatch(item, null), true, 'null matches {}'); });
|
||||
strictEqual(_.isMatch({b: 1}, {a: undefined}), false, 'handles undefined values (1683)');
|
||||
_.each([null, void 0], function(item) { strictEqual(_.isMatch(item, null), true, 'null matches null'); });
|
||||
_.each([null, void 0], function(item) { strictEqual(_.isMatch(item, null), true, 'null matches {}'); });
|
||||
strictEqual(_.isMatch({b: 1}, {a: void 0}), false, 'handles undefined values (1683)');
|
||||
|
||||
_.each([true, 5, NaN, null, undefined], function(item) {
|
||||
_.each([true, 5, NaN, null, void 0], function(item) {
|
||||
strictEqual(_.isMatch({a: 1}, item), true, 'treats primitives as empty');
|
||||
});
|
||||
|
||||
@@ -805,8 +818,8 @@
|
||||
ok(_.isMatch({x: 5, y: 1}, Prototest), 'spec can be a function');
|
||||
|
||||
//null edge cases
|
||||
var oCon = {'constructor': Object};
|
||||
deepEqual(_.map([null, undefined, 5, {}], _.partial(_.isMatch, _, oCon)), [false, false, false, true], 'doesnt falsey match constructor on undefined/null');
|
||||
var oCon = {constructor: Object};
|
||||
deepEqual(_.map([null, void 0, 5, {}], _.partial(_.isMatch, _, oCon)), [false, false, false, true], 'doesnt falsey match constructor on undefined/null');
|
||||
});
|
||||
|
||||
test('matcher', function() {
|
||||
@@ -817,21 +830,21 @@
|
||||
equal(_.matcher({hair: true})(moe), true, 'Returns a boolean');
|
||||
equal(_.matcher({hair: true})(curly), false, 'Returns a boolean');
|
||||
|
||||
equal(_.matcher({__x__: undefined})(5), false, 'can match undefined props on primitives');
|
||||
equal(_.matcher({__x__: undefined})({__x__: undefined}), true, 'can match undefined props');
|
||||
equal(_.matcher({__x__: void 0})(5), false, 'can match undefined props on primitives');
|
||||
equal(_.matcher({__x__: void 0})({__x__: void 0}), true, 'can match undefined props');
|
||||
|
||||
equal(_.matcher({})(null), true, 'Empty spec called with null object returns true');
|
||||
equal(_.matcher({a: 1})(null), false, 'Non-empty spec called with null object returns false');
|
||||
|
||||
ok(_.find(stooges, _.matcher({hair: false})) === curly, 'returns a predicate that can be used by finding functions.');
|
||||
ok(_.find(stooges, _.matcher(moe)) === moe, 'can be used to locate an object exists in a collection.');
|
||||
deepEqual(_.where([null, undefined], {a: 1}), [], 'Do not throw on null values.');
|
||||
deepEqual(_.where([null, void 0], {a: 1}), [], 'Do not throw on null values.');
|
||||
|
||||
deepEqual(_.where([null, undefined], null), [null, undefined], 'null matches null');
|
||||
deepEqual(_.where([null, undefined], {}), [null, undefined], 'null matches {}');
|
||||
deepEqual(_.where([{b: 1}], {a: undefined}), [], 'handles undefined values (1683)');
|
||||
deepEqual(_.where([null, void 0], null), [null, void 0], 'null matches null');
|
||||
deepEqual(_.where([null, void 0], {}), [null, void 0], 'null matches {}');
|
||||
deepEqual(_.where([{b: 1}], {a: void 0}), [], 'handles undefined values (1683)');
|
||||
|
||||
_.each([true, 5, NaN, null, undefined], function(item) {
|
||||
_.each([true, 5, NaN, null, void 0], function(item) {
|
||||
deepEqual(_.where([{a: 1}], item), [{a: 1}], 'treats primitives as empty');
|
||||
});
|
||||
|
||||
@@ -852,82 +865,25 @@
|
||||
ok(_.matcher(Prototest)({x: 5, y: 1}), 'spec can be a function');
|
||||
|
||||
// #1729
|
||||
var o = {'b': 1};
|
||||
var o = {b: 1};
|
||||
var m = _.matcher(o);
|
||||
|
||||
equal(m({'b': 1}), true);
|
||||
equal(m({b: 1}), true);
|
||||
o.b = 2;
|
||||
o.a = 1;
|
||||
equal(m({'b': 1}), true, 'changing spec object doesnt change matches result');
|
||||
equal(m({b: 1}), true, 'changing spec object doesnt change matches result');
|
||||
|
||||
|
||||
//null edge cases
|
||||
var oCon = _.matcher({'constructor': Object});
|
||||
deepEqual(_.map([null, undefined, 5, {}], oCon), [false, false, false, true], 'doesnt falsey match constructor on undefined/null');
|
||||
});
|
||||
|
||||
test('matcher', function() {
|
||||
var moe = {name: 'Moe Howard', hair: true};
|
||||
var curly = {name: 'Curly Howard', hair: false};
|
||||
var stooges = [moe, curly];
|
||||
|
||||
equal(_.matcher({hair: true})(moe), true, 'Returns a boolean');
|
||||
equal(_.matcher({hair: true})(curly), false, 'Returns a boolean');
|
||||
|
||||
equal(_.matcher({__x__: undefined})(5), false, 'can match undefined props on primitives');
|
||||
equal(_.matcher({__x__: undefined})({__x__: undefined}), true, 'can match undefined props');
|
||||
|
||||
equal(_.matcher({})(null), true, 'Empty spec called with null object returns true');
|
||||
equal(_.matcher({a: 1})(null), false, 'Non-empty spec called with null object returns false');
|
||||
|
||||
ok(_.find(stooges, _.matcher({hair: false})) === curly, 'returns a predicate that can be used by finding functions.');
|
||||
ok(_.find(stooges, _.matcher(moe)) === moe, 'can be used to locate an object exists in a collection.');
|
||||
deepEqual(_.where([null, undefined], {a: 1}), [], 'Do not throw on null values.');
|
||||
|
||||
deepEqual(_.where([null, undefined], null), [null, undefined], 'null matches null');
|
||||
deepEqual(_.where([null, undefined], {}), [null, undefined], 'null matches {}');
|
||||
deepEqual(_.where([{b: 1}], {a: undefined}), [], 'handles undefined values (1683)');
|
||||
|
||||
_.each([true, 5, NaN, null, undefined], function(item) {
|
||||
deepEqual(_.where([{a: 1}], item), [{a: 1}], 'treats primitives as empty');
|
||||
});
|
||||
|
||||
function Prototest() {}
|
||||
Prototest.prototype.x = 1;
|
||||
var specObj = new Prototest;
|
||||
var protospec = _.matcher(specObj);
|
||||
equal(protospec({x: 2}), true, 'spec is restricted to own properties');
|
||||
|
||||
specObj.y = 5;
|
||||
protospec = _.matcher(specObj);
|
||||
equal(protospec({x: 1, y: 5}), true);
|
||||
equal(protospec({x: 1, y: 4}), false);
|
||||
|
||||
ok(_.matcher({x: 1, y: 5})(specObj), 'inherited and own properties are checked on the test object');
|
||||
|
||||
Prototest.x = 5;
|
||||
ok(_.matcher(Prototest)({x: 5, y: 1}), 'spec can be a function');
|
||||
|
||||
// #1729
|
||||
var o = {'b': 1};
|
||||
var m = _.matcher(o);
|
||||
|
||||
equal(m({'b': 1}), true);
|
||||
o.b = 2;
|
||||
o.a = 1;
|
||||
equal(m({'b': 1}), true, 'changing spec object doesnt change matches result');
|
||||
|
||||
|
||||
//null edge cases
|
||||
var oCon = _.matcher({'constructor': Object});
|
||||
deepEqual(_.map([null, undefined, 5, {}], oCon), [false, false, false, true], 'doesnt falsey match constructor on undefined/null');
|
||||
var oCon = _.matcher({constructor: Object});
|
||||
deepEqual(_.map([null, void 0, 5, {}], oCon), [false, false, false, true], 'doesnt falsey match constructor on undefined/null');
|
||||
});
|
||||
|
||||
test('findKey', function() {
|
||||
var objects = {
|
||||
a: {'a': 0, 'b': 0},
|
||||
b: {'a': 1, 'b': 1},
|
||||
c: {'a': 2, 'b': 2}
|
||||
a: {a: 0, b: 0},
|
||||
b: {a: 1, b: 1},
|
||||
c: {a: 2, b: 2}
|
||||
};
|
||||
|
||||
equal(_.findKey(objects, function(obj) {
|
||||
@@ -942,7 +898,7 @@
|
||||
|
||||
equal(_.findKey(objects, function(obj) {
|
||||
return obj.b * obj.a === 5;
|
||||
}), undefined);
|
||||
}), void 0);
|
||||
|
||||
strictEqual(_.findKey([1, 2, 3, 4, 5, 6], function(obj) {
|
||||
return obj === 3;
|
||||
@@ -950,7 +906,7 @@
|
||||
|
||||
strictEqual(_.findKey(objects, function(a) {
|
||||
return a.foo === null;
|
||||
}), undefined);
|
||||
}), void 0);
|
||||
|
||||
_.findKey({a: {a: 1}}, function(a, key, obj) {
|
||||
equal(key, 'a');
|
||||
@@ -965,53 +921,53 @@
|
||||
|
||||
|
||||
test('mapObject', function() {
|
||||
var obj = {'a': 1, 'b': 2};
|
||||
var objects = {
|
||||
a: {'a': 0, 'b': 0},
|
||||
b: {'a': 1, 'b': 1},
|
||||
c: {'a': 2, 'b': 2}
|
||||
var obj = {a: 1, b: 2};
|
||||
var objects = {
|
||||
a: {a: 0, b: 0},
|
||||
b: {a: 1, b: 1},
|
||||
c: {a: 2, b: 2}
|
||||
};
|
||||
|
||||
deepEqual(_.mapObject(obj, function(val) {
|
||||
return val * 2;
|
||||
}), {'a': 2, 'b': 4}, 'simple objects');
|
||||
}), {a: 2, b: 4}, 'simple objects');
|
||||
|
||||
deepEqual(_.mapObject(objects, function(val) {
|
||||
return _.reduce(val, function(memo,v){
|
||||
return memo + v;
|
||||
},0);
|
||||
}), {'a': 0, 'b': 2, 'c': 4}, 'nested objects');
|
||||
return _.reduce(val, function(memo, v){
|
||||
return memo + v;
|
||||
}, 0);
|
||||
}), {a: 0, b: 2, c: 4}, 'nested objects');
|
||||
|
||||
deepEqual(_.mapObject(obj, function(val,key,obj) {
|
||||
return obj[key] * 2;
|
||||
}), {'a': 2, 'b': 4}, 'correct keys');
|
||||
deepEqual(_.mapObject(obj, function(val, key, o) {
|
||||
return o[key] * 2;
|
||||
}), {a: 2, b: 4}, 'correct keys');
|
||||
|
||||
deepEqual(_.mapObject([1,2], function(val) {
|
||||
deepEqual(_.mapObject([1, 2], function(val) {
|
||||
return val * 2;
|
||||
}), {'0': 2, '1': 4}, 'check behavior for arrays');
|
||||
}), {0: 2, 1: 4}, 'check behavior for arrays');
|
||||
|
||||
deepEqual(_.mapObject(obj, function(val) {
|
||||
return val * this.multiplier;
|
||||
}, {multiplier : 3}), {'a': 3, 'b': 6}, 'keep context');
|
||||
}, {multiplier: 3}), {a: 3, b: 6}, 'keep context');
|
||||
|
||||
deepEqual(_.mapObject({a: 1}, function() {
|
||||
return this.length;
|
||||
}, [1,2]), {'a': 2}, 'called with context');
|
||||
}, [1, 2]), {a: 2}, 'called with context');
|
||||
|
||||
var ids = _.mapObject({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){
|
||||
return n.id;
|
||||
});
|
||||
deepEqual(ids, {'length': undefined, '0': '1', '1': '2'}, 'Check with array-like objects');
|
||||
deepEqual(ids, {length: void 0, 0: '1', 1: '2'}, 'Check with array-like objects');
|
||||
|
||||
// Passing a property name like _.pluck.
|
||||
var people = {'a': {name : 'moe', age : 30}, 'b': {name : 'curly', age : 50}};
|
||||
deepEqual(_.mapObject(people, 'name'), {'a': 'moe', 'b': 'curly'}, 'predicate string map to object properties');
|
||||
var people = {a: {name: 'moe', age: 30}, b: {name: 'curly', age: 50}};
|
||||
deepEqual(_.mapObject(people, 'name'), {a: 'moe', b: 'curly'}, 'predicate string map to object properties');
|
||||
|
||||
_.each([null, void 0, 1, 'abc', [], {}, undefined], function(val){
|
||||
_.each([null, void 0, 1, 'abc', [], {}, void 0], function(val){
|
||||
deepEqual(_.mapObject(val, _.identity), {}, 'mapValue identity');
|
||||
});
|
||||
|
||||
var Proto = function(){this.a = 1;};
|
||||
var Proto = function(){ this.a = 1; };
|
||||
Proto.prototype.b = 1;
|
||||
var protoObj = new Proto();
|
||||
deepEqual(_.mapObject(protoObj, _.identity), {a: 1}, 'ignore inherited values from prototypes');
|
||||
|
||||
Reference in New Issue
Block a user