Issue #149: _.keys should throw a TypeError for non-objects.

This commit is contained in:
Kit Goncharov
2011-03-20 10:08:32 -06:00
parent 4869b4c6ca
commit 48abcd84c5
2 changed files with 26 additions and 0 deletions

View File

@@ -2,8 +2,33 @@ $(document).ready(function() {
module("Object functions (values, extend, isEqual, and so on...)");
function raises(block, expected, message) {
var pass = typeof block == 'function',
isRegExp = expected && Object.prototype.toString.call(expected) == '[object RegExp]',
isFunction = !isRegExp && typeof expected == 'function';
if (!isFunction && !isRegExp && message == null) {
message = expected;
expected = null;
}
if (pass) {
try {
block();
pass = false;
} catch (error) {
pass = expected == null || (isRegExp && expected.test(error)) || (isFunction && expected.call(null, error));
}
}
ok(pass, typeof message == 'string' && message || 'error');
}
test("objects: keys", function() {
var exception = /TypeError/;
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
raises(function() { _.keys(null); }, exception, 'throws an error for `null` values');
raises(function() { _.keys(void 0); }, exception, 'throws an error for `undefined` values');
raises(function() { _.keys(1); }, exception, 'throws an error for number primitives');
raises(function() { _.keys('a'); }, exception, 'throws an error for string primitives');
raises(function() { _.keys(true); }, exception, 'throws an error for boolean primitives');
});
test("objects: values", function() {

View File

@@ -504,6 +504,7 @@
// Retrieve the names of an object's properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
return keys;