nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
103 lines (102 loc) • 2.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../linq/index");
const Collection_1 = require("./Collection");
const List_1 = require("./List");
class SortedCollection {
constructor(comparator, enumerable) {
this.comparator = comparator;
this.arr = [];
if (enumerable && enumerable[Symbol.iterator]) {
for (const x of enumerable) {
this.arr.push(x);
}
this.arr = this.arr.sort(this.comparator);
}
}
add(...rest) {
for (const x of rest) {
this.arr.push(x);
}
this.arr = this.arr.sort(this.comparator);
}
remove(item) {
const found = this.indexOf(item);
if (found > -1) {
this.arr.splice(found, 1);
this.arr = this.arr.sort(this.comparator);
return true;
}
return false;
}
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);
}
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 array = [];
for (const xx of this.arr) {
array.push(xx);
}
return array;
}
toCollection() {
return new Collection_1.Collection(this);
}
toList() {
return new List_1.List(this);
}
linq() {
return new index_1.Enumerable(this);
}
plinq() {
return new index_1.ParallelEnumerable(this);
}
[Symbol.iterator]() {
return this.arr[Symbol.iterator]();
}
clone() {
return new SortedCollection(this.comparator, this);
}
toSet() {
return new Set(this);
}
isEmpty() {
return this.size === 0;
}
}
exports.SortedCollection = SortedCollection;