lincd
Version:
LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)
134 lines • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreSet = void 0;
class CoreSet extends Set {
createNew(...args) {
return new this.constructor(...args);
}
filter(fn) {
var res = this.createNew();
for (var item of this) {
if (fn(item, item, this)) {
res.add(item);
}
}
return res;
}
reduce(fn, initialValue) {
let i = 0;
let res = initialValue;
for (let item of this) {
res = fn(res, item, i++, this);
}
return res;
}
/**
* Returns the value of the first element in the Set where predicate is true, and undefined
* otherwise.
*/
find(predicate, thisArg) {
for (let item of this) {
if (predicate.apply(thisArg, [item, item, this])) {
return item;
}
}
return undefined;
}
first() {
return this.values().next().value;
}
/**
* Returns the last element added to the set
*/
last() {
//unfortunately there is no efficient way to do this. Either we convert the set to an array or we loop over all values to discover the last
let item;
for (item of this)
;
return item;
}
getFirst(n) {
return this.createNew([...this].slice(0, n));
}
slice(start, end) {
return this.createNew([...this].slice(start, end));
}
sort(compareFn, thisArg) {
//convert this set to an array, sort it with provided parameters, create a new set of the same type and provide the sorted array as content
var sortedArray = thisArg ? [...this].sort.apply(thisArg, [compareFn]) : [...this].sort(compareFn);
return this.createNew(sortedArray);
}
/**
* Determines whether all the members of an array satisfy the specified test.
* @param callbackfn A function that accepts up to three arguments. The every method calls the callbackfn function for each element in array1 until the callbackfn returns false, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
every(callbackfn, thisArg) {
for (let item of this) {
if (!callbackfn.apply(thisArg, [item, this])) {
return false;
}
}
return true;
}
/**
* Determines whether the specified callback function returns true for any element of an array.
* @param callbackfn A function that accepts up to three arguments. The some method calls the callbackfn function for each element in array1 until the callbackfn returns true, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
some(callbackfn, thisArg) {
for (let item of this) {
if (callbackfn.apply(thisArg, [item, this])) {
return true;
}
}
return false;
}
/**
* Calls a defined callback function on each element of the set, and returns an ARRAY that contains the results.
* Note that this method returns an array so that sets can be more easily used with for example JSX
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
map(callbackfn, thisArg) {
//NOTE: we changed this method so that it returns an array because it
// causes trouble in ES5 when we create a new CoreSet and try to add the mapped results which may not have a unique toString() implementation (like in React)
//create the result map, whos values will be 'mapped' into new values from the current values of this map
var mapped = [];
for (let item of this) {
//do the mapping
mapped.push(callbackfn.apply(thisArg, [item, this]));
}
return mapped;
}
concat(...sets) {
var res = this.createNew(this);
for (var set of sets) {
set.forEach(res.add.bind(res));
}
return res;
}
clone() {
return this.createNew(this);
}
reverse() {
return this.createNew([...this].reverse());
}
print() {
console.log(this.toString());
}
/**
* Similar to concat, but adds all items to THIS set instead of creating a new one
* @param sets
*/
addFrom(...sets) {
for (var set of sets) {
if (set) {
set.forEach(this.add.bind(this));
}
}
return this;
}
}
exports.CoreSet = CoreSet;
//# sourceMappingURL=CoreSet.js.map