or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# Object Keys

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: object-keys

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install object-keys`

10

11

## Core Imports

12

13

**ES Modules:**

14

```javascript

15

import objectKeys from "object-keys";

16

```

17

18

**CommonJS:**

19

```javascript

20

const objectKeys = require("object-keys");

21

```

22

23

**Fallback Pattern (Recommended):**

24

```javascript

25

// Use native Object.keys if available, fallback to polyfill

26

const keys = Object.keys || require("object-keys");

27

```

28

29

**Global Shimming:**

30

```javascript

31

// Install globally and use native Object.keys API

32

require("object-keys").shim();

33

// Now Object.keys works everywhere

34

```

35

36

## Basic Usage

37

38

```javascript

39

const objectKeys = require("object-keys");

40

41

// Direct usage as Object.keys replacement

42

const obj = { a: 1, b: 2, c: 3 };

43

const keys = objectKeys(obj);

44

console.log(keys); // ['a', 'b', 'c']

45

46

// Recommended fallback pattern

47

const keys = Object.keys || require("object-keys");

48

const myKeys = keys({ x: 1, y: 2 }); // ['x', 'y']

49

50

// Global shimming approach

51

objectKeys.shim(); // Install Object.keys globally if needed

52

Object.keys(obj); // Now works reliably in all environments

53

54

// Conditional usage based on environment

55

if (!Object.keys) {

56

Object.keys = require("object-keys");

57

}

58

```

59

60

## Capabilities

61

62

### Object Keys Function

63

64

Extracts enumerable property names from an object, providing `Object.keys()` functionality with cross-browser compatibility.

65

66

```javascript { .api }

67

/**

68

* Get enumerable property names from an object

69

* @param {object|function|arguments} obj - The object to get keys from

70

* @returns {string[]} Array of enumerable property names

71

* @throws {TypeError} When called on primitive values (null, undefined, number, string, boolean)

72

*/

73

function objectKeys(obj);

74

```

75

76

**Usage Examples:**

77

78

```javascript

79

const objectKeys = require("object-keys");

80

81

// Basic object

82

const user = { name: "Alice", age: 30, active: true };

83

const keys = objectKeys(user);

84

// Result: ['name', 'age', 'active']

85

86

// Array (returns indices)

87

const arr = ['a', 'b', 'c'];

88

const indices = objectKeys(arr);

89

// Result: ['0', '1', '2']

90

91

// String object

92

const str = new String("hello");

93

const strKeys = objectKeys(str);

94

// Result: ['0', '1', '2', '3', '4']

95

96

// Arguments object

97

function example() {

98

return objectKeys(arguments);

99

}

100

const argKeys = example('a', 'b');

101

// Result: ['0', '1']

102

103

// Function objects (with custom properties)

104

function myFunc() {}

105

myFunc.customProp = true;

106

const funcKeys = objectKeys(myFunc);

107

// Result: ['customProp']

108

109

// Object instances (only own enumerable properties)

110

function Constructor() {

111

this.instanceProp = true;

112

}

113

Constructor.prototype.prototypeProp = true;

114

const instance = new Constructor();

115

const instanceKeys = objectKeys(instance);

116

// Result: ['instanceProp'] (prototype properties excluded)

117

```

118

119

### Shim Installation

120

121

Installs the `Object.keys` shim globally if needed, handling browser-specific compatibility issues.

122

123

```javascript { .api }

124

/**

125

* Install Object.keys shim globally if needed

126

* @returns {Function} The shimmed Object.keys function or original if already present

127

*/

128

objectKeys.shim();

129

```

130

131

**Usage Examples:**

132

133

```javascript

134

const objectKeys = require("object-keys");

135

136

// Install shim (safe to call multiple times)

137

const shimmedKeys = objectKeys.shim();

138

139

// Now Object.keys is available globally

140

const obj = { x: 1, y: 2 };

141

const keys = Object.keys(obj); // Works in all environments

142

143

// The shim handles Safari 5.0 arguments bug automatically

144

function testArguments() {

145

return Object.keys(arguments); // Fixed to work correctly

146

}

147

```

148

149

## Error Handling

150

151

The library throws `TypeError` when attempting to get keys from invalid values:

152

153

```javascript

154

// These will throw TypeError:

155

objectKeys(null); // TypeError: Object.keys called on a non-object

156

objectKeys(undefined); // TypeError: Object.keys called on a non-object

157

objectKeys(42); // TypeError: Object.keys called on a non-object

158

objectKeys("hello"); // TypeError: Object.keys called on a non-object

159

objectKeys(true); // TypeError: Object.keys called on a non-object

160

objectKeys(false); // TypeError: Object.keys called on a non-object

161

162

// These work fine:

163

objectKeys({}); // []

164

objectKeys([]); // []

165

objectKeys(function(){}); // [] or ['prototype'] depending on environment

166

objectKeys(arguments); // ['0', '1', ...] based on arguments

167

objectKeys(new String("test")); // ['0', '1', '2', '3']

168

objectKeys(Object("hello")); // ['0', '1', '2', '3', '4']

169

```

170

171

## Cross-Browser Compatibility

172

173

Object Keys provides comprehensive compatibility across all JavaScript environments:

174

175

**Browser Support:**

176

- **Internet Explorer 6-11**: Full support with proper enumeration of all properties

177

- **Safari 5.0+**: Includes fix for Safari 5.0 arguments object enumeration bug

178

- **Firefox 3.0+**: Complete compatibility across all versions

179

- **Chrome 4.0+**: Full support including legacy versions

180

- **Opera 10.0+**: Complete functionality

181

- **Mobile browsers**: iOS 6.0+, Android Browser 4.2+

182

- **All modern browsers**: Uses native `Object.keys` when available and correctly implemented

183

184

**Runtime Support:**

185

- **Node.js 0.4+**: Works across all Node.js versions from 0.4 to latest

186

- **Legacy environments**: Handles `for...in` enumeration bugs and non-enumerable property issues

187

188

**Special Browser Bug Fixes:**

189

- **Safari 5.0 Arguments Bug**: Automatic detection and correction of arguments object enumeration

190

- **Internet Explorer DontEnum Bug**: Proper handling of shadowed properties like `toString`, `valueOf`

191

- **iOS Mobile Safari**: Fixes prototype enumeration issues in functions

192

- **Window Object Automation**: Handles IE automation equality bugs for host objects

193

- **String Object Indexing**: Correct enumeration of string indices across all environments