values-array

Returns an array of the values of an Array, Iterator, Object, Map, Set, or Typed Array. Useful for when you need the values of a collection object but aren’t sure what type of collection you’ll be given.

Usage no npm install needed!

<script type="module">
  import valuesArray from 'https://cdn.skypack.dev/values-array';
</script>

README

values-array

Returns an array of the values of an Array, Iterator, Object, Map, Set, or Typed Array. Useful for when you need the values of a collection object but aren’t sure what type of collection you’ll be given.

Installation

Requires Node.js 6.0.0 or above.

npm i values-array

API

The module exports a single function.

Parameter

Bindable: c (Array, Iterator, Object, Map, Set, or Typed Array)

Return Value

An array of values from the collection.

Examples

Arrays

const values = require('values-array')

values(['a', 'b']) // ['a', 'b']

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

Iterators

const values = require('values-array')

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

values(gen()) // ['a', 'b']

Maps

const values = require('values-array')

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

values(map) // ['value']

Objects

const values = require('values-array')

values({key: 'value'}) // ['value']

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

Sets

const values = require('values-array')

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

values(set) // ['first', 'second']

Typed Arrays

const values = require('values-array')

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

values(typedArray) // [0]

Related