@eklabdev/superset
v1.0.2
Published
Enhanced Set and Map implementations with additional utility methods
Maintainers
Readme
@eklabdev/superset
Enhanced Set and Map implementations with additional utility methods for advanced set operations and functional programming patterns.
Installation
npm install @eklabdev/supersetFeatures
- Full TypeScript support with type safety
- Extends native Set and Map with additional utility methods
- Immutable operations that return new instances
- Functional programming patterns (map, filter, reduce)
- Advanced set operations (union, intersection, subtraction)
- Comprehensive type definitions
- Zero dependencies
- ES2015+ compatible
Usage
SuperSet
import { SuperSet } from '@eklabdev/superset';
// Create a new SuperSet
const set1 = new SuperSet([1, 2, 3]);
const set2 = SuperSet.from(new Set([3, 4, 5]));
// Basic operations
const union = set1.union(set2); // SuperSet {1, 2, 3, 4, 5}
const intersection = set1.intersection(set2); // SuperSet {3}
const difference = set1.subtract(set2); // SuperSet {1, 2}
// Functional operations
const doubled = set1.map(x => x * 2); // SuperSet {2, 4, 6}
const evenNumbers = set1.filter(x => x % 2 === 0); // SuperSet {2}
const sum = set1.reduce((acc, x) => acc + x, 0); // 6
// Set relations
const isSubset = set1.isSubset(new Set([1, 2, 3, 4])); // true
const isSuperSet = set1.isSuperSet(new Set([1, 2])); // true
const isDisjoint = set1.isDisjoint(new Set([4, 5, 6])); // trueSuperMap
import { SuperMap } from '@eklabdev/superset';
// Create a new SuperMap
const map1 = new SuperMap([['a', 1], ['b', 2]]);
const map2 = SuperMap.from(new Map([['b', 3], ['c', 4]]));
// Basic operations
const union = map1.union(map2); // SuperMap {a: 1, b: 3, c: 4}
const intersection = map1.intersection(map2); // SuperMap {b: 2}
const difference = map1.subtract(map2); // SuperMap {a: 1}
// Functional operations
const doubled = map1.map((value, key) => value * 2); // SuperSet {2, 4}
const evenValues = map1.filter((value) => value % 2 === 0); // SuperMap {b: 2}
const sum = map1.reduce((acc, value) => acc + value, 0); // 3
// Map relations
const isSubset = map1.isSubset(new Map([['a', 1], ['b', 2], ['c', 3]])); // true
const isSuperSet = map1.isSuperSet(new Map([['a', 1]])); // true
const isDisjoint = map1.isDisjoint(new Map([['c', 3], ['d', 4]])); // true
// Value checks
const hasKey = map1.includesKey('a'); // true
const hasValue = map1.includesValue(2); // trueAPI Documentation
SuperSet
Static Methods
static from<T>(set: Set<T> | SuperSet<T>): SuperSet<T>- Convert native Set or SuperSet to SuperSet
Instance Methods
isEmpty(): boolean- Check if set is emptyunion(set: Set<T> | SuperSet<T>): SuperSet<T>- Return new set combining both setsintersection(set: Set<T> | SuperSet<T>): SuperSet<T>- Return new set with common elementssubtract(set: Set<T> | SuperSet<T>): SuperSet<T>- Return new set removing elements from other setisIntersectionOf(...sets: (Set<T> | SuperSet<T>)[]): boolean- Check if this set is intersection of all provided setsisUnionOf(...sets: (Set<T> | SuperSet<T>)[]): boolean- Check if this set is union of all provided setsisSubset(set: Set<T> | SuperSet<T>): boolean- Check if this set is subset of provided setisSuperSet(set: Set<T> | SuperSet<T>): boolean- Check if this set is superset of provided setisDisjoint(set: Set<T> | SuperSet<T>): boolean- Check if sets have no common elementssome(predicate: (value: T) => boolean): boolean- Check if any element satisfies predicateevery(predicate: (value: T) => boolean): boolean- Check if all elements satisfy predicatemap<U>(predicate: (value: T) => U): SuperSet<U>- Transform elements and return new SuperSetfilter(predicate: (value: T) => boolean): SuperSet<T>- Filter elements and return new SuperSetreduce<U>(predicate: (accumulator: U, value: T) => U, initialValue: U): U- Reduce set to single valuetoArray(): T[]- Convert to arraytoSet(): Set<T>- Convert to native Set
SuperMap
Static Methods
static from<K, V>(map: Map<K, V> | SuperMap<K, V>): SuperMap<K, V>- Convert native Map or SuperMap to SuperMap
Instance Methods
isEmpty(): boolean- Check if map is emptyunion(map: Map<K, V> | SuperMap<K, V>): SuperMap<K, V>- Combine maps (later values override)intersection(map: Map<K, V> | SuperMap<K, V>): SuperMap<K, V>- Return map with common keyssubtract(map: Map<K, V> | SuperMap<K, V>): SuperMap<K, V>- Remove keys present in other mapisIntersectionOf(...maps: (Map<K, V> | SuperMap<K, V>)[]): boolean- Check if this map is intersection of all provided mapsisUnionOf(...maps: (Map<K, V> | SuperMap<K, V>)[]): boolean- Check if this map is union of all provided mapsisSubset(map: Map<K, V> | SuperMap<K, V>): boolean- Check if this map's keys are subset of provided mapisSuperSet(map: Map<K, V> | SuperMap<K, V>): boolean- Check if this map's keys are superset of provided mapisDisjoint(map: Map<K, V> | SuperMap<K, V>): boolean- Check if maps have no common keyssome(predicate: (value: V, key: K) => boolean): boolean- Check if any entry satisfies predicateevery(predicate: (value: V, key: K) => boolean): boolean- Check if all entries satisfy predicatemap<U>(predicate: (value: V, key: K) => U): SuperSet<U>- Transform values and return SuperSetfilter(predicate: (value: V, key: K) => boolean): SuperMap<K, V>- Filter entries and return new SuperMapreduce<U>(predicate: (accumulator: U, value: V, key: K) => U, initialValue: U): U- Reduce map to single valueincludesKey(key: K): boolean- Check if key exists in mapincludesValue(value: V): boolean- Check if value exists in mapvaluesToSet(): SuperSet<V>- Convert map values to SuperSetkeysToSet(): SuperSet<K>- Convert map keys to SuperSetentriesToSet(): SuperSet<[K, V]>- Convert map entries to SuperSetvaluesToArray(): V[]- Convert map values to arraykeysToArray(): K[]- Convert map keys to arraytoMap(): Map<K, V>- Convert to native Map
Performance Considerations
- All operations that return new instances are immutable
- Set operations use efficient algorithms to minimize iterations
- Array spread operations are used for simplicity and readability
- For large sets/maps, consider using native Set/Map methods directly for better performance
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
