@munierujp/super-set
v1.0.0
Published
SuperSet class extends Set and add some methods, such as higher-order function.
Downloads
7
Readme
SuperSet.js
SuperSet class extends Set and add some methods, such as higher-order function.
Installation
$ npm i @munierujp/super-set
or
$ yarn add @munierujp/super-set
Use in your script
const SuperSet = require('@munierujp/super-set')
Usage
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(matcher)
const set = SuperSet.of('foo', 'bar')
const filteredSet = set.filter(v => v.startsWith('f'))
console.log(filteredSet) // { 'foo' }
SuperSet.map(mapper)
const set = SuperSet.of('foo', 'bar')
const mappedSet = set.map(v => v + '_')
console.log(mappedSet) // { 'foo_', 'bar_' }
Terminal operation
SuperSet.find(matcher)
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(matcher)
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(matcher)
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' }