https://github.com/lamansky/unique-map
[Node.js] Returns a copy of a Map or Object with duplicate values removed.
https://github.com/lamansky/unique-map
Last synced: about 2 months ago
JSON representation
[Node.js] Returns a copy of a Map or Object with duplicate values removed.
- Host: GitHub
- URL: https://github.com/lamansky/unique-map
- Owner: lamansky
- License: mit
- Created: 2017-12-20T14:43:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-20T15:14:26.000Z (over 8 years ago)
- Last Synced: 2025-08-09T18:14:02.025Z (11 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# unique-map
Returns a copy of a Map or Object with duplicate values removed.
This module only tests values, because it’s impossible for a Map or Object to have a duplicate key. (If you want to use Map/Object keys as a basis for testing uniqueness, use the [`unique-map-by`](https://github.com/lamansky/unique-map-by) module instead.)
Optionally lets you set a numeric limit on total entries in the returned Map/Object.
## Installation
```bash
npm install unique-map --save
```
The module exports a single function.
## Usage Example
```javascript
const uniqueMap = require('unique-map')
const map = new Map([
[1, 'A'],
[2, 'A'],
[3, 'B'],
])
const u = uniqueMap(map)
u.size // 2
u.get(1) // 'A'
u.get(2) // undefined
u.get(3) // 'B'
```
You can also use the `limit` argument to cap the number of total entries returned:
```javascript
const u = uniqueMap(map, {limit: 1})
u.size // 1
u.get(1) // 'A'
u.get(2) // undefined
u.get(3) // undefined
```
The module also works just as well with Objects:
```javascript
const uniqueMap = require('unique-map')
const obj = {
1: 'A',
2: 'A',
3: 'B',
}
const u = uniqueMap(obj)
u[1] // 'A'
u[2] // undefined
u[3] // 'B'
```
## Related Projects
* [deduplicate](https://github.com/lamansky/deduplicate)
* [unique-array-by](https://github.com/lamansky/unique-array-by)
* [unique-iterable](https://github.com/lamansky/unique-iterable)
* [unique-iterable-by](https://github.com/lamansky/unique-iterable-by)
* [unique-map-by](https://github.com/lamansky/unique-map-by)