UNPKG

uc-base-libraries

Version:
291 lines 12.4 kB
System.register([], function (exports_1, context_1) { "use strict"; var Queryable; var __moduleName = context_1 && context_1.id; return { setters: [], execute: function () { Queryable = class Queryable { constructor(array) { this.array = array; this.add = (item) => { this.array.push(item); }; this.remove = (item) => { this.array.remove(item); }; this.push = (item) => { this.array.push(item); }; this.toArray = () => { return this.array.slice(0); }; this.distinct = (compareFunction) => { const lst = new Queryable(); this.forEach((t) => { if (!lst.contains(t, compareFunction)) { lst.add(t); } }); return lst; }; this.where = (whereClause) => { if (!whereClause) { return new Queryable(this.array.slice(0)); } const lst2 = []; this.array.forEach((item) => { if (whereClause(item)) { lst2.push(item); } }); return new Queryable(lst2); }; this.any = (whereClause) => { if (!whereClause) { return this.array.length > 0; } return this.where(whereClause).any(); }; this.forEach = (func) => { const list = this.array; if (func == null) { return false; } $.each(list, (i, item) => { func(item); }); return true; }; this.sum = (func) => { if (!func) { func = (obj) => { return obj; }; } let cnt = 0; this.forEach((item) => { cnt = cnt + func(item); }); return cnt; }; this.max = (func) => { if (!func) { func = (obj) => { return obj; }; } let mx = func(this.first()); this.forEach((item) => { const v = func(item); if (mx < v) { mx = v; } }); return mx; }; this.min = (func) => { if (!func) { func = (obj) => { return obj; }; } let mx = func(this.first()); this.forEach((item) => { const v = func(item); if (mx > v) { mx = v; } }); return mx; }; this.select = (selectItem) => { if (selectItem == null) { return new Queryable(this.array.slice(0)); } return new Queryable(this.array.map(selectItem)); }; this.orderBy = (orderBy, isDescending = false) => { return this.orderByFunction((ob1, ob2) => { const v1 = orderBy(ob1); const v2 = orderBy(ob2); if (v1 > v2) { return 1; } if (v1 < v2) { return -1; } return 0; }, isDescending); }; this.orderByFunction = (orderBy, isDescending = false) => { isDescending = !!isDescending; if (orderBy == null) { return new Queryable(this.array.slice(0)); } let clone = this.array.slice(0); clone.sort(orderBy); if (isDescending) { clone = clone.reverse(); } return new Queryable(clone); }; this.reverse = () => { return new Queryable(this.array.reverse()); }; this.skip = (count) => { if (this.length < count) { return new Queryable([]); } this.array.splice(0, count); return new Queryable(this.array.slice(0)); }; this.take = (count) => { if (this.length === 0) { return new Queryable([]); } if (count > this.length) { count = this.length; } this.array.splice(count - 1, this.length - count); return new Queryable(this.array.slice(0)); }; this.first = () => { if (this.length === 0) { return null; } return this.array[0]; }; this.last = () => { if (this.length === 0) { return null; } return this.array[this.length - 1]; }; this.findItem = (selectItem) => { return this.where(selectItem).first(); }; this.find = (selectItem) => { return this.where(selectItem).first(); }; this.contains = (item, compareFunction) => { if (!compareFunction) { compareFunction = this.equals; } return this.where((item2) => compareFunction(item, item2)).any(); }; this.union = (arr) => { if (arr instanceof Queryable) { return new Queryable(this.array.concat(arr.toArray())); } else { return new Queryable(this.array.concat(arr)); } }; this.intersect = (arr, compareFunction) => { if (!compareFunction) { compareFunction = this.equals; } let q = null; if (arr instanceof Queryable) { q = arr; } else { q = new Queryable(this.array.concat(arr)); } const lst2 = []; this.forEach((item) => { if (q.contains(item, compareFunction)) { lst2.push(item); } }); return new Queryable(lst2); }; this.difference = (arr, compareFunction) => { if (!compareFunction) { compareFunction = this.equals; } let q = null; if (arr instanceof Queryable) { q = arr; } else { q = new Queryable(this.array.concat(arr)); } const lst2 = []; this.forEach((item) => { if (!q.contains(item, compareFunction)) { lst2.push(item); } }); return new Queryable(lst2); }; this.copy = () => { return new Queryable(this.array.slice(0)); }; this.asQueryable = () => { return new Queryable(this.array.slice(0)); }; this.indexOf = (item) => { return this.array.indexOf(item); }; if (this.array == null) { this.array = new Array(); } } equals(x, y) { if (x === y) return true; // if both x and y are null or undefined and exactly the same if (!(x instanceof Object) || !(y instanceof Object)) return false; // if they are not strictly equal, they both need to be Objects if (x.constructor !== y.constructor) return false; // they must have the exact same prototype chain, the closest we can do is // test there constructor. for (const p in x) { if (!x.hasOwnProperty(p)) continue; // other properties were tested using x.constructor === y.constructor if (!y.hasOwnProperty(p)) return false; // allows to compare x[ p ] and y[ p ] when set to undefined if (x[p] === y[p]) continue; // if they have the same strict value or identity then they are equal if (typeof x[p] !== 'object') return false; // Numbers, Strings, Functions, Booleans must be strictly equal if (!this.equals(x[p], y[p])) return false; // Objects and Arrays must be tested recursively } for (const p in y) { if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false; // allows x[ p ] to be set to undefined } return true; } get length() { return this.array.length; } get count() { return this.array.length; } }; exports_1("Queryable", Queryable); Array.prototype.asQueryable = function () { return new Queryable(this); }; Array.prototype.remove = function (item) { const index = this.indexOf(item); if (index > -1) { this.splice(index, 1); } }; } }; }); //# sourceMappingURL=LinqToJs.js.map