semantic-typescript 0.2.5 → 0.2.6
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/dist/collectable.js +19 -16
- package/dist/collector.d.ts +2 -0
- package/dist/collector.js +31 -8
- package/dist/factory.d.ts +1 -0
- package/dist/factory.js +54 -62
- package/dist/semantic.d.ts +3 -3
- package/package.json +1 -1
package/dist/collectable.js
CHANGED
|
@@ -13,8 +13,8 @@ export class Collectable {
|
|
|
13
13
|
anyMatch(predicate) {
|
|
14
14
|
return this.collect(() => {
|
|
15
15
|
return false;
|
|
16
|
-
}, (
|
|
17
|
-
return
|
|
16
|
+
}, (_element, _index, accumulator) => {
|
|
17
|
+
return accumulator;
|
|
18
18
|
}, (result, element) => {
|
|
19
19
|
return result || predicate(element);
|
|
20
20
|
}, (result) => {
|
|
@@ -24,8 +24,8 @@ export class Collectable {
|
|
|
24
24
|
allMatch(predicate) {
|
|
25
25
|
return this.collect(() => {
|
|
26
26
|
return true;
|
|
27
|
-
}, (
|
|
28
|
-
return !
|
|
27
|
+
}, (_element, _index, accumulator) => {
|
|
28
|
+
return !accumulator;
|
|
29
29
|
}, (result, element) => {
|
|
30
30
|
return result && predicate(element);
|
|
31
31
|
}, (result) => {
|
|
@@ -69,20 +69,23 @@ export class Collectable {
|
|
|
69
69
|
}
|
|
70
70
|
findAny() {
|
|
71
71
|
return this.collect(() => {
|
|
72
|
-
return Optional.
|
|
73
|
-
}, () => {
|
|
74
|
-
return
|
|
72
|
+
return Optional.empty();
|
|
73
|
+
}, (_element, _index, accumulator) => {
|
|
74
|
+
return accumulator.isPresent();
|
|
75
75
|
}, (result, element) => {
|
|
76
|
-
|
|
76
|
+
if (Math.random() < 0.5) {
|
|
77
|
+
return Optional.of(element);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
77
80
|
}, (result) => {
|
|
78
81
|
return result;
|
|
79
82
|
});
|
|
80
83
|
}
|
|
81
84
|
findFirst() {
|
|
82
85
|
return this.collect(() => {
|
|
83
|
-
return Optional.
|
|
84
|
-
}, () => {
|
|
85
|
-
return
|
|
86
|
+
return Optional.empty();
|
|
87
|
+
}, (_element, _index, accumulator) => {
|
|
88
|
+
return accumulator.isPresent();
|
|
86
89
|
}, (result, element) => {
|
|
87
90
|
return result.isPresent() ? result : Optional.of(element);
|
|
88
91
|
}, (result) => {
|
|
@@ -91,9 +94,9 @@ export class Collectable {
|
|
|
91
94
|
}
|
|
92
95
|
findLast() {
|
|
93
96
|
return this.collect(() => {
|
|
94
|
-
return Optional.
|
|
97
|
+
return Optional.empty();
|
|
95
98
|
}, () => {
|
|
96
|
-
return
|
|
99
|
+
return false;
|
|
97
100
|
}, (result, element) => {
|
|
98
101
|
return result.isPresent() ? result : Optional.of(element);
|
|
99
102
|
}, (result) => {
|
|
@@ -206,10 +209,10 @@ export class Collectable {
|
|
|
206
209
|
nonMatch(predicate) {
|
|
207
210
|
return this.collect(() => {
|
|
208
211
|
return true;
|
|
209
|
-
}, (
|
|
210
|
-
return
|
|
212
|
+
}, (_element, _index, accumulator) => {
|
|
213
|
+
return !accumulator;
|
|
211
214
|
}, (result, element) => {
|
|
212
|
-
return result || predicate(element);
|
|
215
|
+
return result || !predicate(element);
|
|
213
216
|
}, (result) => {
|
|
214
217
|
return result;
|
|
215
218
|
});
|
package/dist/collector.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export declare class Collector<E, A, R> {
|
|
|
13
13
|
collect(generator: Generator<E>): R;
|
|
14
14
|
collect(iterable: Iterable<E>): R;
|
|
15
15
|
collect(semantic: Semantic<E>): R;
|
|
16
|
+
collect(start: number, end: number): R;
|
|
17
|
+
collect(start: bigint, end: bigint): R;
|
|
16
18
|
static full<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
17
19
|
static full<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
18
20
|
static shortable<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
package/dist/collector.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isFunction, isIterable, isSemantic } from "./guard";
|
|
1
|
+
import { isBigInt, isFunction, isIterable, isNumber, isSemantic } from "./guard";
|
|
2
2
|
import { CollectableSymbol } from "./symbol";
|
|
3
3
|
export class Collector {
|
|
4
4
|
identity;
|
|
@@ -17,17 +17,18 @@ export class Collector {
|
|
|
17
17
|
throw new TypeError("Invalid arguments");
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
collect(
|
|
20
|
+
collect(argument1, argument2) {
|
|
21
21
|
let accumulator = this.identity();
|
|
22
22
|
let count = 0n;
|
|
23
|
-
if (isFunction(
|
|
24
|
-
|
|
23
|
+
if (isFunction(argument1)) {
|
|
24
|
+
let generator = argument1;
|
|
25
|
+
generator((element, index) => {
|
|
25
26
|
accumulator = this.accumulator(accumulator, element, index);
|
|
26
27
|
count++;
|
|
27
28
|
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
28
29
|
}
|
|
29
|
-
else if (isIterable(
|
|
30
|
-
let iterable =
|
|
30
|
+
else if (isIterable(argument1)) {
|
|
31
|
+
let iterable = argument1;
|
|
31
32
|
let index = 0n;
|
|
32
33
|
for (let element of iterable) {
|
|
33
34
|
if (this.interrupt(element, index, accumulator)) {
|
|
@@ -38,8 +39,8 @@ export class Collector {
|
|
|
38
39
|
index++;
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
|
-
else if (isSemantic(
|
|
42
|
-
let semantic =
|
|
42
|
+
else if (isSemantic(argument1)) {
|
|
43
|
+
let semantic = argument1;
|
|
43
44
|
let generator = Reflect.get(semantic, "generator");
|
|
44
45
|
if (isFunction(generator)) {
|
|
45
46
|
generator((element, index) => {
|
|
@@ -51,6 +52,28 @@ export class Collector {
|
|
|
51
52
|
throw new TypeError("Invalid arguments");
|
|
52
53
|
}
|
|
53
54
|
}
|
|
55
|
+
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
56
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
57
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
58
|
+
for (let i = start; i < end; i++) {
|
|
59
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
63
|
+
count++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (isBigInt(argument1) && isBigInt(argument2)) {
|
|
67
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
68
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
69
|
+
for (let i = start; i < end; i++) {
|
|
70
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
74
|
+
count++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
54
77
|
return this.finisher(accumulator);
|
|
55
78
|
}
|
|
56
79
|
static full(identity, accumulator, finisher) {
|
package/dist/factory.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Semantic } from "./semantic";
|
|
|
2
2
|
import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator } from "./utility";
|
|
3
3
|
export declare let blob: Functional<Blob, Semantic<Uint8Array>> & BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
|
|
4
4
|
export declare let empty: <E>() => Semantic<E>;
|
|
5
|
+
export declare let event: BiFunctional<HTMLElement, Array<keyof HTMLElementEventMap>, Semantic<Event>>;
|
|
5
6
|
export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
|
|
6
7
|
export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
|
|
7
8
|
export declare let generate: (<E>(supplier: Supplier<E>, interrupt: Predicate<E>) => Semantic<E>) & (<E>(supplier: Supplier<E>, interrupt: BiPredicate<E, bigint>) => Semantic<E>);
|
package/dist/factory.js
CHANGED
|
@@ -4,8 +4,8 @@ import { Semantic } from "./semantic";
|
|
|
4
4
|
import { invalidate, validate } from "./utility";
|
|
5
5
|
export let blob = (blob, chunk = 64n * 1024n) => {
|
|
6
6
|
let size = Number(chunk);
|
|
7
|
-
if (size <= 0) {
|
|
8
|
-
throw new RangeError("Chunk size must be positive.");
|
|
7
|
+
if (size <= 0 || !Number.isSafeInteger(size)) {
|
|
8
|
+
throw new RangeError("Chunk size must be a safe positive integer.");
|
|
9
9
|
}
|
|
10
10
|
if (invalidate(blob)) {
|
|
11
11
|
throw new TypeError("Blob is invalid.");
|
|
@@ -17,42 +17,31 @@ export let blob = (blob, chunk = 64n * 1024n) => {
|
|
|
17
17
|
let reader = stream.getReader();
|
|
18
18
|
let buffer = new Uint8Array(size);
|
|
19
19
|
let offset = 0;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
(async () => {
|
|
21
|
+
try {
|
|
22
|
+
while (!stoppable) {
|
|
23
|
+
const { done, value } = await reader.read();
|
|
24
|
+
if (done) {
|
|
25
|
+
if (offset > 0) {
|
|
26
|
+
const element = buffer.subarray(0, offset);
|
|
27
|
+
if (interrupt(element, index)) {
|
|
28
|
+
stoppable = true;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
accept(element, index);
|
|
32
|
+
index++;
|
|
33
|
+
}
|
|
34
34
|
}
|
|
35
|
+
break;
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (interrupt(buffer, index)) {
|
|
45
|
-
stoppable = true;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
accept(buffer, index);
|
|
49
|
-
index++;
|
|
50
|
-
}
|
|
51
|
-
offset = 0;
|
|
52
|
-
const leftover = chunkData.subarray(remaining);
|
|
53
|
-
if (leftover.length > 0) {
|
|
54
|
-
buffer.set(leftover, offset);
|
|
55
|
-
offset += leftover.length;
|
|
37
|
+
let chunkData = value;
|
|
38
|
+
let position = 0;
|
|
39
|
+
while (position < chunkData.length && !stoppable) {
|
|
40
|
+
const space = size - offset;
|
|
41
|
+
const toCopy = Math.min(space, chunkData.length - position);
|
|
42
|
+
buffer.set(chunkData.subarray(position, position + toCopy), offset);
|
|
43
|
+
offset += toCopy;
|
|
44
|
+
position += toCopy;
|
|
56
45
|
if (offset === size) {
|
|
57
46
|
if (interrupt(buffer, index)) {
|
|
58
47
|
stoppable = true;
|
|
@@ -60,44 +49,47 @@ export let blob = (blob, chunk = 64n * 1024n) => {
|
|
|
60
49
|
else {
|
|
61
50
|
accept(buffer, index);
|
|
62
51
|
index++;
|
|
63
|
-
offset = 0;
|
|
64
52
|
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
buffer.set(chunkData, offset);
|
|
70
|
-
offset += length;
|
|
71
|
-
if (offset === size) {
|
|
72
|
-
if (interrupt(buffer, index)) {
|
|
73
|
-
stoppable = true;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
accept(buffer, index);
|
|
77
|
-
index++;
|
|
78
53
|
offset = 0;
|
|
79
54
|
}
|
|
80
55
|
}
|
|
81
56
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// Handle error
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
if (stoppable) {
|
|
63
|
+
await reader.cancel();
|
|
87
64
|
}
|
|
88
|
-
}).catch((error) => {
|
|
89
65
|
reader.releaseLock();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
read(reader).catch(() => {
|
|
94
|
-
reader.releaseLock();
|
|
95
|
-
});
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
96
68
|
});
|
|
97
69
|
};
|
|
98
70
|
export let empty = () => {
|
|
99
71
|
return new Semantic(() => { });
|
|
100
72
|
};
|
|
73
|
+
export let event = (element, events) => {
|
|
74
|
+
if (validate(element) && isIterable(events)) {
|
|
75
|
+
return new Semantic((accept, interrupt) => {
|
|
76
|
+
let index = 0n;
|
|
77
|
+
let stop = false;
|
|
78
|
+
for (let name of events) {
|
|
79
|
+
if (!stop) {
|
|
80
|
+
element.addEventListener(name, (event) => {
|
|
81
|
+
if (interrupt(event, index)) {
|
|
82
|
+
stop = true;
|
|
83
|
+
}
|
|
84
|
+
accept(event, index);
|
|
85
|
+
index++;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
throw new TypeError("Invalid arguments.");
|
|
92
|
+
};
|
|
101
93
|
export let fill = (element, count) => {
|
|
102
94
|
if (validate(element) && count > 0n) {
|
|
103
95
|
return new Semantic((accept, interrupt) => {
|
package/dist/semantic.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Collectable, OrderedCollectable, UnorderedCollectable, WindowCollectable } from "./collectable";
|
|
2
|
-
import {
|
|
2
|
+
import { BigIntStatistics, NumericStatistics } from "./statistics";
|
|
3
3
|
import { type Predicate } from "./utility";
|
|
4
4
|
import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator } from "./utility";
|
|
5
5
|
export declare class Semantic<E> {
|
|
@@ -31,8 +31,8 @@ export declare class Semantic<E> {
|
|
|
31
31
|
takeWhile(predicate: Predicate<E>): Semantic<E>;
|
|
32
32
|
toCollectable(): Collectable<E>;
|
|
33
33
|
toCollectable<C extends Collectable<E>>(mapper: Functional<Generator<E>, C>): C;
|
|
34
|
-
toBigintStatistics():
|
|
35
|
-
toNumericStatistics():
|
|
34
|
+
toBigintStatistics(): BigIntStatistics<E>;
|
|
35
|
+
toNumericStatistics(): NumericStatistics<E>;
|
|
36
36
|
toOrdered(): OrderedCollectable<E>;
|
|
37
37
|
toUnoredered(): UnorderedCollectable<E>;
|
|
38
38
|
toWindow(): WindowCollectable<E>;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"url": "https://github.com/eloyhere"
|
|
7
7
|
},
|
|
8
8
|
"description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.6",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|