Ensure _.omit and _.pick work with primitives.

This commit is contained in:
John-David Dalton
2014-06-22 14:32:38 -07:00
parent 423730da76
commit c550e27d21
2 changed files with 34 additions and 6 deletions

View File

@@ -7182,7 +7182,7 @@
return basePick(object, negate(lodash.callback(predicate, thisArg, 3))); return basePick(object, negate(lodash.callback(predicate, thisArg, 3)));
} }
var omitProps = baseFlatten(arguments, false, false, 1); var omitProps = baseFlatten(arguments, false, false, 1);
return basePick(object, baseDifference(keysIn(object), arrayMap(omitProps, String))); return basePick(Object(object), baseDifference(keysIn(object), arrayMap(omitProps, String)));
} }
/** /**
@@ -7243,7 +7243,7 @@
if (object == null) { if (object == null) {
return {}; return {};
} }
return basePick(object, typeof predicate == 'function' return basePick(Object(object), typeof predicate == 'function'
? lodash.callback(predicate, thisArg, 3) ? lodash.callback(predicate, thisArg, 3)
: baseFlatten(arguments, false, false, 1)); : baseFlatten(arguments, false, false, 1));
} }

View File

@@ -7072,6 +7072,14 @@
deepEqual(_.omit(new Foo, 'a', 'c'), expected); deepEqual(_.omit(new Foo, 'a', 'c'), expected);
}); });
test('should return an empty object when `object` is `null` or `undefined`', 2, function() {
Object.prototype.a = 1;
_.each([null, undefined], function(value) {
deepEqual(_.omit(value, 'valueOf'), {});
});
delete Object.prototype.a;
});
test('should work with `arguments` objects as secondary arguments', 1, function() { test('should work with `arguments` objects as secondary arguments', 1, function() {
deepEqual(_.omit(object, args), expected); deepEqual(_.omit(object, args), expected);
}); });
@@ -7080,7 +7088,17 @@
deepEqual(_.omit([1, 2, 3], '0', '2'), { '1': 2 }); deepEqual(_.omit([1, 2, 3], '0', '2'), { '1': 2 });
}); });
test('should work with a callback argument', 1, function() { test('should work with a primitive `object` argument', 1, function() {
Number.prototype.a = 1;
Number.prototype.b = 2;
deepEqual(_.omit(1, 'b'), { 'a': 1 });
delete Number.prototype.a;
delete Number.prototype.b;
});
test('should work with a predicate argument', 1, function() {
var actual = _.omit(object, function(num) { var actual = _.omit(object, function(num) {
return num != 2; return num != 2;
}); });
@@ -7088,7 +7106,7 @@
deepEqual(actual, expected); deepEqual(actual, expected);
}); });
test('should pass the correct `callback` arguments', 1, function() { test('should pass the correct `predicate` arguments', 1, function() {
var args, var args,
object = { 'a': 1, 'b': 2 }, object = { 'a': 1, 'b': 2 },
lastKey = _.keys(object).pop(); lastKey = _.keys(object).pop();
@@ -7619,6 +7637,12 @@
deepEqual(_.pick(new Foo, 'a', 'c'), expected); deepEqual(_.pick(new Foo, 'a', 'c'), expected);
}); });
test('should return an empty object when `object` is `null` or `undefined`', 2, function() {
_.each([null, undefined], function(value) {
deepEqual(_.pick(value, 'valueOf'), {});
});
});
test('should work with `arguments` objects as secondary arguments', 1, function() { test('should work with `arguments` objects as secondary arguments', 1, function() {
deepEqual(_.pick(object, args), expected); deepEqual(_.pick(object, args), expected);
}); });
@@ -7627,7 +7651,11 @@
deepEqual(_.pick([1, 2, 3], '1'), { '1': 2 }); deepEqual(_.pick([1, 2, 3], '1'), { '1': 2 });
}); });
test('should work with a callback argument', 1, function() { test('should work with a primitive `object` argument', 1, function() {
deepEqual(_.pick(1, 'toFixed'), { 'toFixed': (1).toFixed });
});
test('should work with a predicate argument', 1, function() {
var actual = _.pick(object, function(num) { var actual = _.pick(object, function(num) {
return num != 2; return num != 2;
}); });
@@ -7635,7 +7663,7 @@
deepEqual(actual, expected); deepEqual(actual, expected);
}); });
test('should pass the correct `callback` arguments', 1, function() { test('should pass the correct `predicate` arguments', 1, function() {
var args, var args,
object = { 'a': 1, 'b': 2 }, object = { 'a': 1, 'b': 2 },
lastKey = _.keys(object).pop(); lastKey = _.keys(object).pop();