entries-array
v1.2.0
Published
Returns an array of the key-value pairs of an Object, Map, Array, or Typed Array.
Downloads
11
Maintainers
Readme
entries-array
Returns an array of the key-value pairs of an Object, Map, Array, or Typed Array. Useful for when you need the entries of a collection object but aren’t sure what type of collection you’ll be given.
Installation
Requires Node.js 7.0.0 or above.
npm i entries-array
API
The module exports a single function.
Parameters
- Bindable:
c
(Array, iterator, Object, Map, Set, string, or Typed Array) - Optional: Object argument:
arrays
/maps
/sets
(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray
/Map
/Set
(respectively).detectPairs
(boolean): This option only has an effect whenc
is an Array of two-item pairs, such as[[1, 2], [3, 4]]
. WhendetectPairs
is set tofalse
(the default behavior), the Array indexes will be used as the keys (the returned array will be[[0, [1, 2]], [1, [3, 4]]]
). But ifdetectPairs
istrue
, the module will interpret the first item in each pair as the entry key (a copy of the original array will be returned:[[1, 2], [3, 4]]
).inObj
(boolean): Whether or not to act like the “in” operator by including inherited Object properties. Only takes effect ifc
is an Object (i.e. not another recognized type). Defaults tofalse
.reflectObj
(boolean): Whether or not to include non-enumerable Object properties by using reflection. Only takes effect ifc
is an Object (i.e. not another recognized type). Defaults tofalse
.reverse
(boolean): Iftrue
, then entries are returned in reverse order. Defaults tofalse
.
Return Value
An array of two-element key-value pair arrays.
Examples
Arrays
const entries = require('entries-array')
entries(['a', 'b']) // [[0, 'a'], [1, 'b']]
// Supports the bind operator
['a', 'b']::entries() // [[0, 'a'], [1, 'b']]
Examples using an array of entries (key/value pairs):
const entries = require('entries-array')
const arr = [['key1', 'val1'], ['key2', 'val2']]
// Default behavior without the `detectPairs` option
entries(arr) // [[0, ['key1', 'val1']], [1, ['key2', 'val2']]]
// With `detectPairs` set to `true`
entries(arr, {detectPairs: true}) // [['key1', 'val1'], ['key2', 'val2']]
Objects
const entries = require('entries-array')
entries({key: 'value'}) // [['key', 'value']]
// Supports the bind operator
const obj = {key: 'value'}
obj::entries() // [['key', 'value']]
Inherited Object Properties
Include Object properties from the prototype chain by setting inObj
to true
:
const entries = require('entries-array')
function Cls () {}
Cls.prototype.key = 'value'
entries(new Cls(), {inObj: true}) // [['key', 'value']]
Non-Enumerable Object Properties
Include non-enumerable Object properties by setting reflectObj
to true
:
const entries = require('entries-array')
const obj = {}
Object.defineProperty(obj, 'key', {value: 'value', enumerable: false})
entries(obj, {reflectObj: true}) // [['key', 'value']]
Iterators
entries-array
will treat an iterator as if it were an array of values. Each “key” will be an incrementing integer index that starts at zero.
const entries = require('entries-array')
function * gen () {
yield 'a'
yield 'b'
}
entries(gen()) // [[0, 'a'], [1, 'b']]
Maps
const entries = require('entries-array')
const map = new Map()
map.set('key', 'value')
entries(map) // [['key', 'value']]
Sets
entries-array
will treat a Set like an array, and will add integer index keys starting at zero. Note that this behavior is different from that of the built-in Set.prototype.entries()
method.
const entries = require('entries-array')
const set = new Set()
set.add('first')
set.add('second')
entries(set) // [[0, 'first'], [1, 'second']]
Strings
entries-array
will treat a string like a character array.
const entries = require('entries-array')
entries('hi') // [[0, 'h'], [1, 'i']]
Typed Arrays
const entries = require('entries-array')
const typedArray = new Int32Array(new ArrayBuffer(4))
entries(typedArray) // [[0, 0]]