@cahmoraes93/extended-set
v1.2.0
Published
This package create a Set with extended functionalities
Downloads
90
Readme
Extended Set
Extended Set is a Set Data structure with extended functionalities. It implements map, reduce and filter functions. In addiction, provides methods to handle Set basic operations.
map
Map a new Set with callback function.
property
- callback: (originalSet) => mappedSet
example
const set = new ExtendedSet<number>()
set.add(2).add(3).add(10)
const result = set.map((item) => item * 2)
console.log(result) // Set {4, 6, 20}
filter
Filter a new Set with callback function.
property
- callback: (originalSet) => filteredSet
example:
const set = new ExtendedSet<number>([1, 2, 3, 4, 5])
const evenSet = set.filter((item) => item % 2 === 0)
console.log(evenSet) // Set {2, 4}
reduce
Create a new Set based in a reducer function.
property
- callback: (originalSet) => reducedValue
example
const set = new ExtendedSet<number>()
set.add(2).add(3).add(0).add(5).add(20)
const smallestNumber = set.reduce((acc, item) => (acc > item ? item : acc))
console.log(smallestNumber) // 0
find
Find an item in Set. Returns the item or null.
property
- callback: (item: Type) => boolean
example
const users = [{ name: 'John' }, { name: 'George' }, { name: 'Jackie' }]
const set = ExtendedSet.of(users)
const findGeorge = set.find((item) => item.name === 'George')
console.log(findGeorge) // { name: 'George' }
const findAnnie = set.find((item) => item.name === 'Annie')
console.log(findAnnie) // null
every
Return true when every element in Set is truthy, else return false
property
- callback: (item: Type) => boolean
example
const users = [1, 2, 3]
const set = ExtendedSet.of(users)
const numbersAreGreaterThanZero = set.every((number) => number > 0)
console.log(numbersAreGreaterTahZero) // true
const numbersAreLessThanZero = set.every((number) => number < 0)
console.log(numbersAreLessThanZero) // false
some
Return true when some element in Set is truthy, else return false
property
- callback: (item: Type) => boolean
example
const numbers = [1, -2, 3, 4]
const set = ExtendedSet.of(numbers)
const isThereNumberLessThanZero = set.some((item) => item < 0)
console.log(isThereNumberLessThanZero) // true
const isThereNumberEqualsZero = set.some((item) => item === 0)
console.log(isThereNumberEqualsZero) // false
of
Create a new Set from an array
property
- anArray = []
// Array to convert into Set
example
const set = ExtendedSet.of([1, 2, 3])
console.log(set.toString()) // 1,2,3
toArray
Create a new Array from Set.
example:
const set = new ExtendedSet<number>([1, 2, 3, 4, 5])
const numbers = set.toArray()
console.log(numbers) // [1, 2, 3, 4, 5]
isSuperSetOf
Check if Set is a superset of other set. Returns true if Set is a superset of other set. Else returns false.
property
- other: Set
example
const set_1 = new ExtendedSet<number>([1, 2, 3, 4])
const set_2 = new ExtendedSet<number>([1, 2])
const result = set_1.union(set_2)
console.log(result) // true
isSubSetOf
Check if Set is a subset of other set. Returns true if Set is a subset of other set. Else returns false.
property
- other: Set
example
const set_1 = new ExtendedSet<number>([1, 2])
const set_2 = new ExtendedSet<number>([1, 2, 3, 4])
const result = set_1.isSubSetOf(set_2)
console.log(result) // true
union
Create new Set from an union of Sets.
property
- others: Set[]
example
const set_1 = new ExtendedSet<number>([1, 2, 3, 4])
const set_2 = new ExtendedSet<number>([1, 5])
const set_3 = new ExtendedSet<number>([6, 2])
const result = set_1.union(set_2, set_3)
console.log(result) // Set {1, 2, 3, 4, 5, 6}
intersection
Create an intersection Set from Sets.
property
- other: Set
example
const set_1 = new ExtendedSet<number>([1, 2, 3, 4])
const set_2 = new ExtendedSet<number>([1, 4])
const result = set_1.intersection(set_2)
console.log(result) // Set {1, 4}
difference
Create an intersection Difference from Sets.
property
- other: Set
example
const set_1 = new ExtendedSet<number>([1, 2, 3, 4])
const set_2 = new ExtendedSet<number>([1, 4])
const result = set_2.difference(set_1)
console.log(result) // Set {2, 3}