unique-map
v1.0.0
Published
Returns a copy of a Map or Object with duplicate values removed.
Downloads
1
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
module instead.)
Optionally lets you set a numeric limit on total entries in the returned Map/Object.
Installation
npm install unique-map --save
The module exports a single function.
Usage Example
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:
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:
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'