An Object.keys replacement for environments where Object.keys is not available, with comprehensive cross-browser compatibility.
npx @tessl/cli install tessl/npm-object-keys@1.1.0Object Keys provides a polyfill/shim for Object.keys() functionality in JavaScript environments where it may not be available, particularly older browsers and Node.js versions. It offers both a direct replacement function and a shimming capability that can add Object.keys to the global Object prototype if missing.
npm install object-keysES Modules:
import objectKeys from "object-keys";CommonJS:
const objectKeys = require("object-keys");Fallback Pattern (Recommended):
// Use native Object.keys if available, fallback to polyfill
const keys = Object.keys || require("object-keys");Global Shimming:
// Install globally and use native Object.keys API
require("object-keys").shim();
// Now Object.keys works everywhereconst objectKeys = require("object-keys");
// Direct usage as Object.keys replacement
const obj = { a: 1, b: 2, c: 3 };
const keys = objectKeys(obj);
console.log(keys); // ['a', 'b', 'c']
// Recommended fallback pattern
const keys = Object.keys || require("object-keys");
const myKeys = keys({ x: 1, y: 2 }); // ['x', 'y']
// Global shimming approach
objectKeys.shim(); // Install Object.keys globally if needed
Object.keys(obj); // Now works reliably in all environments
// Conditional usage based on environment
if (!Object.keys) {
Object.keys = require("object-keys");
}Extracts enumerable property names from an object, providing Object.keys() functionality with cross-browser compatibility.
/**
* Get enumerable property names from an object
* @param {object|function|arguments} obj - The object to get keys from
* @returns {string[]} Array of enumerable property names
* @throws {TypeError} When called on primitive values (null, undefined, number, string, boolean)
*/
function objectKeys(obj);Usage Examples:
const objectKeys = require("object-keys");
// Basic object
const user = { name: "Alice", age: 30, active: true };
const keys = objectKeys(user);
// Result: ['name', 'age', 'active']
// Array (returns indices)
const arr = ['a', 'b', 'c'];
const indices = objectKeys(arr);
// Result: ['0', '1', '2']
// String object
const str = new String("hello");
const strKeys = objectKeys(str);
// Result: ['0', '1', '2', '3', '4']
// Arguments object
function example() {
return objectKeys(arguments);
}
const argKeys = example('a', 'b');
// Result: ['0', '1']
// Function objects (with custom properties)
function myFunc() {}
myFunc.customProp = true;
const funcKeys = objectKeys(myFunc);
// Result: ['customProp']
// Object instances (only own enumerable properties)
function Constructor() {
this.instanceProp = true;
}
Constructor.prototype.prototypeProp = true;
const instance = new Constructor();
const instanceKeys = objectKeys(instance);
// Result: ['instanceProp'] (prototype properties excluded)Installs the Object.keys shim globally if needed, handling browser-specific compatibility issues.
/**
* Install Object.keys shim globally if needed
* @returns {Function} The shimmed Object.keys function or original if already present
*/
objectKeys.shim();Usage Examples:
const objectKeys = require("object-keys");
// Install shim (safe to call multiple times)
const shimmedKeys = objectKeys.shim();
// Now Object.keys is available globally
const obj = { x: 1, y: 2 };
const keys = Object.keys(obj); // Works in all environments
// The shim handles Safari 5.0 arguments bug automatically
function testArguments() {
return Object.keys(arguments); // Fixed to work correctly
}The library throws TypeError when attempting to get keys from invalid values:
// These will throw TypeError:
objectKeys(null); // TypeError: Object.keys called on a non-object
objectKeys(undefined); // TypeError: Object.keys called on a non-object
objectKeys(42); // TypeError: Object.keys called on a non-object
objectKeys("hello"); // TypeError: Object.keys called on a non-object
objectKeys(true); // TypeError: Object.keys called on a non-object
objectKeys(false); // TypeError: Object.keys called on a non-object
// These work fine:
objectKeys({}); // []
objectKeys([]); // []
objectKeys(function(){}); // [] or ['prototype'] depending on environment
objectKeys(arguments); // ['0', '1', ...] based on arguments
objectKeys(new String("test")); // ['0', '1', '2', '3']
objectKeys(Object("hello")); // ['0', '1', '2', '3', '4']Object Keys provides comprehensive compatibility across all JavaScript environments:
Browser Support:
Object.keys when available and correctly implementedRuntime Support:
for...in enumeration bugs and non-enumerable property issuesSpecial Browser Bug Fixes:
toString, valueOf