@js-commons/super-set
v1.0.1
Published
SuperSet class extends Set and add some methods, such as higher-order function.
Downloads
7
Readme
SuperSet
SuperSet class extends Set and add some methods, such as higher-order function.
Installation
$ npm i @js-commons/super-set
or
$ yarn add @js-commons/super-set
Use in your script
const SuperSet = require('@js-commons/super-set')
or
import SuperSet from '@js-commons/super-set'
API
You can use Set's self methods and below additional methods.
Creation
SuperSet.empty()
const set = SuperSet.empty()
console.log(set) // {}
SuperSet.of(...values)
const set = SuperSet.of('foo', 'bar')
console.log(set) // { 'foo', 'bar' }
SuperSet.from(iterable)
const set = new Set(['foo', 'bar'])
const superSet = SuperSet.from(set)
console.log(superSet) // { 'foo', 'bar' }
Intermediate operation
SuperSet.peek(callback)
const set = SuperSet.of('foo', 'bar')
const tmpArray = []
const peekedSet = set.peek(v => tmpArray.push(v))
console.log(peekedSet) // { 'foo', 'bar' }
console.log(tmpArray) // [ 'foo', 'bar' ]
SuperSet.peek(callback, thisArg)
const set = SuperSet.of('foo', 'bar')
const thisArg = 'baz'
const tmpArray = []
const peekedSet = set.peek(function (v) {
tmpArray.push(v + this)
}, thisArg)
console.log(peekedSet) // { 'foo', 'bar' }
console.log(tmpArray) // [ 'foobaz', 'barbaz' ]
SuperSet.filter(callback)
const set = SuperSet.of('foo', 'bar')
const filteredSet = set.filter(v => v.startsWith('f'))
console.log(filteredSet) // { 'foo' }
SuperSet.map(callback)
const set = SuperSet.of('foo', 'bar')
const mappedSet = set.map(v => v + '_')
console.log(mappedSet) // { 'foo_', 'bar_' }
Terminal operation
SuperSet.find(callback)
const set = SuperSet.of('foo', 'bar')
const value = set.find(v => v.startsWith('f'))
console.log(value) // foo
onst set = SuperSet.empty()
const value = set.find(v => v.startsWith('f'))
console.log(value) // undefined
SuperSet.some(callback)
const set = SuperSet.of('foo', 'bar')
console.log(set.some(v => v.startsWith('f'))) // true
const set = SuperSet.empty()
console.log(set.some(v => v.startsWith('f'))) // false
SuperSet.every(callback)
const set = SuperSet.of('foo', 'bar')
console.log(set.every(v => v.length === 3)) // true
const set = SuperSet.of('foo', 'bar')
console.log(set.every(v => v.startsWith('f'))) // false
SuperSet.isEmpty()
const set = SuperSet.of('foo', 'bar')
console.log(set.isEmpty()) // false
const set = SuperSet.empty()
console.log(set.isEmpty()) // true
SuperSet.isNotEmpty()
const set = SuperSet.of('foo', 'bar')
console.log(set.isNotEmpty()) // true
const set = SuperSet.empty()
console.log(set.isNotEmpty()) // false
SuperSet.toArray()
const set = SuperSet.of('foo', 'bar')
const values = set.toArray()
console.log(values) // [ 'foo', 'bar' ]
SuperSet.toSet()
const superSet = SuperSet.of('foo', 'bar')
const set = superSet.toSet()
console.log(set) // { 'foo', 'bar' }