@js-commons/super-map
v1.1.2
Published
SuperMap class extends Map and add some methods, such as higher-order function.
Downloads
5
Readme
SuperMap
SuperMap class extends Map and add some methods, such as higher-order function.
Installation
$ npm i @js-commons/super-map
or
$ yarn add @js-commons/super-map
Use in your script
const SuperMap = require('@js-commons/super-map')
or
import SuperMap from '@js-commons/super-map'
Definition
Entry
Entry is expressed as array which has two elements(0:key, 1:value).
const entry1 = ['foo', 'Foo']
const entry2 = ['bar', 'Bar']
const map = SuperMap.of(entry1, entry2)
console.log(map) // {"foo" => "Foo", "bar" => "Bar"}
API
You can use Map's self methods and below additional methods.
Creation
SuperMap.empty()
const map = SuperMap.empty()
console.log(map) // {}
SuperMap.of(...entries)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map) // { 'foo' => 'Foo', 'bar' => 'Bar' }
SuperMap.from(iterable)
const map = new Map([['foo', 'Foo'], ['bar', 'Bar']])
const superMap = SuperMap.from(map)
console.log(superMap) // { 'foo' => 'Foo', 'bar' => 'Bar' }
Intermediate operation
SuperMap.peek(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const tmpMap = new Map()
const peekedMap = map.peek((v, k) => tmpMap.set(k, v))
console.log(peekedMap) // { 'foo' => 'Foo', 'bar' => 'Bar' }
console.log(tmpMap) // { 'foo' => 'Foo', 'bar' => 'Bar' }
SuperMap.peek(callback, thisArg)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const thisArg = 'baz'
const tmpMap = new Map()
const peekedMap = map.peek(function (v, k) {
tmpMap.set(k, v + this)
}, thisArg)
console.log(peekedMap) // { 'foo' => 'Foo', 'bar' => 'Bar' }
console.log(tmpMap) // { 'foo' => 'Foobaz', 'bar' => 'Barbaz' }
SuperMap.filter(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const filteredMap = map.filter((v, k) => k.startsWith('f'))
console.log(filteredMap) // { 'foo' => 'Foo' }
SuperMap.map(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const mappedMap = map.map((v, k) => [k + '_', v + '-'])
console.log(mappedMap) // { 'foo_' => 'Foo-', 'bar_' => 'Bar-' }
SuperMap.mapEntry(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const mappedMap = map.mapEntry((v, k) => [k + '_', v + '-'])
console.log(mappedMap) // { 'foo_' => 'Foo-', 'bar_' => 'Bar-' }
SuperMap.mapKey(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const mappedMap = map.mapKey((v, k) => k + '_')
console.log(mappedMap) // { 'foo_' => 'Foo', 'bar_' => 'Bar' }
SuperMap.mapValue(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const mappedMap = map.mapValue((v, k) => v + '-')
console.log(mappedMap) // { 'foo' => 'Foo-', 'bar' => 'Bar-' }
Terminal operation
SuperMap.find(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const entry = map.find((v, k) => k.startsWith('f'))
console.log(entry) // [ 'foo', 'Foo' ]
const map = SuperMap.empty()
const entry = map.find((v, k) => k.startsWith('f'))
console.log(entry) // undefined
SuperMap.findEntry(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const entry = map.findEntry((v, k) => k.startsWith('f'))
console.log(entry) // [ 'foo', 'Foo' ]
const map = SuperMap.empty()
const entry = map.findEntry((v, k) => k.startsWith('f'))
console.log(entry) // undefined
SuperMap.findKey(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const key = map.findKey((v, k) => k.startsWith('f'))
console.log(key) // foo
const map = SuperMap.empty()
const key = map.findKey((v, k) => k.startsWith('f'))
console.log(key) // undefined
SuperMap.findValue(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const value = map.findValue((v, k) => k.startsWith('f'))
console.log(value) // Foo
const map = SuperMap.empty()
const value = map.findValue((v, k) => k.startsWith('f'))
console.log(value) // undefined
SuperMap.hasEntry(entry)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.hasEntry(['foo', 'Foo'])) // true
const map = SuperMap.empty()
console.log(map.hasEntry(['foo', 'Foo'])) // false
SuperMap.hasKey(key)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.hasKey('foo')) // true
const map = SuperMap.empty()
console.log(map.hasKey('foo')) // false
SuperMap.hasValue(value)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.hasValue('Foo')) // true
const map = SuperMap.empty()
console.log(map.hasValue('Foo')) // false
SuperMap.some(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.some((v, k) => k.startsWith('f'))) // true
const map = SuperMap.empty()
console.log(map.some((v, k) => k.startsWith('f'))) // false
SuperMap.every(callback)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.every((v, k) => k.length === 3)) // true
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.every((v, k) => k.startsWith('f'))) // false
SuperMap.isEmpty()
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.isEmpty()) // false
const map = SuperMap.empty()
console.log(map.isEmpty()) // true
SuperMap.isNotEmpty()
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
console.log(map.isNotEmpty()) // true
const map = SuperMap.empty()
console.log(map.isNotEmpty()) // false
SuperMap.toEntries()
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const entries = map.toEntries()
console.log(entries) // [ [ 'foo', 'Foo' ], [ 'bar', 'Bar' ] ]
SuperMap.toKeys()
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const keys = map.toKeys()
console.log(keys) // [ 'foo', 'bar' ]
SuperMap.toValues()
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const values = map.toValues()
console.log(values) // [ 'Foo', 'Bar' ]
SuperMap.toMap()
const superMap = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const map = superMap.toMap()
console.log(map) // { 'foo' => 'Foo', 'bar' => 'Bar' }