space-lift
Version:
TypeScript Array, Object, Map, Set, Union, Enum utils
81 lines (80 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSetPipe = exports.SetWrapper = void 0;
/** A Set wrapper providing extra functionalities and more chaining opportunities */
class SetWrapper {
constructor(_value) {
this._value = _value;
this._isLiftWrapper = true;
/**
* Pipes this Set with an arbitrary transformation function.
*/
this.pipe = pipe;
}
value() {
return this._value;
}
_clone() {
return new Set(this._value);
}
/**
* Adds a new value to this Set.
*/
add(item) {
return new SetWrapper(this._clone().add(item));
}
/**
* Adds all items from the passed iterable to this Set.
*/
addAll(items) {
return new SetWrapper(new Set([...this._value, ...items]));
}
/**
* Deletes one value from this Set.
*/
delete(item) {
const result = this._clone();
result.delete(item);
return new SetWrapper(result);
}
/**
* Maps this Set's items, unless void or undefined is returned, in which case the item is filtered.
* This is effectively a `filter` + `map` combined in one.
*/
collect(iterator) {
const result = new Set();
this._value.forEach(item => {
const res = iterator(item);
if (res !== undefined)
result.add(res);
});
return new SetWrapper(result);
}
filter(predicate) {
return this.collect(item => (predicate(item) ? item : undefined));
}
/**
* Returns the Set of all items of this Set that are also found in the passed Set.
*/
intersection(other) {
return this.filter(item => other.has(item));
}
/**
* Returns the Set of all items of this Set that are not found in the passed Set.
*/
difference(other) {
return this.filter(item => !other.has(item));
}
/**
* Transforms this Set into an Array. The insertion order is kept.
*/
toArray() {
return this.pipe(s => [...s]);
}
}
exports.SetWrapper = SetWrapper;
let pipe;
function setSetPipe(_pipe) {
pipe = _pipe;
}
exports.setSetPipe = setSetPipe;