object-keys
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "object-keys", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Jordan Harband", | ||
@@ -21,8 +21,12 @@ "description": "An Object.keys replacement, in case Object.keys is not available. From https://github.com/kriskowal/es5-shim", | ||
], | ||
"dependencies": { | ||
"indexof": "~0.0.1", | ||
"is-extended": "~0.0.4" | ||
}, | ||
"devDependencies": { | ||
"tap": "~0.4.1", | ||
"tape": "~0.3.2" | ||
"tape": "~0.3.3" | ||
}, | ||
"testling": { | ||
"files": "test/index.js", | ||
"files": "test.js", | ||
"browsers": [ | ||
@@ -29,0 +33,0 @@ "iexplore/6.0..latest", |
#object-keys | ||
[![Build Status](https://travis-ci.org/ljharb/object-keys.png)](https://travis-ci.org/ljharb/object-keys) | ||
[![browser support](https://ci.testling.com/ljharb/object-keys.png)](https://ci.testling.com/ljharb/object-keys) | ||
@@ -4,0 +6,0 @@ |
69
shim.js
@@ -1,40 +0,45 @@ | ||
// modified from https://github.com/kriskowal/es5-shim | ||
var has = Object.prototype.hasOwnProperty, | ||
hasDontEnumBug = true, | ||
dontEnums = [ | ||
"toString", | ||
"toLocaleString", | ||
"valueOf", | ||
"hasOwnProperty", | ||
"isPrototypeOf", | ||
"propertyIsEnumerable", | ||
"constructor" | ||
], | ||
dontEnumsLength = dontEnums.length; | ||
for (var key in {"toString": null}) { hasDontEnumBug = false; } | ||
(function () { | ||
"use strict"; | ||
var keysShim = function keys(object) { | ||
if ((typeof object !== "object" && typeof object !== "function") || object === null) { | ||
throw new TypeError("Object.keys called on a non-object"); | ||
} | ||
// modified from https://github.com/kriskowal/es5-shim | ||
var has = Object.prototype.hasOwnProperty, | ||
is = require('is-extended'), | ||
forEach = require('./shim-foreach.js'), | ||
hasDontEnumBug = true, | ||
dontEnums = [ | ||
"toString", | ||
"toLocaleString", | ||
"valueOf", | ||
"hasOwnProperty", | ||
"isPrototypeOf", | ||
"propertyIsEnumerable", | ||
"constructor" | ||
], | ||
key, keysShim; | ||
for (key in {"toString": null}) { hasDontEnumBug = false; } | ||
var keys = []; | ||
for (var name in object) { | ||
if (has.call(object, name)) { | ||
keys.push(name); | ||
keysShim = function keys(object) { | ||
if (!is.isObject(object)) { | ||
throw new TypeError("Object.keys called on a non-object"); | ||
} | ||
} | ||
if (hasDontEnumBug) { | ||
for (var i = 0, ii = dontEnumsLength; i < ii; i++) { | ||
var dontEnum = dontEnums[i]; | ||
if (has.call(dontEnums, dontEnum)) { | ||
keys.push(dontEnum); | ||
var name, theKeys = []; | ||
for (name in object) { | ||
if (has.call(object, name)) { | ||
theKeys.push(name); | ||
} | ||
} | ||
} | ||
return keys; | ||
}; | ||
module.exports = keysShim; | ||
if (hasDontEnumBug) { | ||
forEach(dontEnums, function (dontEnum) { | ||
if (has.call(dontEnums, dontEnum)) { | ||
theKeys.push(dontEnum); | ||
} | ||
}); | ||
} | ||
return theKeys; | ||
}; | ||
module.exports = keysShim; | ||
}()); | ||
34
test.js
var test = require('tape'); // require('tap').test; // tape works in browserify, tap works in node 0.10 | ||
var shimmedKeys = require('./index.js'); | ||
var is = require('is-extended'); | ||
var keys = require('./shim.js'); | ||
var forEach = require('./shim-foreach.js'); | ||
var indexOf = require('indexof'); | ||
test('works', function (t) { | ||
var obj = { | ||
a: true, | ||
b: true, | ||
c: true | ||
}; | ||
var obj = { | ||
"str": "boz", | ||
@@ -23,3 +21,2 @@ "obj": {}, | ||
t.test('exports a function', function (st) { | ||
st.plan(1); | ||
if (Object.keys) { | ||
@@ -30,18 +27,19 @@ st.equal(Object.keys, shimmedKeys, 'Object.keys is supported and exported'); | ||
} | ||
st.end(); | ||
}); | ||
t.test('working with actual shim', function (st) { | ||
st.plan(1); | ||
st.notEqual(Object.keys, keys, 'keys shim is not native Object.keys'); | ||
st.end(); | ||
}); | ||
t.test('works with an object literal', function (st) { | ||
st.plan(2); | ||
var theKeys = keys(obj); | ||
st.equal(Array.isArray(theKeys), true, 'returns an array'); | ||
st.equal(is.isArray(theKeys), true, 'returns an array'); | ||
st.deepEqual(theKeys, objKeys, 'Object has expected keys'); | ||
st.end(); | ||
}); | ||
t.test('returns names which are own properties', function (st) { | ||
keys(obj).forEach(function (name) { | ||
forEach(keys(obj), function (name) { | ||
st.equal(obj.hasOwnProperty(name), true); | ||
@@ -53,8 +51,8 @@ }); | ||
t.test('returns names which are enumerable', function (st) { | ||
var loopedValues = []; | ||
for (var k in obj) { | ||
var k, loopedValues = []; | ||
for (k in obj) { | ||
loopedValues.push(k); | ||
} | ||
keys(obj).forEach(function (name) { | ||
st.notEqual(loopedValues.indexOf(name), -1, name + ' is not enumerable'); | ||
forEach(keys(obj), function (name) { | ||
st.notEqual(indexOf(loopedValues, name), -1, name + ' is not enumerable'); | ||
}); | ||
@@ -65,6 +63,6 @@ st.end(); | ||
t.test('throws an error for a non-object', function (st) { | ||
st.plan(1); | ||
st.throws(function () { | ||
return keys(42); | ||
}, new RangeError(), 'throws on a non-object'); | ||
}, RangeError, 'throws on a non-object'); | ||
st.end(); | ||
}); | ||
@@ -75,3 +73,2 @@ t.end(); | ||
test('works with an object instance', function (t) { | ||
t.plan(2); | ||
var Prototype = function () {}; | ||
@@ -82,5 +79,6 @@ Prototype.prototype.foo = true; | ||
var theKeys = keys(obj); | ||
t.equal(Array.isArray(theKeys), true, 'returns an array'); | ||
t.equal(is.isArray(theKeys), true, 'returns an array'); | ||
t.deepEqual(theKeys, ['bar'], 'Instance has expected keys'); | ||
t.end(); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4964
8
119
28
2
+ Addedindexof@~0.0.1
+ Addedis-extended@~0.0.4
+ Addeddeclare.js@0.0.8(transitive)
+ Addedextended@0.0.6(transitive)
+ Addedextender@0.0.10(transitive)
+ Addedindexof@0.0.1(transitive)
+ Addedis-extended@0.0.10(transitive)