@munierujp/super-map
v1.1.1
Published
SuperMap class extends Map and add some methods, such as higher-order function.
Downloads
6
Readme
SuperMap.js
SuperMap class extends Map and add some methods, such as higher-order function.
Installation
$ npm i @munierujp/super-map
or
$ yarn add @munierujp/super-map
Use in your script
const SuperMap = require('@munierujp/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"}
Usage
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(matcher)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const filteredMap = map.filter((v, k) => k.startsWith('f'))
console.log(filteredMap) // { 'foo' => 'Foo' }
SuperMap.map(mapper)
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(mapper)
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(mapper)
const map = SuperMap.of(['foo', 'Foo'], ['bar', 'Bar'])
const mappedMap = map.mapKey((v, k) => k + '_')
console.log(mappedMap) // { 'foo_' => 'Foo', 'bar_' => 'Bar' }
SuperMap.mapValue(mapper)
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(matcher)
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(matcher)
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(matcher)
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(matcher)
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(matcher)
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(matcher)
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' }