UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

423 lines (422 loc) 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const MemoryParallelQueryableProvider_1 = require("./linq/MemoryParallelQueryableProvider"); const Enumerable_1 = require("./linq/Enumerable"); const ParallelEnumerable_1 = require("./linq/ParallelEnumerable"); const List_1 = require("./collections/List"); const ObjectUtils_1 = require("./utils/ObjectUtils"); const NumberUtils_1 = require("./utils/NumberUtils"); const parallels_1 = require("./parallels"); const StringUtils_1 = require("./utils/StringUtils"); function getEunmerable() { return new Enumerable_1.Enumerable(this); } function getParallelEunmerable() { return new ParallelEnumerable_1.ParallelEnumerable(this, new MemoryParallelQueryableProvider_1.MemoryParallelQueryableProvider()); } function contains(item) { return this.includes(item); } function hasDuplicate() { const len = this.length, out = [], counts = []; for (let i = 0; i < len; i++) { const item1 = this[i]; let found = counts.find(xx => xx.item === item1); if (!found) { found = { item: item1, count: 0 }; counts.push(found); } found.count++; if (found.count > 1) { return true; } } return false; } function findDuplicates() { const len = this.length, out = [], counts = []; for (let i = 0; i < len; i++) { const item1 = this[i]; let found = counts.find(xx => xx.item === item1); if (!found) { found = { item: item1, count: 0 }; counts.push(found); } found.count++; if (found.count === 2) { out.push(item1); } } return out; } function itemCount() { const len = this.length, counts = []; for (let i = 0; i < len; i++) { const item1 = this[i]; let found = counts.find(xx => xx.item === item1); if (!found) { found = { item: item1, count: 0 }; counts.push(found); } found.count++; } return counts; } function toUnique() { const a = []; for (let i = 0, l = this.length; i < l; i++) if (a.indexOf(this[i]) === -1) a.push(this[i]); return a; } function mapContainsKey(key) { for (const item of this.keys()) { if (key === item) return true; } return false; } function mapContainsValue(value) { for (const item of this.values()) { if (value === item) return true; } return false; } function toArray() { const arr = []; for (const item of this[Symbol.iterator]()) { arr.push(item); } return arr; } function toList() { const arr = new List_1.List(); for (const item of this[Symbol.iterator]()) { arr.add(item); } return arr; } function mapKeyToList() { const arr = new List_1.List(); for (const [key, value] of this[Symbol.iterator]()) { arr.add(key); } return arr; } function mapValueToList() { const arr = new List_1.List(); for (const [key, value] of this[Symbol.iterator]()) { arr.add(value); } return arr; } function mapIsEmpty() { return this.size === 0; } function mapPut(key, value) { this.set(key, value); return this; } function mapPutAll(...entries) { for (const [key, value] of entries) { this.set(key, value); } return this; } function mapMap(callbackfn, thisArg) { const res = new Map(); let counter = 0; for (const [key, value] of this) { const fc = callbackfn([key, value], counter, thisArg || this); res.set(fc[0], fc[1]); counter++; } return res; } function mapWeakMap(callbackfn, thisArg) { const res = new WeakMap(); let counter = 0; for (const [key, value] of this) { const fc = callbackfn([key, value], counter, thisArg || this); res.set(fc[0], fc[1]); counter++; } return res; } function mapEntriesToObjectDictionary() { const arr = {}; for (const [key, value] of this[Symbol.iterator]()) { arr[key] = value; } return arr; } function setFilter(callbackfn, thisArg) { const res = new Set(); let counter = 0; for (const item of this) { if (callbackfn(item, counter, thisArg || this)) { res.add(item); } counter++; } return res; } function setWeakFilter(callbackfn, thisArg) { const res = new WeakSet(); let counter = 0; for (const item of this) { if (callbackfn(item, counter, thisArg || this)) { res.add(item); } counter++; } return res; } function setFind(callbackfn, thisArg) { let counter = 0; for (const item of this) { if (callbackfn(item, counter, thisArg || this)) { return item; } counter++; } return null; } function setEvery(callbackfn, thisArg) { const res = true; let counter = 0; for (const item of this) { if (!callbackfn(item, counter, thisArg || this)) { return false; } counter++; } return res; } function setSome(callbackfn, thisArg) { const res = false; let counter = 0; for (const item of this) { if (callbackfn(item, counter, thisArg || this)) { return true; } counter++; } return res; } function setMap(callbackfn, thisArg) { const res = new Set(); let counter = 0; for (const item of this) { res.add(callbackfn(item, counter, thisArg || this)); counter++; } return res; } function setWeakMap(callbackfn, thisArg) { const res = new WeakSet(); let counter = 0; for (const item of this) { res.add(callbackfn(item, counter, thisArg || this)); counter++; } return res; } function setUnion(other) { const result = new Set(this); for (const item of other) { if (!result.has(item)) { result.add(item); } } return result; } function setIntersect(other) { const result = new Set(); for (const item of other) { if (this.has(item)) { result.add(item); } } return result; } function setExcept(other) { const result = new Set(this); for (const item of other) { if (result.has(item)) { result.delete(item); } } return result; } function setXor(other) { const unioned = this.union(other); const intersected = this.intersect(other); return unioned.except(intersected); } function setIsSuperSetOf(other) { const result = true; for (const item of other) { if (!this.has(item)) { return false; } } return result; } function setIsSubSetOf(other) { const result = true; for (const item of this) { if (!other.has(item)) { return false; } } return result; } function setIsEmpty() { return this.size === 0; } function setJoin(seperator) { let res = ''; const that = this; seperator = seperator !== undefined ? seperator : ' , '; if (that.size === 0) { return ''; } else if (that.size === 1) { return that.values().next().value.toString(); } else { const itr = that.values(); res = itr.next().value.toString(); for (const item of itr) { res = res + seperator + item.toString(); } } return res; } function clearArray() { this.length = 0; } function arrayGroupByImpl(array, fn) { const groups = {}; array.forEach(function (o) { const group = JSON.stringify(fn(o)); groups[group] = groups[group] || []; groups[group].push(o); }); return Object.keys(groups).map(function (group) { return groups[group]; }); } function arrayGroupBy(selector) { if (typeof selector === 'string') { const s = selector.split(','); const fn = (item) => { return s.map((xx) => item[xx]); }; return arrayGroupByImpl(this, fn); } else if (Array.isArray(selector)) { const fn = (item) => { return selector.map((xx) => item[xx]); }; return arrayGroupByImpl(this, fn); } else { return arrayGroupByImpl(this, selector); } } Object.createInstance = ObjectUtils_1.createInstance; Object.deepAssign = ObjectUtils_1.toDeepAssign; Object.isObjectType = ObjectUtils_1.isObjectType; Object.cloneObject = ObjectUtils_1.toCloneObject; Object.deepEqual = ObjectUtils_1.deepEqual; String.prototype.toPascalCase = StringUtils_1.toPascalCaseWrapper; String.prototype.toCamelCase = StringUtils_1.toCamelCaseWrapper; String.prototype.toTitleCase = StringUtils_1.toTitleCaseWrapper; String.prototype.toSentenceCase = StringUtils_1.toSentenceCaseWrapper; String.prototype.toUpperFirstCase = StringUtils_1.toUpperFirstCaseWrapper; String.prototype.toLowerFirstCase = StringUtils_1.toLowerFirstCaseWrapper; String.prototype.toSnakeCase = StringUtils_1.toSnakeCaseWrapper; String.prototype.toDotCase = StringUtils_1.toDotCaseWrapper; Number.prototype.toBigDecimal = NumberUtils_1.toBigDecimal; Number.prototype.toComplexNumber = NumberUtils_1.toComplexNumber; Array.prototype.clear = clearArray; Array.prototype.groupBy = arrayGroupBy; Array.prototype.toUnique = toUnique; Array.prototype.itemCount = itemCount; Array.prototype.findDuplicates = findDuplicates; Array.prototype.hasDuplicate = hasDuplicate; Array.prototype.contains = contains; Array.prototype.linq = getEunmerable; Array.prototype.plinq = getParallelEunmerable; Array.prototype.toArray = toArray; Array.prototype.toList = toList; Map.prototype.toArray = toArray; Map.prototype.toList = toList; Map.prototype.toKeyList = mapKeyToList; Map.prototype.toValueList = mapValueToList; Map.prototype.toObject = mapEntriesToObjectDictionary; Map.prototype.containsKey = mapContainsKey; Map.prototype.containsValue = mapContainsValue; Map.prototype.linq = getEunmerable; Map.prototype.plinq = getParallelEunmerable; Map.prototype.isEmpty = mapIsEmpty; Map.prototype.putAll = mapPutAll; Map.prototype.put = mapPut; Map.prototype.map = mapMap; WeakMap.prototype.map = mapWeakMap; WeakMap.prototype.putAll = mapPutAll; WeakMap.prototype.put = mapPut; WeakMap.prototype.isEmpty = mapIsEmpty; WeakMap.prototype.toArray = toArray; WeakMap.prototype.toList = toList; WeakMap.prototype.toKeyList = mapKeyToList; WeakMap.prototype.toValueList = mapValueToList; WeakMap.prototype.toObject = mapEntriesToObjectDictionary; WeakMap.prototype.containsKey = mapContainsKey; WeakMap.prototype.containsValue = mapContainsValue; WeakMap.prototype.linq = getEunmerable; WeakMap.prototype.plinq = getParallelEunmerable; Set.prototype.linq = getEunmerable; Set.prototype.plinq = getParallelEunmerable; Set.prototype.toArray = toArray; Set.prototype.toList = toList; Set.prototype.intersect = setIntersect; Set.prototype.union = setUnion; Set.prototype.except = setExcept; Set.prototype.xor = setXor; Set.prototype.isEmpty = setIsEmpty; Set.prototype.isSubSetOf = setIsSubSetOf; Set.prototype.isSuperSetOf = setIsSuperSetOf; Set.prototype.join = setJoin; Set.prototype.filter = setFilter; Set.prototype.find = setFind; Set.prototype.every = setEvery; Set.prototype.some = setSome; Set.prototype.map = setMap; WeakSet.prototype.linq = getEunmerable; WeakSet.prototype.plinq = getParallelEunmerable; WeakSet.prototype.toArray = toArray; WeakSet.prototype.toList = toList; WeakSet.prototype.intersect = setIntersect; WeakSet.prototype.union = setUnion; WeakSet.prototype.except = setExcept; WeakSet.prototype.xor = setXor; WeakSet.prototype.isEmpty = setIsEmpty; WeakSet.prototype.isSubSetOf = setIsSubSetOf; WeakSet.prototype.isSuperSetOf = setIsSuperSetOf; WeakSet.prototype.join = setJoin; WeakSet.prototype.filter = setWeakFilter; WeakSet.prototype.find = setFind; WeakSet.prototype.every = setEvery; WeakSet.prototype.some = setSome; WeakSet.prototype.map = setWeakMap; Promise.prototype.delay = parallels_1.delay; Promise.prototype.timeout = parallels_1.timeout; Promise.prototype.spread = parallels_1.spread; Promise.prototype.wait = parallels_1.wait; Promise.delay = parallels_1.delay; Promise.extendedPromise = parallels_1.extendedPromise; Promise.wait = parallels_1.wait;