@jamashita/lluvia-sequence 2.5.0 → 2.8.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/dist/cjs/ASequence.d.ts +9 -8
- package/dist/cjs/ASequence.d.ts.map +1 -1
- package/dist/cjs/ASequence.js +39 -50
- package/dist/cjs/ASequence.js.map +1 -1
- package/dist/cjs/ImmutableSequence.d.ts +4 -5
- package/dist/cjs/ImmutableSequence.d.ts.map +1 -1
- package/dist/cjs/ImmutableSequence.js +0 -1
- package/dist/cjs/ImmutableSequence.js.map +1 -1
- package/dist/cjs/MutableSequence.d.ts +7 -8
- package/dist/cjs/MutableSequence.d.ts.map +1 -1
- package/dist/cjs/MutableSequence.js +3 -4
- package/dist/cjs/MutableSequence.js.map +1 -1
- package/dist/cjs/ReadonlySequence.d.ts +2 -1
- package/dist/cjs/ReadonlySequence.d.ts.map +1 -1
- package/dist/cjs/Sequence.d.ts +1 -1
- package/dist/cjs/Sequence.d.ts.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +6 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/{Mock → mock}/MockSequence.d.ts +1 -2
- package/dist/cjs/mock/MockSequence.d.ts.map +1 -0
- package/dist/cjs/{Mock → mock}/MockSequence.js +0 -1
- package/dist/cjs/mock/MockSequence.js.map +1 -0
- package/dist/esm/ASequence.d.ts +9 -8
- package/dist/esm/ASequence.d.ts.map +1 -1
- package/dist/esm/ASequence.js +39 -50
- package/dist/esm/ASequence.js.map +1 -1
- package/dist/esm/ImmutableSequence.d.ts +4 -5
- package/dist/esm/ImmutableSequence.d.ts.map +1 -1
- package/dist/esm/ImmutableSequence.js +0 -1
- package/dist/esm/ImmutableSequence.js.map +1 -1
- package/dist/esm/MutableSequence.d.ts +7 -8
- package/dist/esm/MutableSequence.d.ts.map +1 -1
- package/dist/esm/MutableSequence.js +3 -4
- package/dist/esm/MutableSequence.js.map +1 -1
- package/dist/esm/ReadonlySequence.d.ts +2 -1
- package/dist/esm/ReadonlySequence.d.ts.map +1 -1
- package/dist/esm/Sequence.d.ts +1 -1
- package/dist/esm/Sequence.d.ts.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/{Mock → mock}/MockSequence.d.ts +1 -2
- package/dist/esm/mock/MockSequence.d.ts.map +1 -0
- package/dist/esm/{Mock → mock}/MockSequence.js +0 -1
- package/dist/esm/mock/MockSequence.js.map +1 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +6 -8
- package/src/ASequence.ts +53 -67
- package/src/ImmutableSequence.ts +19 -21
- package/src/MutableSequence.ts +16 -18
- package/src/ReadonlySequence.ts +3 -1
- package/src/Sequence.ts +1 -1
- package/src/__tests__/ASequence.spec.ts +227 -229
- package/src/__tests__/ImmutableSequence.spec.ts +231 -285
- package/src/__tests__/MutableSequence.spec.ts +210 -260
- package/src/index.ts +1 -1
- package/src/{Mock → mock}/MockSequence.ts +1 -3
- package/dist/cjs/Mock/MockSequence.d.ts.map +0 -1
- package/dist/cjs/Mock/MockSequence.js.map +0 -1
- package/dist/esm/Mock/MockSequence.d.ts.map +0 -1
- package/dist/esm/Mock/MockSequence.js.map +0 -1
package/src/ASequence.ts
CHANGED
@@ -12,7 +12,7 @@ import {
|
|
12
12
|
import { Quantity } from '@jamashita/lluvia-collection';
|
13
13
|
import { Sequence } from './Sequence';
|
14
14
|
|
15
|
-
export abstract class ASequence<V, T extends ASequence<V, T
|
15
|
+
export abstract class ASequence<V, T extends ASequence<V, T>> extends Quantity<number, V> implements Sequence<V> {
|
16
16
|
protected sequence: Array<V>;
|
17
17
|
|
18
18
|
protected constructor(sequence: Array<V>) {
|
@@ -20,17 +20,17 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
20
20
|
this.sequence = sequence;
|
21
21
|
}
|
22
22
|
|
23
|
-
public abstract add(value: V): Sequence<V
|
24
|
-
|
25
|
-
public abstract set(key: number, value: V): T;
|
26
|
-
|
27
|
-
public abstract remove(key: number): T;
|
23
|
+
public abstract add(value: V): Sequence<V>;
|
28
24
|
|
29
25
|
public abstract duplicate(): T;
|
30
26
|
|
27
|
+
public abstract override filter(predicate: BinaryPredicate<V, number>): T;
|
28
|
+
|
31
29
|
public abstract override map<W>(mapper: Mapper<V, W>): Sequence<W>;
|
32
30
|
|
33
|
-
public abstract
|
31
|
+
public abstract remove(key: number): T;
|
32
|
+
|
33
|
+
public abstract set(key: number, value: V): T;
|
34
34
|
|
35
35
|
public abstract sort(comparator: BinaryFunction<V, V, number>): T;
|
36
36
|
|
@@ -60,38 +60,32 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
60
60
|
return false;
|
61
61
|
}
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
let tr: IteratorResult<V> = ti.next();
|
66
|
-
let or: IteratorResult<unknown> = oi.next();
|
67
|
-
|
68
|
-
while (tr.done !== true && or.done !== true) {
|
69
|
-
if (isEqualable(tr.value) && isEqualable(or.value)) {
|
70
|
-
if (!tr.value.equals(or.value)) {
|
71
|
-
return false;
|
72
|
-
}
|
73
|
-
}
|
74
|
-
else if (tr.value !== or.value) {
|
75
|
-
return false;
|
76
|
-
}
|
77
|
-
|
78
|
-
tr = ti.next();
|
79
|
-
or = oi.next();
|
80
|
-
|
81
|
-
if (tr.done === true && or.done === true) {
|
63
|
+
return this.sequence.every((v: V, i: number) => {
|
64
|
+
if (v === other.sequence[i]) {
|
82
65
|
return true;
|
83
66
|
}
|
84
|
-
|
67
|
+
if (isEqualable(v)) {
|
68
|
+
return v.equals(other.sequence[i]);
|
69
|
+
}
|
85
70
|
|
86
|
-
|
71
|
+
return false;
|
72
|
+
});
|
87
73
|
}
|
88
74
|
|
89
75
|
public every(predicate: BinaryPredicate<V, number>): boolean {
|
90
|
-
|
91
|
-
|
76
|
+
return this.sequence.every(predicate);
|
77
|
+
}
|
78
|
+
|
79
|
+
protected filterInternal(predicate: BinaryPredicate<V, number>): Array<V> {
|
80
|
+
const arr: Array<V> = [];
|
81
|
+
|
82
|
+
this.sequence.forEach((v: V, i: number) => {
|
83
|
+
if (predicate(v, i)) {
|
84
|
+
arr.push(v);
|
85
|
+
}
|
92
86
|
});
|
93
87
|
|
94
|
-
return
|
88
|
+
return arr;
|
95
89
|
}
|
96
90
|
|
97
91
|
public find(predicate: BinaryPredicate<V, number>): Nullable<V> {
|
@@ -105,9 +99,7 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
105
99
|
}
|
106
100
|
|
107
101
|
public forEach(catalogue: Catalogue<number, V>): void {
|
108
|
-
this.sequence.forEach(
|
109
|
-
catalogue(v, i);
|
110
|
-
});
|
102
|
+
this.sequence.forEach(catalogue);
|
111
103
|
}
|
112
104
|
|
113
105
|
public get(key: number): Nullable<V> {
|
@@ -126,40 +118,12 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
126
118
|
}).values();
|
127
119
|
}
|
128
120
|
|
129
|
-
public
|
130
|
-
|
131
|
-
return
|
132
|
-
}
|
133
|
-
}
|
134
|
-
|
135
|
-
public size(): number {
|
136
|
-
return this.sequence.length;
|
137
|
-
}
|
138
|
-
|
139
|
-
public some(predicate: BinaryPredicate<V, number>): boolean {
|
140
|
-
const found: Ambiguous<V> = this.sequence.find(predicate);
|
141
|
-
|
142
|
-
return !Kind.isUndefined(found);
|
143
|
-
}
|
144
|
-
|
145
|
-
public toArray(): Array<V> {
|
146
|
-
return [...this.sequence];
|
147
|
-
}
|
148
|
-
|
149
|
-
public values(): Iterable<V> {
|
150
|
-
return this.toArray();
|
151
|
-
}
|
152
|
-
|
153
|
-
protected filterInternal(predicate: BinaryPredicate<V, number>): Array<V> {
|
154
|
-
const arr: Array<V> = [];
|
155
|
-
|
156
|
-
this.sequence.forEach((v: V, i: number) => {
|
157
|
-
if (predicate(v, i)) {
|
158
|
-
arr.push(v);
|
159
|
-
}
|
160
|
-
});
|
121
|
+
public reduce(reducer: BinaryFunction<V, V, V>, initialValue?: V): V {
|
122
|
+
if (Kind.isUndefined(initialValue)) {
|
123
|
+
return this.sequence.reduce(reducer);
|
124
|
+
}
|
161
125
|
|
162
|
-
return
|
126
|
+
return this.sequence.reduce(reducer, initialValue);
|
163
127
|
}
|
164
128
|
|
165
129
|
protected removeInternal(key: number): Array<V> {
|
@@ -173,6 +137,12 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
173
137
|
return [...this.sequence.slice(0, key), ...this.sequence.slice(key + 1)];
|
174
138
|
}
|
175
139
|
|
140
|
+
public serialize(): string {
|
141
|
+
return this.sequence.map<string>((v: V) => {
|
142
|
+
return Objet.identify(v);
|
143
|
+
}).join(', ');
|
144
|
+
}
|
145
|
+
|
176
146
|
protected setInternal(key: number, value: V): Array<V> {
|
177
147
|
if (!Kind.isInteger(key)) {
|
178
148
|
return this.sequence;
|
@@ -183,4 +153,20 @@ export abstract class ASequence<V, T extends ASequence<V, T>, N extends string =
|
|
183
153
|
|
184
154
|
return [...this.sequence.slice(0, key), value, ...this.sequence.slice(key + 1)];
|
185
155
|
}
|
156
|
+
|
157
|
+
public size(): number {
|
158
|
+
return this.sequence.length;
|
159
|
+
}
|
160
|
+
|
161
|
+
public some(predicate: BinaryPredicate<V, number>): boolean {
|
162
|
+
return this.sequence.some(predicate);
|
163
|
+
}
|
164
|
+
|
165
|
+
public toArray(): Array<V> {
|
166
|
+
return [...this.sequence];
|
167
|
+
}
|
168
|
+
|
169
|
+
public values(): Iterable<V> {
|
170
|
+
return this.toArray();
|
171
|
+
}
|
186
172
|
}
|
package/src/ImmutableSequence.ts
CHANGED
@@ -2,29 +2,27 @@ import { BinaryFunction, BinaryPredicate, Mapper } from '@jamashita/anden-type';
|
|
2
2
|
import { Collection } from '@jamashita/lluvia-collection';
|
3
3
|
import { ASequence } from './ASequence';
|
4
4
|
|
5
|
-
export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V
|
6
|
-
public readonly noun: 'ImmutableSequence' = 'ImmutableSequence';
|
7
|
-
|
5
|
+
export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>> {
|
8
6
|
private static readonly EMPTY: ImmutableSequence<unknown> = new ImmutableSequence<unknown>([]);
|
9
7
|
|
10
|
-
public static empty<
|
11
|
-
return ImmutableSequence.EMPTY as ImmutableSequence<
|
8
|
+
public static empty<V>(): ImmutableSequence<V> {
|
9
|
+
return ImmutableSequence.EMPTY as ImmutableSequence<V>;
|
12
10
|
}
|
13
11
|
|
14
|
-
public static of<
|
15
|
-
return ImmutableSequence.ofInternal
|
12
|
+
public static of<V>(collection: Collection<number, V>): ImmutableSequence<V> {
|
13
|
+
return ImmutableSequence.ofInternal([...collection.values()]);
|
16
14
|
}
|
17
15
|
|
18
|
-
public static ofArray<
|
19
|
-
return ImmutableSequence.ofInternal
|
16
|
+
public static ofArray<V>(array: ReadonlyArray<V>): ImmutableSequence<V> {
|
17
|
+
return ImmutableSequence.ofInternal([...array]);
|
20
18
|
}
|
21
19
|
|
22
|
-
private static ofInternal<
|
20
|
+
private static ofInternal<V>(array: Array<V>): ImmutableSequence<V> {
|
23
21
|
if (array.length === 0) {
|
24
|
-
return ImmutableSequence.empty
|
22
|
+
return ImmutableSequence.empty();
|
25
23
|
}
|
26
24
|
|
27
|
-
return new ImmutableSequence
|
25
|
+
return new ImmutableSequence(array);
|
28
26
|
}
|
29
27
|
|
30
28
|
protected constructor(sequence: Array<V>) {
|
@@ -32,23 +30,23 @@ export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>, 'Im
|
|
32
30
|
}
|
33
31
|
|
34
32
|
public add(value: V): ImmutableSequence<V> {
|
35
|
-
return ImmutableSequence.ofArray
|
33
|
+
return ImmutableSequence.ofArray([...this.sequence, value]);
|
36
34
|
}
|
37
35
|
|
38
36
|
public duplicate(): ImmutableSequence<V> {
|
39
37
|
if (this.isEmpty()) {
|
40
|
-
return ImmutableSequence.empty
|
38
|
+
return ImmutableSequence.empty();
|
41
39
|
}
|
42
40
|
|
43
|
-
return ImmutableSequence.ofArray
|
41
|
+
return ImmutableSequence.ofArray([...this.sequence]);
|
44
42
|
}
|
45
43
|
|
46
44
|
public filter(predicate: BinaryPredicate<V, number>): ImmutableSequence<V> {
|
47
|
-
return ImmutableSequence.ofArray
|
45
|
+
return ImmutableSequence.ofArray(this.filterInternal(predicate));
|
48
46
|
}
|
49
47
|
|
50
48
|
public override isEmpty(): boolean {
|
51
|
-
if (this === ImmutableSequence.empty
|
49
|
+
if (this === ImmutableSequence.empty()) {
|
52
50
|
return true;
|
53
51
|
}
|
54
52
|
|
@@ -56,7 +54,7 @@ export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>, 'Im
|
|
56
54
|
}
|
57
55
|
|
58
56
|
public map<W>(mapper: Mapper<V, W>): ImmutableSequence<W> {
|
59
|
-
return ImmutableSequence.ofArray
|
57
|
+
return ImmutableSequence.ofArray(this.sequence.map(mapper));
|
60
58
|
}
|
61
59
|
|
62
60
|
public remove(key: number): ImmutableSequence<V> {
|
@@ -66,7 +64,7 @@ export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>, 'Im
|
|
66
64
|
return this;
|
67
65
|
}
|
68
66
|
|
69
|
-
return ImmutableSequence.ofArray
|
67
|
+
return ImmutableSequence.ofArray(sequence);
|
70
68
|
}
|
71
69
|
|
72
70
|
public set(key: number, value: V): ImmutableSequence<V> {
|
@@ -76,7 +74,7 @@ export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>, 'Im
|
|
76
74
|
return this;
|
77
75
|
}
|
78
76
|
|
79
|
-
return ImmutableSequence.ofArray
|
77
|
+
return ImmutableSequence.ofArray(sequence);
|
80
78
|
}
|
81
79
|
|
82
80
|
public sort(comparator: BinaryFunction<V, V, number>): ImmutableSequence<V> {
|
@@ -84,6 +82,6 @@ export class ImmutableSequence<V> extends ASequence<V, ImmutableSequence<V>, 'Im
|
|
84
82
|
|
85
83
|
arr.sort(comparator);
|
86
84
|
|
87
|
-
return ImmutableSequence.ofArray
|
85
|
+
return ImmutableSequence.ofArray(arr);
|
88
86
|
}
|
89
87
|
}
|
package/src/MutableSequence.ts
CHANGED
@@ -2,54 +2,52 @@ import { BinaryFunction, BinaryPredicate, Mapper } from '@jamashita/anden-type';
|
|
2
2
|
import { Collection } from '@jamashita/lluvia-collection';
|
3
3
|
import { ASequence } from './ASequence';
|
4
4
|
|
5
|
-
export class MutableSequence<V> extends ASequence<V, MutableSequence<V
|
6
|
-
public
|
7
|
-
|
8
|
-
public static empty<VT>(): MutableSequence<VT> {
|
9
|
-
return MutableSequence.ofArray<VT>([]);
|
5
|
+
export class MutableSequence<V> extends ASequence<V, MutableSequence<V>> {
|
6
|
+
public static empty<V>(): MutableSequence<V> {
|
7
|
+
return MutableSequence.ofArray([]);
|
10
8
|
}
|
11
9
|
|
12
|
-
public static of<
|
13
|
-
return MutableSequence.ofInternal
|
10
|
+
public static of<V>(collection: Collection<number, V>): MutableSequence<V> {
|
11
|
+
return MutableSequence.ofInternal([...collection.values()]);
|
14
12
|
}
|
15
13
|
|
16
|
-
public static ofArray<
|
17
|
-
return MutableSequence.ofInternal
|
14
|
+
public static ofArray<V>(array: ReadonlyArray<V>): MutableSequence<V> {
|
15
|
+
return MutableSequence.ofInternal([...array]);
|
18
16
|
}
|
19
17
|
|
20
|
-
private static ofInternal<
|
21
|
-
return new MutableSequence
|
18
|
+
private static ofInternal<V>(array: Array<V>): MutableSequence<V> {
|
19
|
+
return new MutableSequence(array);
|
22
20
|
}
|
23
21
|
|
24
22
|
protected constructor(sequence: Array<V>) {
|
25
23
|
super(sequence);
|
26
24
|
}
|
27
25
|
|
28
|
-
public add(value: V):
|
26
|
+
public add(value: V): this {
|
29
27
|
this.sequence.push(value);
|
30
28
|
|
31
29
|
return this;
|
32
30
|
}
|
33
31
|
|
34
32
|
public duplicate(): MutableSequence<V> {
|
35
|
-
return MutableSequence.ofArray
|
33
|
+
return MutableSequence.ofArray([...this.sequence]);
|
36
34
|
}
|
37
35
|
|
38
36
|
public filter(predicate: BinaryPredicate<V, number>): MutableSequence<V> {
|
39
|
-
return MutableSequence.ofArray
|
37
|
+
return MutableSequence.ofArray(this.filterInternal(predicate));
|
40
38
|
}
|
41
39
|
|
42
40
|
public map<W>(mapper: Mapper<V, W>): MutableSequence<W> {
|
43
|
-
return MutableSequence.ofArray
|
41
|
+
return MutableSequence.ofArray(this.sequence.map(mapper));
|
44
42
|
}
|
45
43
|
|
46
|
-
public remove(key: number):
|
44
|
+
public remove(key: number): this {
|
47
45
|
this.sequence = this.removeInternal(key);
|
48
46
|
|
49
47
|
return this;
|
50
48
|
}
|
51
49
|
|
52
|
-
public set(key: number, value: V):
|
50
|
+
public set(key: number, value: V): this {
|
53
51
|
this.sequence = this.setInternal(key, value);
|
54
52
|
|
55
53
|
return this;
|
@@ -60,6 +58,6 @@ export class MutableSequence<V> extends ASequence<V, MutableSequence<V>, 'Mutabl
|
|
60
58
|
|
61
59
|
arr.sort(comparator);
|
62
60
|
|
63
|
-
return MutableSequence.ofArray
|
61
|
+
return MutableSequence.ofArray(arr);
|
64
62
|
}
|
65
63
|
}
|
package/src/ReadonlySequence.ts
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
import { BinaryFunction, BinaryPredicate, Cloneable, Mapper } from '@jamashita/anden-type';
|
2
2
|
import { Collection } from '@jamashita/lluvia-collection';
|
3
3
|
|
4
|
-
export interface ReadonlySequence<V
|
4
|
+
export interface ReadonlySequence<V> extends Collection<number, V>, Cloneable<ReadonlySequence<V>> {
|
5
5
|
filter(predicate: BinaryPredicate<V, number>): ReadonlySequence<V>;
|
6
6
|
|
7
7
|
iterator(): IterableIterator<[number, V]>;
|
8
8
|
|
9
9
|
map<W>(mapper: Mapper<V, W>): ReadonlySequence<W>;
|
10
10
|
|
11
|
+
reduce(reducer: BinaryFunction<V, V, V>, initialValue?: V): V;
|
12
|
+
|
11
13
|
sort(comparator: BinaryFunction<V, V, number>): ReadonlySequence<V>;
|
12
14
|
|
13
15
|
toArray(): Array<V>;
|
package/src/Sequence.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { BinaryPredicate, Mapper } from '@jamashita/anden-type';
|
2
2
|
import { ReadonlySequence } from './ReadonlySequence';
|
3
3
|
|
4
|
-
export interface Sequence<V
|
4
|
+
export interface Sequence<V> extends ReadonlySequence<V> {
|
5
5
|
add(value: V): Sequence<V>;
|
6
6
|
|
7
7
|
filter(predicate: BinaryPredicate<V, number>): Sequence<V>;
|