CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-object-keys

An Object.keys replacement for environments where Object.keys is not available, with comprehensive cross-browser compatibility.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Object Keys

Object 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.

Package Information

  • Package Name: object-keys
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install object-keys

Core Imports

ES 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 everywhere

Basic Usage

const 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");
}

Capabilities

Object Keys Function

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)

Shim Installation

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
}

Error Handling

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']

Cross-Browser Compatibility

Object Keys provides comprehensive compatibility across all JavaScript environments:

Browser Support:

  • Internet Explorer 6-11: Full support with proper enumeration of all properties
  • Safari 5.0+: Includes fix for Safari 5.0 arguments object enumeration bug
  • Firefox 3.0+: Complete compatibility across all versions
  • Chrome 4.0+: Full support including legacy versions
  • Opera 10.0+: Complete functionality
  • Mobile browsers: iOS 6.0+, Android Browser 4.2+
  • All modern browsers: Uses native Object.keys when available and correctly implemented

Runtime Support:

  • Node.js 0.4+: Works across all Node.js versions from 0.4 to latest
  • Legacy environments: Handles for...in enumeration bugs and non-enumerable property issues

Special Browser Bug Fixes:

  • Safari 5.0 Arguments Bug: Automatic detection and correction of arguments object enumeration
  • Internet Explorer DontEnum Bug: Proper handling of shadowed properties like toString, valueOf
  • iOS Mobile Safari: Fixes prototype enumeration issues in functions
  • Window Object Automation: Handles IE automation equality bugs for host objects
  • String Object Indexing: Correct enumeration of string indices across all environments

docs

index.md

tile.json