scats 1.0.32 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -3
- package/coverage/clover.xml +937 -0
- package/coverage/coverage-final.json +15 -0
- package/coverage/lcov-report/array-iterable.ts.html +1709 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +79 -0
- package/coverage/lcov-report/collection.ts.html +1475 -0
- package/coverage/lcov-report/either.ts.html +1934 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/hashmap.ts.html +527 -0
- package/coverage/lcov-report/hashset.ts.html +392 -0
- package/coverage/lcov-report/index.html +126 -0
- package/coverage/lcov-report/index.ts.html +101 -0
- package/coverage/lcov-report/option.ts.html +758 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +170 -0
- package/coverage/lcov-report/src/abstract-map.ts.html +317 -0
- package/coverage/lcov-report/src/abstract-set.ts.html +200 -0
- package/coverage/lcov-report/src/array-iterable.ts.html +1751 -0
- package/coverage/lcov-report/src/collection.ts.html +1778 -0
- package/coverage/lcov-report/src/either.ts.html +1934 -0
- package/coverage/lcov-report/src/hashmap.ts.html +428 -0
- package/coverage/lcov-report/src/hashset.ts.html +482 -0
- package/coverage/lcov-report/src/index.html +276 -0
- package/coverage/lcov-report/src/index.ts.html +110 -0
- package/coverage/lcov-report/src/mutable/hashmap.ts.html +821 -0
- package/coverage/lcov-report/src/mutable/hashset.ts.html +611 -0
- package/coverage/lcov-report/src/mutable/index.html +126 -0
- package/coverage/lcov-report/src/mutable.ts.html +89 -0
- package/coverage/lcov-report/src/option.ts.html +758 -0
- package/coverage/lcov-report/src/try.ts.html +923 -0
- package/coverage/lcov-report/src/util.ts.html +518 -0
- package/coverage/lcov-report/try.ts.html +923 -0
- package/coverage/lcov-report/util.ts.html +518 -0
- package/coverage/lcov.info +2223 -0
- package/dist/abstract-map.d.ts +25 -0
- package/dist/abstract-map.js +62 -0
- package/dist/abstract-set.d.ts +13 -0
- package/dist/abstract-set.js +33 -0
- package/dist/array-iterable.d.ts +1 -3
- package/dist/array-iterable.js +21 -23
- package/dist/collection.d.ts +59 -20
- package/dist/collection.js +254 -62
- package/dist/hashmap.d.ts +12 -26
- package/dist/hashmap.js +39 -58
- package/dist/hashset.d.ts +12 -17
- package/dist/hashset.js +25 -35
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/mutable/hashmap.d.ts +24 -0
- package/dist/mutable/hashmap.js +118 -0
- package/dist/mutable/hashset.d.ts +20 -0
- package/dist/mutable/hashset.js +88 -0
- package/dist/mutable.d.ts +3 -0
- package/dist/mutable.js +9 -0
- package/dist/option.js +1 -1
- package/package.json +12 -12
- package/src/abstract-map.ts +79 -0
- package/src/abstract-set.ts +40 -0
- package/src/array-iterable.ts +26 -12
- package/src/collection.ts +394 -85
- package/src/either.ts +2 -2
- package/src/hashmap.ts +74 -73
- package/src/hashset.ts +82 -42
- package/src/index.ts +3 -0
- package/src/mutable/hashmap.ts +247 -0
- package/src/mutable/hashset.ts +177 -0
- package/src/mutable.ts +3 -0
- package/src/option.ts +2 -2
- package/src/try.ts +9 -9
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Option } from '../option';
|
|
2
|
+
import { AbstractMap, Tuple2 } from '../abstract-map';
|
|
3
|
+
import * as immutable from '../hashmap';
|
|
4
|
+
export declare class HashMap<K, V> extends AbstractMap<K, V, HashMap<K, V>> {
|
|
5
|
+
constructor(map?: Map<K, V>);
|
|
6
|
+
protected fromArray(array: Tuple2<K, V>[]): HashMap<K, V>;
|
|
7
|
+
static of<K, V>(...values: Tuple2<K, V>[]): HashMap<K, V>;
|
|
8
|
+
static from<K, V>(values: Iterable<Tuple2<K, V>>): HashMap<K, V>;
|
|
9
|
+
addAll(values: Iterable<Tuple2<K, V>>): this;
|
|
10
|
+
updateWith(key: K): (remappingFunction: (maybeValue: Option<V>) => Option<V>) => Option<V>;
|
|
11
|
+
subtractAll(values: Iterable<K>): this;
|
|
12
|
+
clear(): void;
|
|
13
|
+
clone(): HashMap<K, V>;
|
|
14
|
+
filterInPlace(p: (entry: Tuple2<K, V>) => boolean): this;
|
|
15
|
+
mapValuesInPlace(f: (entry: Tuple2<K, V>) => V): this;
|
|
16
|
+
getOrElseUpdate(key: K, defaultValue: () => V): V;
|
|
17
|
+
set(key: K, value: V): this;
|
|
18
|
+
put(key: K, value: V): Option<V>;
|
|
19
|
+
remove(key: K): Option<V>;
|
|
20
|
+
update(key: K, value: V): void;
|
|
21
|
+
addOne(elem: Tuple2<K, V>): this;
|
|
22
|
+
subtractOne(key: K): this;
|
|
23
|
+
get toImmutable(): immutable.HashMap<K, V>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HashMap = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const abstract_map_1 = require("../abstract-map");
|
|
6
|
+
const immutable = tslib_1.__importStar(require("../hashmap"));
|
|
7
|
+
class HashMap extends abstract_map_1.AbstractMap {
|
|
8
|
+
constructor(map = new Map()) {
|
|
9
|
+
super(map);
|
|
10
|
+
}
|
|
11
|
+
fromArray(array) {
|
|
12
|
+
return HashMap.of(...array);
|
|
13
|
+
}
|
|
14
|
+
static of(...values) {
|
|
15
|
+
return new HashMap(new Map(values));
|
|
16
|
+
}
|
|
17
|
+
static from(values) {
|
|
18
|
+
return HashMap.of(...Array.from(values));
|
|
19
|
+
}
|
|
20
|
+
addAll(values) {
|
|
21
|
+
for (const [key, value] of values) {
|
|
22
|
+
this.map.set(key, value);
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
updateWith(key) {
|
|
27
|
+
const previousValue = this.get(key);
|
|
28
|
+
return (remappingFunction) => {
|
|
29
|
+
const nextValue = remappingFunction(previousValue);
|
|
30
|
+
if (previousValue.isEmpty && nextValue.isEmpty) {
|
|
31
|
+
return nextValue;
|
|
32
|
+
}
|
|
33
|
+
else if (previousValue.isDefined && nextValue.isEmpty) {
|
|
34
|
+
this.remove(key);
|
|
35
|
+
return nextValue;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.update(key, nextValue.get);
|
|
39
|
+
return nextValue;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
subtractAll(values) {
|
|
44
|
+
if (this.isEmpty) {
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
for (const key of values) {
|
|
48
|
+
this.map.delete(key);
|
|
49
|
+
if (this.isEmpty) {
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
clear() {
|
|
56
|
+
this.map.clear();
|
|
57
|
+
}
|
|
58
|
+
clone() {
|
|
59
|
+
const contentClone = new Map(this.map);
|
|
60
|
+
return new HashMap(contentClone);
|
|
61
|
+
}
|
|
62
|
+
filterInPlace(p) {
|
|
63
|
+
if (this.nonEmpty) {
|
|
64
|
+
const entries = this.entries;
|
|
65
|
+
entries.foreach(e => {
|
|
66
|
+
if (!p(e)) {
|
|
67
|
+
this.remove(e[0]);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
mapValuesInPlace(f) {
|
|
74
|
+
if (this.nonEmpty) {
|
|
75
|
+
const entries = this.entries;
|
|
76
|
+
entries.foreach(e => {
|
|
77
|
+
this.update(e[0], f(e));
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
getOrElseUpdate(key, defaultValue) {
|
|
83
|
+
return this.get(key).getOrElse(() => {
|
|
84
|
+
const newValue = defaultValue();
|
|
85
|
+
this.map.set(key, newValue);
|
|
86
|
+
return newValue;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
set(key, value) {
|
|
90
|
+
this.map.set(key, value);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
put(key, value) {
|
|
94
|
+
const res = this.get(key);
|
|
95
|
+
this.map.set(key, value);
|
|
96
|
+
return res;
|
|
97
|
+
}
|
|
98
|
+
remove(key) {
|
|
99
|
+
const res = this.get(key);
|
|
100
|
+
this.subtractOne(key);
|
|
101
|
+
return res;
|
|
102
|
+
}
|
|
103
|
+
update(key, value) {
|
|
104
|
+
this.map.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
addOne(elem) {
|
|
107
|
+
this.map.set(elem[0], elem[1]);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
subtractOne(key) {
|
|
111
|
+
this.map.delete(key);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
get toImmutable() {
|
|
115
|
+
return immutable.HashMap.of(...this.map.entries());
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.HashMap = HashMap;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AbstractSet } from '../abstract-set';
|
|
2
|
+
import * as immutable from '../hashset';
|
|
3
|
+
export declare class HashSet<T> extends AbstractSet<T, HashSet<T>> {
|
|
4
|
+
constructor(items?: Set<T>);
|
|
5
|
+
static of<T>(...items: T[]): HashSet<T>;
|
|
6
|
+
static from<T>(values: Iterable<T>): HashSet<T>;
|
|
7
|
+
protected fromArray(array: T[]): HashSet<T>;
|
|
8
|
+
add(elem: T): boolean;
|
|
9
|
+
addAll(xs: Iterable<T>): this;
|
|
10
|
+
subtractAll(xs: Iterable<T>): this;
|
|
11
|
+
remove(elem: T): boolean;
|
|
12
|
+
filterInPlace(p: (item: T) => boolean): this;
|
|
13
|
+
clear(): void;
|
|
14
|
+
addOne(elem: T): this;
|
|
15
|
+
subtractOne(elem: T): this;
|
|
16
|
+
concat(that: Iterable<T>): HashSet<T>;
|
|
17
|
+
intersect(that: AbstractSet<T, any>): HashSet<T>;
|
|
18
|
+
union(that: AbstractSet<T, any>): HashSet<T>;
|
|
19
|
+
get toImmutable(): immutable.HashSet<T>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HashSet = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const abstract_set_1 = require("../abstract-set");
|
|
6
|
+
const immutable = tslib_1.__importStar(require("../hashset"));
|
|
7
|
+
class HashSet extends abstract_set_1.AbstractSet {
|
|
8
|
+
constructor(items = new Set()) {
|
|
9
|
+
super(items);
|
|
10
|
+
}
|
|
11
|
+
static of(...items) {
|
|
12
|
+
return new HashSet(new Set(items));
|
|
13
|
+
}
|
|
14
|
+
static from(values) {
|
|
15
|
+
return HashSet.of(...Array.from(values));
|
|
16
|
+
}
|
|
17
|
+
fromArray(array) {
|
|
18
|
+
return HashSet.of(...array);
|
|
19
|
+
}
|
|
20
|
+
add(elem) {
|
|
21
|
+
const res = this.items.has(elem);
|
|
22
|
+
if (!res) {
|
|
23
|
+
this.items.add(elem);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return !res;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
addAll(xs) {
|
|
31
|
+
for (const x of xs) {
|
|
32
|
+
this.items.add(x);
|
|
33
|
+
}
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
subtractAll(xs) {
|
|
37
|
+
for (const x of xs) {
|
|
38
|
+
this.items.delete(x);
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
remove(elem) {
|
|
43
|
+
const res = this.items.has(elem);
|
|
44
|
+
if (res) {
|
|
45
|
+
this.items.delete(elem);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return res;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
filterInPlace(p) {
|
|
53
|
+
if (this.nonEmpty) {
|
|
54
|
+
const arr = this.toArray;
|
|
55
|
+
for (const t of arr) {
|
|
56
|
+
if (!p(t)) {
|
|
57
|
+
this.remove(t);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
clear() {
|
|
64
|
+
this.items.clear();
|
|
65
|
+
}
|
|
66
|
+
addOne(elem) {
|
|
67
|
+
this.add(elem);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
subtractOne(elem) {
|
|
71
|
+
this.remove(elem);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
concat(that) {
|
|
75
|
+
const newSet = new Set([...this.items, ...that]);
|
|
76
|
+
return new HashSet(newSet);
|
|
77
|
+
}
|
|
78
|
+
intersect(that) {
|
|
79
|
+
return this.filter(x => that.contains(x));
|
|
80
|
+
}
|
|
81
|
+
union(that) {
|
|
82
|
+
return this.concat(that);
|
|
83
|
+
}
|
|
84
|
+
get toImmutable() {
|
|
85
|
+
return immutable.HashSet.of(...this.items);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.HashSet = HashSet;
|
package/dist/mutable.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HashSet = exports.HashMap = exports.ArrayBuffer = void 0;
|
|
4
|
+
var collection_1 = require("./collection");
|
|
5
|
+
Object.defineProperty(exports, "ArrayBuffer", { enumerable: true, get: function () { return collection_1.ArrayBuffer; } });
|
|
6
|
+
var hashmap_1 = require("./mutable/hashmap");
|
|
7
|
+
Object.defineProperty(exports, "HashMap", { enumerable: true, get: function () { return hashmap_1.HashMap; } });
|
|
8
|
+
var hashset_1 = require("./mutable/hashset");
|
|
9
|
+
Object.defineProperty(exports, "HashSet", { enumerable: true, get: function () { return hashset_1.HashSet; } });
|
package/dist/option.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scats",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Useful scala classes in typescript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"clean": "rimraf dist",
|
|
9
9
|
"lint": "eslint \"{src,test}/**/*.ts\" --fix",
|
|
10
|
-
"prebuild": "npm run clean && npm run lint",
|
|
10
|
+
"prebuild": "npm run clean && npm run lint && npm run test",
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"compile": "tsc",
|
|
13
13
|
"build": "npm run compile"
|
|
@@ -36,17 +36,17 @@
|
|
|
36
36
|
},
|
|
37
37
|
"homepage": "https://github.com/papirosko/scats#readme",
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/jest": "^
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "
|
|
41
|
-
"@typescript-eslint/parser": "
|
|
42
|
-
"eslint": "7.
|
|
43
|
-
"eslint-plugin-import": "^2.
|
|
44
|
-
"jest": "^
|
|
45
|
-
"ts-jest": "^
|
|
46
|
-
"ts-node": "^
|
|
47
|
-
"typescript": "^4.
|
|
39
|
+
"@types/jest": "^27.0.3",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "5.6.0",
|
|
41
|
+
"@typescript-eslint/parser": "5.6.0",
|
|
42
|
+
"eslint": "7.32.0",
|
|
43
|
+
"eslint-plugin-import": "^2.25.3",
|
|
44
|
+
"jest": "^27.4.4",
|
|
45
|
+
"ts-jest": "^27.1.1",
|
|
46
|
+
"ts-node": "^10.4.0",
|
|
47
|
+
"typescript": "^4.5.3"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"tslib": "^2.1
|
|
50
|
+
"tslib": "^2.3.1"
|
|
51
51
|
}
|
|
52
52
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {ArrayIterable} from './array-iterable';
|
|
2
|
+
import {option, Option} from './option';
|
|
3
|
+
import {HashSet} from './hashset';
|
|
4
|
+
import {Collection} from './collection';
|
|
5
|
+
|
|
6
|
+
export type Tuple2<K, V> = [K, V];
|
|
7
|
+
|
|
8
|
+
export abstract class AbstractMap<K, V, S extends AbstractMap<K, V, any>> extends ArrayIterable<Tuple2<K, V>, S> {
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
protected constructor(protected readonly map: Map<K, V>) {
|
|
12
|
+
super();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get size(): number {
|
|
16
|
+
return this.map.size;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get isEmpty(): boolean {
|
|
20
|
+
return this.map.size <= 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get(key: K): Option<V> {
|
|
24
|
+
return option(this.map.get(key));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getOrElse(key: K, defaultValue: () => V): V {
|
|
28
|
+
return this.get(key).getOrElse(defaultValue);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getOrElseValue(key: K, defaultValue: V): V {
|
|
32
|
+
return this.get(key).getOrElseValue(defaultValue);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get keySet(): HashSet<K> {
|
|
36
|
+
return HashSet.of(...Array.from(this.map.keys()));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get keyIterator(): IterableIterator<K> {
|
|
40
|
+
return this.map.keys();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get keys(): Collection<K> {
|
|
44
|
+
return new Collection(Array.from(this.map.keys()));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get values(): Collection<V> {
|
|
48
|
+
return new Collection(Array.from(this.map.values()));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get valueIterator(): IterableIterator<V> {
|
|
52
|
+
return this.map.values();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get entries(): Collection<Tuple2<K, V>> {
|
|
56
|
+
return new Collection(Array.from(this.map.entries()));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get entriesIterator(): IterableIterator<Tuple2<K, V>> {
|
|
60
|
+
return this.map.entries();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
containsKey(key: K): boolean {
|
|
64
|
+
return this.map.has(key);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get toCollection(): Collection<Tuple2<K, V>> {
|
|
68
|
+
return new Collection<Tuple2<K, V>>(Array.from(this.map.entries()));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get toMap(): Map<K, V> {
|
|
72
|
+
return this.map;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get toArray(): Array<Tuple2<K, V>> {
|
|
76
|
+
return Array.from(this.map.entries());
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {ArrayIterable} from './array-iterable';
|
|
2
|
+
import {ArrayBuffer, Collection} from './collection';
|
|
3
|
+
|
|
4
|
+
export abstract class AbstractSet<T, S extends AbstractSet<T, any>> extends ArrayIterable<T, S> {
|
|
5
|
+
|
|
6
|
+
protected constructor(protected readonly items: Set<T>) {
|
|
7
|
+
super();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
contains(item: T): boolean {
|
|
12
|
+
return this.items.has(item);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get toArray(): Array<T> {
|
|
16
|
+
return Array.from(this.items.keys());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get toCollection(): Collection<T> {
|
|
20
|
+
return new Collection(Array.from(this.items.keys()));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get toSet(): Set<T> {
|
|
24
|
+
return new Set(this.items);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get isEmpty(): boolean {
|
|
28
|
+
return this.items.size <= 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get size(): number {
|
|
32
|
+
return this.items.size;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get toBuffer(): ArrayBuffer<T> {
|
|
36
|
+
return new ArrayBuffer(Array.from(this.items.keys()));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
}
|
package/src/array-iterable.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {none, option, Option, some} from './
|
|
2
|
-
import {HashMap} from './hashmap';
|
|
3
|
-
import {Collection} from './collection';
|
|
1
|
+
import {Collection, HashMap, none, option, Option, some} from './index';
|
|
4
2
|
|
|
5
3
|
export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implements Iterable<T> {
|
|
6
4
|
|
|
@@ -53,20 +51,34 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
53
51
|
count(p: (item: T) => boolean): number {
|
|
54
52
|
let res = 0;
|
|
55
53
|
this.toArray.forEach(i => {
|
|
56
|
-
if (p(i)) {
|
|
54
|
+
if (p(i)) {
|
|
55
|
+
res++;
|
|
56
|
+
}
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
return res;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Tests whether the collection is empty.
|
|
64
|
+
* @return `true` if the collection contains no elements, `false` otherwise.
|
|
65
|
+
*/
|
|
62
66
|
get isEmpty(): boolean {
|
|
63
67
|
return this.size <= 0;
|
|
64
68
|
}
|
|
65
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Tests whether the collection is not empty.
|
|
72
|
+
* @return `true` if the collection contains at least one element, `false` otherwise.
|
|
73
|
+
*/
|
|
66
74
|
get nonEmpty(): boolean {
|
|
67
75
|
return !this.isEmpty;
|
|
68
76
|
}
|
|
69
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The size of this collection.
|
|
80
|
+
* @return the number of elements in this collection.
|
|
81
|
+
*/
|
|
70
82
|
get size(): number {
|
|
71
83
|
return this.toArray.length;
|
|
72
84
|
}
|
|
@@ -113,7 +125,7 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
113
125
|
const array = this.toArray;
|
|
114
126
|
let acc = array[0];
|
|
115
127
|
if (array.length > 1) {
|
|
116
|
-
for (let i = 1; i< array.length; i++) {
|
|
128
|
+
for (let i = 1; i < array.length; i++) {
|
|
117
129
|
acc = op(acc, array[i]);
|
|
118
130
|
}
|
|
119
131
|
}
|
|
@@ -140,7 +152,7 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
140
152
|
let acc = this.last;
|
|
141
153
|
const array = this.toArray.reverse();
|
|
142
154
|
if (array.length > 1) {
|
|
143
|
-
for (let i = 1; i< array.length; i++) {
|
|
155
|
+
for (let i = 1; i < array.length; i++) {
|
|
144
156
|
acc = op(acc, array[i]);
|
|
145
157
|
}
|
|
146
158
|
}
|
|
@@ -152,7 +164,6 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
152
164
|
}
|
|
153
165
|
|
|
154
166
|
|
|
155
|
-
|
|
156
167
|
foldLeft<B>(initial: B): (op: (acc: B, next: T) => B) => B {
|
|
157
168
|
return (op: (acc: B, next: T) => B) => {
|
|
158
169
|
return this.toArray.reduce((a, n) => op(a, n), initial);
|
|
@@ -254,7 +265,7 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
254
265
|
} else if (n === 0) {
|
|
255
266
|
return this as unknown as C;
|
|
256
267
|
} else {
|
|
257
|
-
return this.fromArray(array.slice(n
|
|
268
|
+
return this.fromArray(array.slice(n, array.length));
|
|
258
269
|
}
|
|
259
270
|
}
|
|
260
271
|
|
|
@@ -265,7 +276,7 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
265
276
|
} else if (n === 0) {
|
|
266
277
|
return this as unknown as C;
|
|
267
278
|
} else {
|
|
268
|
-
return this.fromArray(array.slice(0
|
|
279
|
+
return this.fromArray(array.slice(0, array.length - n));
|
|
269
280
|
}
|
|
270
281
|
|
|
271
282
|
}
|
|
@@ -322,6 +333,12 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
322
333
|
}
|
|
323
334
|
|
|
324
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Sums up the elements of this collection.
|
|
338
|
+
*
|
|
339
|
+
* @param elementToNum a parameter converting an element to a number.
|
|
340
|
+
* @return the sum of all elements produced by elementToNum over the elements of this collection.
|
|
341
|
+
*/
|
|
325
342
|
sum(elementToNum: (element: T) => number): number {
|
|
326
343
|
if (this.isEmpty) {
|
|
327
344
|
return 0;
|
|
@@ -367,7 +384,6 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
367
384
|
}
|
|
368
385
|
|
|
369
386
|
|
|
370
|
-
|
|
371
387
|
/** The rest of the collection without its first element. */
|
|
372
388
|
get tail(): C {
|
|
373
389
|
if (this.isEmpty) throw new Error('empty.tail');
|
|
@@ -409,8 +425,6 @@ export abstract class ArrayIterable<T, C extends ArrayIterable<T, any>> implemen
|
|
|
409
425
|
}
|
|
410
426
|
|
|
411
427
|
|
|
412
|
-
|
|
413
|
-
|
|
414
428
|
/**
|
|
415
429
|
* Partitions this $coll into a map according to a discriminator function `key`. All the values that
|
|
416
430
|
* have the same discriminator are then transformed by the `f` function and then reduced into a
|