https://github.com/lamansky/construct-map
[Node.js] Turns entries into a Map or other key-value collection.
https://github.com/lamansky/construct-map
collection construct entries javascript map node node-module object
Last synced: about 2 months ago
JSON representation
[Node.js] Turns entries into a Map or other key-value collection.
- Host: GitHub
- URL: https://github.com/lamansky/construct-map
- Owner: lamansky
- License: mit
- Created: 2018-02-20T07:31:13.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-07T09:11:59.000Z (about 7 years ago)
- Last Synced: 2025-03-05T11:07:08.443Z (3 months ago)
- Topics: collection, construct, entries, javascript, map, node, node-module, object
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# construct-map
Turns entries into a Map or other key-value collection.
Useful when you’re not sure ahead of time what type of collection it’ll be.
## Installation
Requires [Node.js](https://nodejs.org/) 6.0.0 or above.
```bash
npm i construct-map
```## API
The module exports a single function.
### Parameters
1. `Cls` (object, function, or string): The class to construct, an instance of the desired class, or the global string name of the class. The class to which this argument refers can be just about any built-in collection (`Array`, `Map`, `Object`, `Set`, Typed Arrays, `WeakMap`, `WeakSet`), or any class that accepts an `entries` iterable as the first argument of its constructor.
2. `entries` (iterable): A collection of key-value pairs. (If constructing an `Array`, `Set`, `WeakSet`, or Typed Array, the keys will be ignored.)### Return Value
A `Cls` object constructed with `entries`.
## Example
```javascript
const constructMap = require('construct-map')const map = constructMap(Map, [['key', 'value']])
map.get('key') // 'value'constructMap(Object, [['key', 'value']]) // {key: 'value'}
// The module is particularly useful for reassembling a collection
// after performing some operation on its entries.
const doSomething = x => x
const obj = {key: 'value'}
constructMap(obj, doSomething(Object.entries(obj))) // {key: 'value'}
```