atomic-fns 0.3.4 → 0.3.5
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 +1 -1
- package/core/collections/abc.d.ts +1 -1
- package/core/collections/abc.ts +1 -1
- package/core/collections/deque.d.ts +55 -48
- package/core/collections/deque.js +61 -53
- package/core/collections/deque.ts +63 -54
- package/core/functools/index.d.ts +1 -0
- package/core/functools/index.ts +2 -0
- package/core/globals/index.d.ts +1 -1
- package/core/globals/index.js +1 -1
- package/core/globals/index.ts +1 -1
- package/core/i18n/date/index.d.ts +2 -1
- package/core/i18n/date/index.ts +1 -1
- package/package.json +1 -1
- package/tests/globals.spec.ts +5 -5
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ You may prefer this because it's:
|
|
|
21
21
|
|
|
22
22
|
## Get Started
|
|
23
23
|
|
|
24
|
-
This library is available as an npm package.
|
|
24
|
+
This library is available as an [npm package](https://www.npmjs.com/package/atomic-fns).
|
|
25
25
|
|
|
26
26
|
To install the package you need to have [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (or [yarn](https://yarnpkg.com/getting-started/install)) and then run:
|
|
27
27
|
|
package/core/collections/abc.ts
CHANGED
|
@@ -2,107 +2,114 @@ import { Iteratee } from '../globals/index.js';
|
|
|
2
2
|
import { Sequence } from './abc.js';
|
|
3
3
|
/**
|
|
4
4
|
* Fixed capacity double-ended queue implemented as ring deque.
|
|
5
|
-
*
|
|
6
|
-
* @constructor
|
|
5
|
+
* @template T
|
|
7
6
|
*/
|
|
8
|
-
export declare class Deque<T> extends Sequence<T> {
|
|
7
|
+
export declare class Deque<T = any> extends Sequence<T> {
|
|
9
8
|
items: T[];
|
|
10
9
|
start: number;
|
|
11
10
|
count: number;
|
|
12
11
|
capacity: number;
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
ArrayConstructor: any;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new `Deque` instance of a fixed size.
|
|
15
|
+
* @param {Array} [arrType = []] The initial elements or array type to use (accepts any kind of typed array).
|
|
16
|
+
* @param {number} [capacity=1024] The fixed size of the deque. Defaults to `1024`.
|
|
17
|
+
*/
|
|
18
|
+
constructor(arrType?: T[], capacity?: number);
|
|
19
|
+
/** Returns the total number of elements in the deque. */
|
|
15
20
|
get size(): number;
|
|
16
21
|
/**
|
|
17
22
|
* Method used to clear the structure.
|
|
18
|
-
*
|
|
19
|
-
* @return {undefined}
|
|
20
23
|
*/
|
|
21
24
|
clear(): void;
|
|
22
25
|
/**
|
|
23
26
|
* Alias of {@link Deque.append}
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
* @return {number} - Returns the new size of the deque.
|
|
27
|
+
* @param {T} item Item to append.
|
|
28
|
+
* @returns {number} Returns the new size of the deque.
|
|
27
29
|
*/
|
|
28
30
|
add(item: any): number;
|
|
29
31
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
33
|
-
* @return {number} - Returns the new size of the deque.
|
|
32
|
+
* Appends a new element to the end of the deque.
|
|
33
|
+
* @param {T} item Item to append.
|
|
34
|
+
* @returns {number} Returns the new size of the deque.
|
|
34
35
|
*/
|
|
35
36
|
append(item: any): number;
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @return {number} - Returns the new size of the deque.
|
|
38
|
+
* Appends a new element to the beginning of the deque.
|
|
39
|
+
* @param {T} item Item to prepend.
|
|
40
|
+
* @returns {number} Returns the new size of the deque.
|
|
41
41
|
*/
|
|
42
42
|
appendleft(item: any): number;
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @return {any} - Returns the popped item.
|
|
44
|
+
* Removes and returns the element at the end of the deque.
|
|
45
|
+
* @returns {?T} Returns the popped item.
|
|
47
46
|
*/
|
|
48
47
|
pop(): T | undefined;
|
|
49
48
|
/**
|
|
50
49
|
* Alias of {@link Deque.pop}
|
|
50
|
+
* @returns {?T} Returns the popped item.
|
|
51
51
|
*/
|
|
52
52
|
remove(): T | undefined;
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @return {any} - Returns the shifted item.
|
|
54
|
+
* Removes and returns the element at the beginning of the deque.
|
|
55
|
+
* @returns {?T} Returns the removed deque element.
|
|
57
56
|
*/
|
|
58
57
|
popleft(): T | undefined;
|
|
59
58
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @return {any}
|
|
59
|
+
* Returns the element at the beginning of the deque.
|
|
60
|
+
* @returns {?T} The element if exists
|
|
63
61
|
*/
|
|
64
62
|
first(): T | undefined;
|
|
65
63
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @return {any}
|
|
64
|
+
* Returns the element at a specified index position in the deque.
|
|
65
|
+
* @param {number} index The given index
|
|
66
|
+
* @returns {?T} The element if exists
|
|
70
67
|
*/
|
|
71
68
|
get(index: any): T | undefined;
|
|
72
69
|
/**
|
|
73
|
-
* Update the
|
|
74
|
-
* @param index The given index
|
|
75
|
-
* @param value The new value to set at `index`.
|
|
70
|
+
* Update the element at a given `index`.
|
|
71
|
+
* @param {number} index The given index
|
|
76
72
|
*/
|
|
77
73
|
set(index: number, value: T): void;
|
|
74
|
+
/**
|
|
75
|
+
* Check if a given element is in the deque.
|
|
76
|
+
* @param {T} x The value to find
|
|
77
|
+
* @returns {boolean} `true` if the value is found
|
|
78
|
+
*/
|
|
78
79
|
contains(x: any): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the first element that matches the `iteratee` function.
|
|
82
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
83
|
+
* @returns {?T} The element if exists
|
|
84
|
+
*/
|
|
79
85
|
find(iteratee: Iteratee<T>): T | undefined;
|
|
80
86
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* @param {function} iteratee - Function to call for each item.
|
|
84
|
-
* @param {object} scope - Optional scope.
|
|
85
|
-
* @return {undefined}
|
|
87
|
+
* Calls `iteratee` function for every element in the deque.
|
|
88
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
86
89
|
*/
|
|
87
90
|
forEach(iteratee: any): void;
|
|
91
|
+
/**
|
|
92
|
+
* Returns a new array with all the elements in reverse order.
|
|
93
|
+
* @returns {Array<T>} The elements reversed
|
|
94
|
+
*/
|
|
88
95
|
reversed(): any;
|
|
89
96
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @return {array}
|
|
97
|
+
* Returns a new array with all the elements in the order they were added to the deque.
|
|
98
|
+
* @returns {Array<T>} The elements array
|
|
93
99
|
*/
|
|
94
100
|
toArray(): any;
|
|
95
101
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @return {Iterator}
|
|
102
|
+
* Returns an iterator over the deque's values.
|
|
103
|
+
* @returns {Iterator}
|
|
99
104
|
*/
|
|
100
105
|
values(): Iterable<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Alias of {@link Deque.values}. Returns iterator of values.
|
|
108
|
+
*/
|
|
101
109
|
keys(): Iterable<T>;
|
|
102
110
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* @return {Iterator}
|
|
111
|
+
* Creates an iterator of `[index, value]` pairs for all elements in the deque.
|
|
112
|
+
* @returns {Iterator}
|
|
106
113
|
*/
|
|
107
114
|
entries(): Iterator<[number, T], any, undefined> & Iterable<[number, T]>;
|
|
108
115
|
[Symbol.iterator](): Iterable<T>;
|
|
@@ -4,36 +4,39 @@ import { eq } from '../operators/index.js';
|
|
|
4
4
|
import { Sequence } from './abc.js';
|
|
5
5
|
/**
|
|
6
6
|
* Fixed capacity double-ended queue implemented as ring deque.
|
|
7
|
-
*
|
|
8
|
-
* @constructor
|
|
7
|
+
* @template T
|
|
9
8
|
*/
|
|
10
9
|
export class Deque extends Sequence {
|
|
11
10
|
items;
|
|
12
11
|
start = 0;
|
|
13
12
|
count = 0;
|
|
14
13
|
capacity;
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
ArrayConstructor;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new `Deque` instance of a fixed size.
|
|
17
|
+
* @param {Array} [arrType = []] The initial elements or array type to use (accepts any kind of typed array).
|
|
18
|
+
* @param {number} [capacity=1024] The fixed size of the deque. Defaults to `1024`.
|
|
19
|
+
*/
|
|
20
|
+
constructor(arrType = [], capacity = 1024) {
|
|
17
21
|
super();
|
|
18
22
|
if (typeof capacity !== 'number' || capacity <= 0)
|
|
19
23
|
throw new ValueError(`Deque capacity should be a positive integer.`);
|
|
20
|
-
this.
|
|
24
|
+
this.ArrayConstructor = arrType.constructor;
|
|
21
25
|
this.capacity = capacity;
|
|
22
|
-
this.items = new this.
|
|
26
|
+
this.items = new this.ArrayConstructor(arrType.length || this.capacity);
|
|
23
27
|
this.clear();
|
|
24
|
-
if (
|
|
25
|
-
for (const item of
|
|
28
|
+
if (arrType.length) {
|
|
29
|
+
for (const item of arrType) {
|
|
26
30
|
this.append(item);
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
}
|
|
34
|
+
/** Returns the total number of elements in the deque. */
|
|
30
35
|
get size() {
|
|
31
36
|
return this.count;
|
|
32
37
|
}
|
|
33
38
|
/**
|
|
34
39
|
* Method used to clear the structure.
|
|
35
|
-
*
|
|
36
|
-
* @return {undefined}
|
|
37
40
|
*/
|
|
38
41
|
clear() {
|
|
39
42
|
// Properties
|
|
@@ -42,18 +45,16 @@ export class Deque extends Sequence {
|
|
|
42
45
|
}
|
|
43
46
|
/**
|
|
44
47
|
* Alias of {@link Deque.append}
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
* @return {number} - Returns the new size of the deque.
|
|
48
|
+
* @param {T} item Item to append.
|
|
49
|
+
* @returns {number} Returns the new size of the deque.
|
|
48
50
|
*/
|
|
49
51
|
add(item) {
|
|
50
52
|
return this.append(item);
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @return {number} - Returns the new size of the deque.
|
|
55
|
+
* Appends a new element to the end of the deque.
|
|
56
|
+
* @param {T} item Item to append.
|
|
57
|
+
* @returns {number} Returns the new size of the deque.
|
|
57
58
|
*/
|
|
58
59
|
append(item) {
|
|
59
60
|
if (this.count === this.capacity)
|
|
@@ -63,10 +64,9 @@ export class Deque extends Sequence {
|
|
|
63
64
|
return ++this.count;
|
|
64
65
|
}
|
|
65
66
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @return {number} - Returns the new size of the deque.
|
|
67
|
+
* Appends a new element to the beginning of the deque.
|
|
68
|
+
* @param {T} item Item to prepend.
|
|
69
|
+
* @returns {number} Returns the new size of the deque.
|
|
70
70
|
*/
|
|
71
71
|
appendleft(item) {
|
|
72
72
|
if (this.count === this.capacity)
|
|
@@ -79,9 +79,8 @@ export class Deque extends Sequence {
|
|
|
79
79
|
return ++this.count;
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* @return {any} - Returns the popped item.
|
|
82
|
+
* Removes and returns the element at the end of the deque.
|
|
83
|
+
* @returns {?T} Returns the popped item.
|
|
85
84
|
*/
|
|
86
85
|
pop() {
|
|
87
86
|
if (this.count === 0)
|
|
@@ -92,14 +91,14 @@ export class Deque extends Sequence {
|
|
|
92
91
|
}
|
|
93
92
|
/**
|
|
94
93
|
* Alias of {@link Deque.pop}
|
|
94
|
+
* @returns {?T} Returns the popped item.
|
|
95
95
|
*/
|
|
96
96
|
remove() {
|
|
97
97
|
return this.pop();
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @return {any} - Returns the shifted item.
|
|
100
|
+
* Removes and returns the element at the beginning of the deque.
|
|
101
|
+
* @returns {?T} Returns the removed deque element.
|
|
103
102
|
*/
|
|
104
103
|
popleft() {
|
|
105
104
|
if (this.count === 0)
|
|
@@ -112,9 +111,8 @@ export class Deque extends Sequence {
|
|
|
112
111
|
return this.items[index];
|
|
113
112
|
}
|
|
114
113
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @return {any}
|
|
114
|
+
* Returns the element at the beginning of the deque.
|
|
115
|
+
* @returns {?T} The element if exists
|
|
118
116
|
*/
|
|
119
117
|
first() {
|
|
120
118
|
if (this.count === 0)
|
|
@@ -122,10 +120,9 @@ export class Deque extends Sequence {
|
|
|
122
120
|
return this.items[this.start];
|
|
123
121
|
}
|
|
124
122
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @
|
|
128
|
-
* @return {any}
|
|
123
|
+
* Returns the element at a specified index position in the deque.
|
|
124
|
+
* @param {number} index The given index
|
|
125
|
+
* @returns {?T} The element if exists
|
|
129
126
|
*/
|
|
130
127
|
get(index) {
|
|
131
128
|
if (this.count === 0)
|
|
@@ -138,9 +135,8 @@ export class Deque extends Sequence {
|
|
|
138
135
|
return this.items[index];
|
|
139
136
|
}
|
|
140
137
|
/**
|
|
141
|
-
* Update the
|
|
142
|
-
* @param index The given index
|
|
143
|
-
* @param value The new value to set at `index`.
|
|
138
|
+
* Update the element at a given `index`.
|
|
139
|
+
* @param {number} index The given index
|
|
144
140
|
*/
|
|
145
141
|
set(index, value) {
|
|
146
142
|
if (index < 0)
|
|
@@ -149,6 +145,11 @@ export class Deque extends Sequence {
|
|
|
149
145
|
throw new KeyError(`Invalid index: ${index} not in Deque.`);
|
|
150
146
|
this.items[index] = value;
|
|
151
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Check if a given element is in the deque.
|
|
150
|
+
* @param {T} x The value to find
|
|
151
|
+
* @returns {boolean} `true` if the value is found
|
|
152
|
+
*/
|
|
152
153
|
contains(x) {
|
|
153
154
|
for (const value of this.values()) {
|
|
154
155
|
if (eq(x, value))
|
|
@@ -156,6 +157,11 @@ export class Deque extends Sequence {
|
|
|
156
157
|
}
|
|
157
158
|
return false;
|
|
158
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns the first element that matches the `iteratee` function.
|
|
162
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
163
|
+
* @returns {?T} The element if exists
|
|
164
|
+
*/
|
|
159
165
|
find(iteratee) {
|
|
160
166
|
for (const [key, value] of this.entries()) {
|
|
161
167
|
if (iteratee(value, key, this))
|
|
@@ -163,11 +169,8 @@ export class Deque extends Sequence {
|
|
|
163
169
|
}
|
|
164
170
|
}
|
|
165
171
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @param {function} iteratee - Function to call for each item.
|
|
169
|
-
* @param {object} scope - Optional scope.
|
|
170
|
-
* @return {undefined}
|
|
172
|
+
* Calls `iteratee` function for every element in the deque.
|
|
173
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
171
174
|
*/
|
|
172
175
|
forEach(iteratee) {
|
|
173
176
|
const c = this.capacity;
|
|
@@ -182,20 +185,23 @@ export class Deque extends Sequence {
|
|
|
182
185
|
i = 0;
|
|
183
186
|
}
|
|
184
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns a new array with all the elements in reverse order.
|
|
190
|
+
* @returns {Array<T>} The elements reversed
|
|
191
|
+
*/
|
|
185
192
|
reversed() {
|
|
186
193
|
return this.toArray().reverse();
|
|
187
194
|
}
|
|
188
195
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @return {array}
|
|
196
|
+
* Returns a new array with all the elements in the order they were added to the deque.
|
|
197
|
+
* @returns {Array<T>} The elements array
|
|
192
198
|
*/
|
|
193
199
|
toArray() {
|
|
194
200
|
// Optimization
|
|
195
201
|
const offset = this.start + this.count;
|
|
196
202
|
if (offset < this.capacity)
|
|
197
203
|
return this.items.slice(this.start, offset);
|
|
198
|
-
const array = new this.
|
|
204
|
+
const array = new this.ArrayConstructor(this.count);
|
|
199
205
|
const c = this.capacity;
|
|
200
206
|
const l = this.count;
|
|
201
207
|
let i = this.start;
|
|
@@ -210,9 +216,8 @@ export class Deque extends Sequence {
|
|
|
210
216
|
return array;
|
|
211
217
|
}
|
|
212
218
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @return {Iterator}
|
|
219
|
+
* Returns an iterator over the deque's values.
|
|
220
|
+
* @returns {Iterator}
|
|
216
221
|
*/
|
|
217
222
|
values() {
|
|
218
223
|
const items = this.items;
|
|
@@ -236,13 +241,15 @@ export class Deque extends Sequence {
|
|
|
236
241
|
};
|
|
237
242
|
});
|
|
238
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Alias of {@link Deque.values}. Returns iterator of values.
|
|
246
|
+
*/
|
|
239
247
|
keys() {
|
|
240
248
|
return this.values();
|
|
241
249
|
}
|
|
242
250
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* @return {Iterator}
|
|
251
|
+
* Creates an iterator of `[index, value]` pairs for all elements in the deque.
|
|
252
|
+
* @returns {Iterator}
|
|
246
253
|
*/
|
|
247
254
|
entries() {
|
|
248
255
|
const items = this.items;
|
|
@@ -271,9 +278,10 @@ export class Deque extends Sequence {
|
|
|
271
278
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
272
279
|
return this.inspect();
|
|
273
280
|
}
|
|
281
|
+
// This is just for debugging in nodejs
|
|
274
282
|
inspect() {
|
|
275
283
|
const array = this.toArray();
|
|
276
|
-
array.type = this.
|
|
284
|
+
array.type = this.ArrayConstructor.name;
|
|
277
285
|
array.capacity = this.capacity;
|
|
278
286
|
// Trick so that node displays the name of the constructor
|
|
279
287
|
Object.defineProperty(array, 'constructor', {
|
|
@@ -5,40 +5,44 @@ import { Sequence } from './abc.js'
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Fixed capacity double-ended queue implemented as ring deque.
|
|
8
|
-
*
|
|
9
|
-
* @constructor
|
|
8
|
+
* @template T
|
|
10
9
|
*/
|
|
11
|
-
export class Deque<T> extends Sequence<T> {
|
|
10
|
+
export class Deque<T = any> extends Sequence<T> {
|
|
12
11
|
items: T[]
|
|
13
12
|
start: number = 0
|
|
14
13
|
count: number = 0
|
|
15
14
|
capacity: number
|
|
16
|
-
|
|
15
|
+
ArrayConstructor: any
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new `Deque` instance of a fixed size.
|
|
19
|
+
* @param {Array} [arrType = []] The initial elements or array type to use (accepts any kind of typed array).
|
|
20
|
+
* @param {number} [capacity=1024] The fixed size of the deque. Defaults to `1024`.
|
|
21
|
+
*/
|
|
22
|
+
constructor(arrType = [] as T[], capacity = 1024) {
|
|
19
23
|
super()
|
|
20
24
|
if (typeof capacity !== 'number' || capacity <= 0)
|
|
21
25
|
throw new ValueError(`Deque capacity should be a positive integer.`)
|
|
22
26
|
|
|
23
|
-
this.
|
|
27
|
+
this.ArrayConstructor = arrType.constructor
|
|
24
28
|
this.capacity = capacity
|
|
25
|
-
this.items = new this.
|
|
29
|
+
this.items = new this.ArrayConstructor(arrType.length || this.capacity)
|
|
26
30
|
this.clear()
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
|
|
32
|
+
if (arrType.length) {
|
|
33
|
+
for (const item of arrType) {
|
|
29
34
|
this.append(item)
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
}
|
|
33
38
|
|
|
39
|
+
/** Returns the total number of elements in the deque. */
|
|
34
40
|
get size() {
|
|
35
41
|
return this.count
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
/**
|
|
39
45
|
* Method used to clear the structure.
|
|
40
|
-
*
|
|
41
|
-
* @return {undefined}
|
|
42
46
|
*/
|
|
43
47
|
clear() {
|
|
44
48
|
// Properties
|
|
@@ -48,19 +52,17 @@ export class Deque<T> extends Sequence<T> {
|
|
|
48
52
|
|
|
49
53
|
/**
|
|
50
54
|
* Alias of {@link Deque.append}
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
53
|
-
* @return {number} - Returns the new size of the deque.
|
|
55
|
+
* @param {T} item Item to append.
|
|
56
|
+
* @returns {number} Returns the new size of the deque.
|
|
54
57
|
*/
|
|
55
58
|
add(item) {
|
|
56
59
|
return this.append(item)
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
* @return {number} - Returns the new size of the deque.
|
|
63
|
+
* Appends a new element to the end of the deque.
|
|
64
|
+
* @param {T} item Item to append.
|
|
65
|
+
* @returns {number} Returns the new size of the deque.
|
|
64
66
|
*/
|
|
65
67
|
append(item) {
|
|
66
68
|
if (this.count === this.capacity) this.popleft()
|
|
@@ -73,10 +75,9 @@ export class Deque<T> extends Sequence<T> {
|
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @
|
|
79
|
-
* @return {number} - Returns the new size of the deque.
|
|
78
|
+
* Appends a new element to the beginning of the deque.
|
|
79
|
+
* @param {T} item Item to prepend.
|
|
80
|
+
* @returns {number} Returns the new size of the deque.
|
|
80
81
|
*/
|
|
81
82
|
appendleft(item) {
|
|
82
83
|
if (this.count === this.capacity) this.pop()
|
|
@@ -92,9 +93,8 @@ export class Deque<T> extends Sequence<T> {
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* @return {any} - Returns the popped item.
|
|
96
|
+
* Removes and returns the element at the end of the deque.
|
|
97
|
+
* @returns {?T} Returns the popped item.
|
|
98
98
|
*/
|
|
99
99
|
pop() {
|
|
100
100
|
if (this.count === 0) return
|
|
@@ -108,15 +108,15 @@ export class Deque<T> extends Sequence<T> {
|
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* Alias of {@link Deque.pop}
|
|
111
|
+
* @returns {?T} Returns the popped item.
|
|
111
112
|
*/
|
|
112
113
|
remove() {
|
|
113
114
|
return this.pop()
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @return {any} - Returns the shifted item.
|
|
118
|
+
* Removes and returns the element at the beginning of the deque.
|
|
119
|
+
* @returns {?T} Returns the removed deque element.
|
|
120
120
|
*/
|
|
121
121
|
popleft() {
|
|
122
122
|
if (this.count === 0) return
|
|
@@ -132,9 +132,8 @@ export class Deque<T> extends Sequence<T> {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* @return {any}
|
|
135
|
+
* Returns the element at the beginning of the deque.
|
|
136
|
+
* @returns {?T} The element if exists
|
|
138
137
|
*/
|
|
139
138
|
first() {
|
|
140
139
|
if (this.count === 0) return
|
|
@@ -143,10 +142,9 @@ export class Deque<T> extends Sequence<T> {
|
|
|
143
142
|
}
|
|
144
143
|
|
|
145
144
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* @
|
|
149
|
-
* @return {any}
|
|
145
|
+
* Returns the element at a specified index position in the deque.
|
|
146
|
+
* @param {number} index The given index
|
|
147
|
+
* @returns {?T} The element if exists
|
|
150
148
|
*/
|
|
151
149
|
get(index) {
|
|
152
150
|
if (this.count === 0) return
|
|
@@ -160,9 +158,8 @@ export class Deque<T> extends Sequence<T> {
|
|
|
160
158
|
}
|
|
161
159
|
|
|
162
160
|
/**
|
|
163
|
-
* Update the
|
|
164
|
-
* @param index The given index
|
|
165
|
-
* @param value The new value to set at `index`.
|
|
161
|
+
* Update the element at a given `index`.
|
|
162
|
+
* @param {number} index The given index
|
|
166
163
|
*/
|
|
167
164
|
set(index: number, value: T) {
|
|
168
165
|
if (index < 0) index += this.count
|
|
@@ -170,6 +167,11 @@ export class Deque<T> extends Sequence<T> {
|
|
|
170
167
|
this.items[index] = value
|
|
171
168
|
}
|
|
172
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Check if a given element is in the deque.
|
|
172
|
+
* @param {T} x The value to find
|
|
173
|
+
* @returns {boolean} `true` if the value is found
|
|
174
|
+
*/
|
|
173
175
|
contains(x) {
|
|
174
176
|
for (const value of this.values()) {
|
|
175
177
|
if (eq(x, value)) return true
|
|
@@ -177,6 +179,11 @@ export class Deque<T> extends Sequence<T> {
|
|
|
177
179
|
return false
|
|
178
180
|
}
|
|
179
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Returns the first element that matches the `iteratee` function.
|
|
184
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
185
|
+
* @returns {?T} The element if exists
|
|
186
|
+
*/
|
|
180
187
|
find(iteratee: Iteratee<T>) {
|
|
181
188
|
for (const [key, value] of this.entries()) {
|
|
182
189
|
if (iteratee(value, key, this)) return value
|
|
@@ -184,11 +191,8 @@ export class Deque<T> extends Sequence<T> {
|
|
|
184
191
|
}
|
|
185
192
|
|
|
186
193
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* @param {function} iteratee - Function to call for each item.
|
|
190
|
-
* @param {object} scope - Optional scope.
|
|
191
|
-
* @return {undefined}
|
|
194
|
+
* Calls `iteratee` function for every element in the deque.
|
|
195
|
+
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
192
196
|
*/
|
|
193
197
|
forEach(iteratee) {
|
|
194
198
|
const c = this.capacity
|
|
@@ -205,14 +209,17 @@ export class Deque<T> extends Sequence<T> {
|
|
|
205
209
|
}
|
|
206
210
|
}
|
|
207
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Returns a new array with all the elements in reverse order.
|
|
214
|
+
* @returns {Array<T>} The elements reversed
|
|
215
|
+
*/
|
|
208
216
|
reversed() {
|
|
209
217
|
return this.toArray().reverse()
|
|
210
218
|
}
|
|
211
219
|
|
|
212
220
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @return {array}
|
|
221
|
+
* Returns a new array with all the elements in the order they were added to the deque.
|
|
222
|
+
* @returns {Array<T>} The elements array
|
|
216
223
|
*/
|
|
217
224
|
toArray() {
|
|
218
225
|
// Optimization
|
|
@@ -220,7 +227,7 @@ export class Deque<T> extends Sequence<T> {
|
|
|
220
227
|
|
|
221
228
|
if (offset < this.capacity) return this.items.slice(this.start, offset)
|
|
222
229
|
|
|
223
|
-
const array = new this.
|
|
230
|
+
const array = new this.ArrayConstructor(this.count)
|
|
224
231
|
const c = this.capacity
|
|
225
232
|
const l = this.count
|
|
226
233
|
let i = this.start
|
|
@@ -238,9 +245,8 @@ export class Deque<T> extends Sequence<T> {
|
|
|
238
245
|
}
|
|
239
246
|
|
|
240
247
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* @return {Iterator}
|
|
248
|
+
* Returns an iterator over the deque's values.
|
|
249
|
+
* @returns {Iterator}
|
|
244
250
|
*/
|
|
245
251
|
values(): Iterable<T> {
|
|
246
252
|
const items = this.items
|
|
@@ -269,14 +275,16 @@ export class Deque<T> extends Sequence<T> {
|
|
|
269
275
|
})
|
|
270
276
|
}
|
|
271
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Alias of {@link Deque.values}. Returns iterator of values.
|
|
280
|
+
*/
|
|
272
281
|
keys() {
|
|
273
282
|
return this.values()
|
|
274
283
|
}
|
|
275
284
|
|
|
276
285
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* @return {Iterator}
|
|
286
|
+
* Creates an iterator of `[index, value]` pairs for all elements in the deque.
|
|
287
|
+
* @returns {Iterator}
|
|
280
288
|
*/
|
|
281
289
|
entries() {
|
|
282
290
|
const items = this.items
|
|
@@ -312,10 +320,11 @@ export class Deque<T> extends Sequence<T> {
|
|
|
312
320
|
return this.inspect()
|
|
313
321
|
}
|
|
314
322
|
|
|
323
|
+
// This is just for debugging in nodejs
|
|
315
324
|
inspect() {
|
|
316
325
|
const array = this.toArray()
|
|
317
326
|
|
|
318
|
-
array.type = this.
|
|
327
|
+
array.type = this.ArrayConstructor.name
|
|
319
328
|
array.capacity = this.capacity
|
|
320
329
|
|
|
321
330
|
// Trick so that node displays the name of the constructor
|
|
@@ -87,6 +87,7 @@ single(3) // => 1
|
|
|
87
87
|
export declare function once<T>(func: Function<T>): (...args: any[]) => T;
|
|
88
88
|
export declare type Maybe<T> = T | undefined;
|
|
89
89
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
90
|
+
export declare type Optional<T> = Maybe<T>;
|
|
90
91
|
/**
|
|
91
92
|
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
92
93
|
*
|
package/core/functools/index.ts
CHANGED
|
@@ -206,6 +206,8 @@ export type Maybe<T> = T | undefined
|
|
|
206
206
|
|
|
207
207
|
export type Result<T, E = any> = [Maybe<T>, Maybe<E>]
|
|
208
208
|
|
|
209
|
+
export type Optional<T> = Maybe<T>
|
|
210
|
+
|
|
209
211
|
/**
|
|
210
212
|
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
211
213
|
*
|
package/core/globals/index.d.ts
CHANGED
package/core/globals/index.js
CHANGED
package/core/globals/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare type DateParts = {
|
|
|
7
7
|
second?: number;
|
|
8
8
|
millisecond?: number;
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
declare type DateLike = number | string | Date | DateParts;
|
|
11
11
|
/**
|
|
12
12
|
* Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string compatible with `strftime`.
|
|
13
13
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
|
|
@@ -221,3 +221,4 @@ export declare function daysInMonth(year: number, month: number): number | null;
|
|
|
221
221
|
* @returns {number}
|
|
222
222
|
*/
|
|
223
223
|
export declare function weeksInYear(weekYear: number): 53 | 52;
|
|
224
|
+
export {};
|
package/core/i18n/date/index.ts
CHANGED
|
@@ -22,7 +22,7 @@ export type DateParts = {
|
|
|
22
22
|
millisecond?: number
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
type DateLike = number | string | Date | DateParts
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string compatible with `strftime`.
|
package/package.json
CHANGED
package/tests/globals.spec.ts
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
type,
|
|
35
35
|
uniqueId,
|
|
36
36
|
values,
|
|
37
|
-
|
|
37
|
+
noop
|
|
38
38
|
} from '../core/globals/index.js'
|
|
39
39
|
import { enumerate } from '../core/itertools/index.js'
|
|
40
40
|
|
|
@@ -74,7 +74,7 @@ describe('globals', () => {
|
|
|
74
74
|
assert(type(new WeakMap()) === 'WeakMap')
|
|
75
75
|
assert(type(new Set()) === 'Set')
|
|
76
76
|
assert(type(new WeakSet()) === 'WeakSet')
|
|
77
|
-
assert(type(new Promise(
|
|
77
|
+
assert(type(new Promise(noop)) === 'Promise')
|
|
78
78
|
assert(type(new Int8Array()) === 'Int8Array')
|
|
79
79
|
assert(type(new Uint8Array()) === 'Uint8Array')
|
|
80
80
|
assert(type(new Uint8ClampedArray()) === 'Uint8ClampedArray')
|
|
@@ -115,14 +115,14 @@ describe('globals', () => {
|
|
|
115
115
|
})
|
|
116
116
|
|
|
117
117
|
it('isFunction', () => {
|
|
118
|
-
assert(isFunction(
|
|
118
|
+
assert(isFunction(noop))
|
|
119
119
|
assert(!isFunction([]))
|
|
120
120
|
})
|
|
121
121
|
|
|
122
122
|
it('isAsyncFunction', () => {
|
|
123
123
|
assert(isAsyncFunction(async function () {}))
|
|
124
124
|
assert(isAsyncFunction(async () => {}))
|
|
125
|
-
assert(!isAsyncFunction(
|
|
125
|
+
assert(!isAsyncFunction(noop))
|
|
126
126
|
})
|
|
127
127
|
|
|
128
128
|
it('isNaN', () => {
|
|
@@ -143,7 +143,7 @@ describe('globals', () => {
|
|
|
143
143
|
})
|
|
144
144
|
|
|
145
145
|
it('isPromise', () => {
|
|
146
|
-
assert(isPromise(new Promise(
|
|
146
|
+
assert(isPromise(new Promise(noop)))
|
|
147
147
|
})
|
|
148
148
|
|
|
149
149
|
it('isSymbol', () => {
|