UNPKG

nowjs-core

Version:

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

102 lines (101 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Enumerable_1 = require("../linq/Enumerable"); const ParallelEnumerable_1 = require("../linq/ParallelEnumerable"); const List_1 = require("./List"); class Collection { constructor(enumerable) { this.arr = []; if (enumerable && enumerable[Symbol.iterator]) { for (const x of enumerable) { this.arr.push(x); } } } isEmpty() { return this.size === 0; } add(...items) { if (items instanceof Array) { for (const x of items) { this.arr.push(x); } } else { this.arr.push(items); } } remove(item) { const ix = this.arr.indexOf(item); if (ix === -1 || ix > this.arr.length) return false; return this.arr.splice(ix, 1).length === 1; } clear() { this.arr.splice(0, this.arr.length); return true; } contains(item) { return this.arr.includes(item); } get size() { return this.arr.length; } get(index) { return this.arr[index]; } indexOf(item) { return this.arr.indexOf(item); } lastIndexOf(item) { return this.arr.lastIndexOf(item); } clone() { return new Collection(this); } join(seperator) { let res = ''; const that = this; seperator = seperator !== undefined ? seperator : ' , '; if (that.size === 0) { return ''; } else if (that.size === 1) { return that[Symbol.iterator]().next().value.toString(); } else { const itr = that[Symbol.iterator](); res = itr.next().value.toString(); for (const item of itr) { res = res + seperator + item.toString(); } } return res; } toArray() { const arr = []; for (const item of this) { arr.push(item); } return arr; } toCollection() { return new Collection(this); } toList() { return new List_1.List(this); } toSet() { return new Set(this); } linq() { return new Enumerable_1.Enumerable(this); } plinq() { return new ParallelEnumerable_1.ParallelEnumerable(this); } [Symbol.iterator]() { return this.arr[Symbol.iterator](); } } exports.Collection = Collection;