@berish/linq
Version:
LINQ - very usefull helper for working with arrays and collections
244 lines • 8.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const decimal_js_1 = require("decimal.js");
class LINQ extends Array {
static from(items) {
if (this.isLINQ(items))
return items;
const linq = new LINQ();
if (items && items.length > 0)
linq.push(...items);
return linq;
}
static isLINQ(data) {
if (data instanceof LINQ)
return true;
return false;
}
toArray() {
return super.slice();
}
clone() {
return this.slice(0);
}
equals(linq) {
return this === linq;
}
equalsValues(linq, selectFunc) {
if (this.equals(linq))
return true;
if (linq == null)
return false;
if (this.length !== linq.length)
return false;
// If you don't care about the order of the elements inside
// the array, you should sort both arrays here.
// Please note that calling sort on an array will modify that array.
// you might want to clone your array first.
const linq1 = this.select(selectFunc);
const linq2 = LINQ.from(linq).select(selectFunc);
return linq1.every((m, i) => linq2[i] === m);
}
where(whereFunc) {
if (!whereFunc)
return this;
const returnValue = this.filter((m, i) => whereFunc(m, i, this));
return LINQ.from(returnValue);
}
whereInSelect(selectFunc, whereFunc) {
if (!selectFunc || !whereFunc)
return this;
const selected = this.select(selectFunc);
return this.where((m, i) => whereFunc(selected[i], i, selected));
}
whereInSelectWithAccum(selectFunc, getAccumFunc, whereFunc) {
if (!selectFunc || !getAccumFunc || !whereFunc)
return this;
const selected = this.select(selectFunc);
const accum = getAccumFunc(selected, this);
return this.where((m, i, linq) => whereFunc(selected[i], i, selected, accum));
}
select(selectFunc) {
if (!selectFunc)
return this;
const returnValue = this.map((m, i) => selectFunc(m, i, this));
return LINQ.from(returnValue);
}
selectMany(selectFunc) {
const selected = this.select(selectFunc);
const returnValue = [].concat(...selected);
return LINQ.from(returnValue);
}
take(count) {
const returnValue = this.slice(0, count);
return LINQ.from(returnValue);
}
skip(count) {
const returnValue = this.slice(count);
return LINQ.from(returnValue);
}
count(whereFunc) {
if (!whereFunc)
return this.length || 0;
return this.where(whereFunc).count();
}
indexWhere(whereFunc) {
const selected = this.select((m, i, linq) => (whereFunc(m, i, linq) ? i : null));
return selected.notNull();
}
elementsAtIndex(indexes) {
return LINQ.from(indexes).select(m => this[m]);
}
notNull(selectFunc) {
if (selectFunc)
return this.where((m, i, linq) => selectFunc(m, i, linq) != null);
return this.where(m => m != null);
}
notEmpty(selectFunc) {
if (selectFunc)
return this.where((m, i, linq) => !!selectFunc(m, i, linq));
return this.where(m => !!m);
}
first(whereFunc) {
if (whereFunc)
return this.where(whereFunc).first();
return this[0];
}
last(whereFunc) {
if (whereFunc)
return this.where(whereFunc).last();
const lastIndex = this.length ? this.length - 1 : 0;
return this[lastIndex];
}
distinct(selectFunc) {
if (selectFunc)
return this.whereInSelect(selectFunc, (m, i, selected) => selected.indexOf(m) === i);
return this.where((m, i, linq) => linq.indexOf(m) === i);
}
max(numberFunc) {
return this.whereInSelectWithAccum(numberFunc, selected => decimal_js_1.default.max(...selected).toNumber(), (m, i, linq, max) => m === max);
}
maxValue(numberFunc) {
return decimal_js_1.default.max(...this.select(numberFunc));
}
min(numberFunc) {
return this.whereInSelectWithAccum(numberFunc, selected => decimal_js_1.default.min(...selected).toNumber(), (m, i, linq, min) => m === min);
}
minValue(numberFunc) {
return decimal_js_1.default.min(...this.select(numberFunc));
}
ofType(type, selectFunc) {
const types = Array.isArray(type) ? LINQ.from(type) : LINQ.from([type]);
return this.whereInSelect(selectFunc, m => types.some(k => (typeof k === 'string' ? typeof m === k : m instanceof k)));
}
orderByAscending(sortSelectFunc) {
if (sortSelectFunc)
return LINQ.from(this.sort((a, b) => {
const ak = sortSelectFunc(a);
const bk = sortSelectFunc(b);
if (ak < bk)
return -1;
if (ak > bk)
return 1;
return 0;
}));
return LINQ.from(this.sort((a, b) => {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}));
}
orderByDescending(sortSelectFunc) {
if (sortSelectFunc)
return LINQ.from(this.sort((a, b) => {
const ak = sortSelectFunc(a);
const bk = sortSelectFunc(b);
if (ak > bk)
return -1;
if (ak < bk)
return 1;
return 0;
}));
return LINQ.from(this.sort((a, b) => {
if (a > b)
return -1;
if (a < b)
return 1;
return 0;
}));
}
sum(selectFunc) {
if (selectFunc)
return this.select(selectFunc).sum();
return this.reduce((a, b) => a.plus(new decimal_js_1.default(b)), new decimal_js_1.default(0));
}
except(items, selectFunc) {
const array = Array.isArray(items) ? items : [items];
if (selectFunc) {
const linq = LINQ.from(array).select(selectFunc);
return this.where((m, i, l) => linq.indexOf(selectFunc(m, i, l)) === -1);
}
return this.where(m => array.indexOf(m) === -1);
}
groupBy(selectFunc, compareKeyFunc) {
compareKeyFunc = compareKeyFunc || ((a, b) => a === b);
const tuples = LINQ.from();
const keys = this.select(selectFunc);
for (const key of keys) {
const cacheTuple = tuples.filter(m => compareKeyFunc(m[0], key))[0];
if (!cacheTuple)
tuples.push([key, this.whereInSelect(selectFunc, k => compareKeyFunc(key, k))]);
}
return tuples;
}
contains(value, selectFunc) {
if (selectFunc)
return this.select(selectFunc).contains(selectFunc(value));
return this.indexOf(value) !== -1;
}
containsAll(value, selectFunc) {
if (selectFunc) {
const selected = this.select(selectFunc);
return value.every((m, i) => selected.indexOf(selectFunc(m, i, this)) !== -1);
}
return value.every(m => this.indexOf(m) !== -1);
}
average(selectFunc, whereFunc) {
if (whereFunc)
return this.where(whereFunc).average(selectFunc);
if (selectFunc)
return this.select(selectFunc).average();
return new decimal_js_1.default(this.sum()).dividedBy(this.count());
}
intersect(items, selectFunc) {
const array = Array.isArray(items) ? items : [items];
if (selectFunc) {
const linq = LINQ.from(array).select(selectFunc);
return this.where((m, i, l) => linq.indexOf(selectFunc(m, i, l)) !== -1);
}
return this.where(m => array.indexOf(m) !== -1);
}
// Corrected return types for system Array<T> methods
reverse() {
return super.reverse();
}
concat(...items) {
return super.concat(...items);
}
slice(start, end) {
return super.slice(start, end);
}
splice(start, deleteCount, ...items) {
return super.splice(start, deleteCount, ...items);
}
map(callbackfn, thisArg) {
return super.map(callbackfn, thisArg);
}
filter(callbackfn, thisArg) {
return super.filter(callbackfn, thisArg);
}
}
exports.default = LINQ;
//# sourceMappingURL=index.js.map