object-keys
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The npm package "object-keys" serves as a replacement for Object.keys method in JavaScript, in cases where Object.keys is not accessible or available. This shim implementation is taken directly from es5-shim, with additional modifications, including some from lodash.
To use object-keys, initially, you will need to install the module using npm with the command npm install object-keys
. After the module is successfully installed, you can require it in your JavaScript file.
Here is a basic usage example:
var keys = require('object-keys');
var assert = require('assert');
var obj = {
a: true,
b: true,
c: true
};
assert.deepEqual(keys(obj), ['a', 'b', 'c']);
In this example, the keys
variable holds the function imported from object-keys
, and it's used to get the keys from the obj
. The assert.deepEqual
function is used to verify that the keys received match the expected ones: a, b, and c.
Also, the package allows two additional usage cases demonstrating its purpose as a shim. If the native Object.keys method is not present or is present, you can use it as follows:
When Object.keys is not present:
var keys = require('object-keys');
var assert = require('assert');
delete Object.keys; // Object.keys is not present
var shimmedKeys = keys.shim();
assert.equal(shimmedKeys, keys);
assert.deepEqual(Object.keys(obj), keys(obj));
When Object.keys is present:
var keys = require('object-keys');
var assert = require('assert');
var shimmedKeys = keys.shim(); // Object.keys is present
assert.equal(shimmedKeys, Object.keys);
assert.deepEqual(Object.keys(obj), keys(obj));
For comprehensive documentation about the object-keys npm package, you can simply visit the GitHub repository linked in the package's readme file: https://github.com/ljharb/object-keys. All usage information, along with the source code and test examples, can be found at this location. Apart from the information found in the readme, you can also look into the source code for more detailed insights.