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

v1.6.0

Published

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

Downloads

159

Readme

entries-iterator

Returns an iterator 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-iterator

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 items will be iterated as [0, [1, 2]] and [1, [3, 4]]). But if detectPairs is true, the module will interpret the first item in each pair as the entry key (the items will be iterated as [1, 2] and [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 iterated in reverse order. Defaults to false.

Return Value

An iterator which yields two-element key-value pair arrays.

Examples

Arrays

const entries = require('entries-iterator')

const i = entries(['a', 'b'])
i.next().value // [0, 'a']
i.next().value // [1, 'b']
i.next().done // true

// Supports the bind operator
['a', 'b']::entries()

Examples using an array of entries (key/value pairs):

const entries = require('entries-iterator')

const arr = [['key1', 'val1'], ['key2', 'val2']]

// Default behavior without the `detectPairs` option
const i1 = entries(arr)
i1.next().value // [0, ['key1', 'val1']]
i1.next().value // [1, ['key2', 'val2']]
i1.next().done // true

// With `detectPairs` set to `true`
const i2 = entries(arr, {detectPairs: true})
i2.next().value // ['key1', 'val1']
i2.next().value // ['key2', 'val2']
i2.next().done // true

Iterators

entries-iterator 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-iterator')

function * gen () {
  yield 'a'
  yield 'b'
}

const i = entries(gen())
i.next().value // [0, 'a']
i.next().value // [1, 'b']
i.next().done // true

Maps

const entries = require('entries-iterator')

const map = new Map()
map.set('key', 'value')

const i = entries(map)
i.next().value // ['key', 'value']
i.next().done // true

Objects

When we say “Object” here, we mean an Object that does not fall under another recognized category (like Array or Map).

const entries = require('entries-iterator')

const i = entries({key: 'value'})
i.next().value // ['key', 'value']
i.next().done // true

// Supports the bind operator
const obj = {key: 'value'}
obj::entries()

Inherited Object Properties

Include Object properties from the prototype chain by setting inObj to true:

const entries = require('entries-iterator')

function Cls () {}
Cls.prototype.key = 'value'

const i = entries(new Cls(), {inObj: true})
i.next().value // ['key', 'value']
i.next().done // true

Non-Enumerable Object Properties

Include non-enumerable Object properties by setting reflectObj to true:

const entries = require('entries-iterator')

const obj = {}
Object.defineProperty(obj, 'key', {value: 'value', enumerable: false})

const i = entries(obj, {reflectObj: true})
i.next().value // ['key', 'value']
i.next().done // true

Sets

entries-iterator 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-iterator')

const set = new Set()
set.add('first')
set.add('second')

const i = entries(set)
i.next().value // [0, 'first']
i.next().value // [1, 'second']
i.next().done // true

Strings

entries-iterator will treat a string like a character array.

const entries = require('entries-iterator')

const i = entries('hi')
i.next().value // [0, 'h']
i.next().value // [1, 'i']
i.next().done // true

Typed Arrays

const entries = require('entries-iterator')

const typedArray = new Int32Array(new ArrayBuffer(4))

const i = entries(typedArray)
i.next().value // [0, 0]
i.next().done // true

Related