npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

entries-array

v1.2.0

Published

Returns an array of the key-value pairs of an Object, Map, Array, or Typed Array.

Downloads

11

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

  1. Bindable: c (Array, iterator, Object, Map, Set, string, or Typed Array)
  2. 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 to Array/Map/Set (respectively).
    • detectPairs (boolean): This option only has an effect when c is an Array of two-item pairs, such as [[1, 2], [3, 4]]. When detectPairs is set to false (the default behavior), the Array indexes will be used as the keys (the returned array will be [[0, [1, 2]], [1, [3, 4]]]). But if detectPairs is true, 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 if c is an Object (i.e. not another recognized type). Defaults to false.
    • reflectObj (boolean): Whether or not to include non-enumerable Object properties by using reflection. Only takes effect if c is an Object (i.e. not another recognized type). Defaults to false.
    • reverse (boolean): If true, then entries are returned in reverse order. Defaults to false.

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

Related