atomic-fns 0.3.1 → 0.3.3
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/.esdoc.json +2 -2
- package/README.md +48 -13
- package/core/collections/BTree.d.ts +229 -0
- package/core/collections/BTree.js +1165 -0
- package/core/collections/BTree.ts +1340 -0
- package/core/collections/Heap.d.ts +69 -0
- package/core/collections/Heap.js +181 -0
- package/core/collections/Heap.ts +189 -0
- package/core/collections/LRUCache.d.ts +131 -0
- package/core/collections/LRUCache.js +388 -0
- package/core/collections/LRUCache.ts +465 -0
- package/core/collections/SortedMap.d.ts +92 -0
- package/core/collections/SortedMap.js +130 -0
- package/core/collections/SortedMap.ts +146 -0
- package/core/collections/SortedSet.d.ts +63 -0
- package/core/collections/SortedSet.js +93 -0
- package/core/collections/SortedSet.ts +104 -0
- package/core/collections/SortedTree.d.ts +103 -0
- package/core/collections/SortedTree.js +141 -0
- package/core/collections/SortedTree.ts +160 -0
- package/core/collections/SplayTree.d.ts +105 -0
- package/core/collections/SplayTree.js +338 -0
- package/core/collections/SplayTree.ts +354 -0
- package/core/collections/abc.d.ts +152 -0
- package/core/collections/abc.js +39 -0
- package/core/collections/abc.ts +176 -0
- package/core/collections/deque.d.ts +110 -0
- package/core/collections/deque.js +285 -0
- package/core/collections/deque.ts +329 -0
- package/core/collections/frozenset.d.ts +19 -0
- package/core/collections/frozenset.js +33 -0
- package/core/collections/frozenset.ts +37 -0
- package/{src → core}/collections/index.d.ts +41 -162
- package/{src → core}/collections/index.js +57 -165
- package/{src → core}/collections/index.ts +61 -225
- package/core/collections/test.html +10 -0
- package/core/collections/typedArrays.d.ts +31 -0
- package/core/collections/typedArrays.js +143 -0
- package/core/collections/typedArrays.ts +163 -0
- package/core/datetime/index.d.ts +45 -0
- package/core/datetime/index.js +71 -0
- package/core/datetime/index.ts +128 -0
- package/{src → core}/functools/index.d.ts +2 -2
- package/{src → core}/functools/index.js +2 -2
- package/{src → core}/functools/index.ts +2 -2
- package/{src → core}/globals/index.d.ts +2 -7
- package/{src → core}/globals/index.js +5 -2
- package/{src → core}/globals/index.ts +6 -9
- package/{src → core}/index.d.ts +0 -1
- package/{src → core}/index.js +0 -1
- package/{src → core}/index.ts +0 -1
- package/{src → core}/itertools/index.d.ts +9 -7
- package/{src → core}/itertools/index.js +25 -9
- package/{src → core}/itertools/index.ts +39 -16
- package/{src → core}/math/index.d.ts +0 -0
- package/{src → core}/math/index.js +0 -0
- package/{src → core}/math/index.ts +0 -0
- package/core/operators/index.d.ts +148 -0
- package/core/operators/index.js +271 -0
- package/core/operators/index.ts +282 -0
- package/{src → core}/random/index.d.ts +0 -0
- package/{src → core}/random/index.js +0 -0
- package/{src → core}/random/index.ts +0 -0
- package/package.json +6 -6
- package/tests/BTree.spec.ts +143 -0
- package/tests/Cache.spec.ts +161 -0
- package/tests/Heap.spec.ts +66 -0
- package/tests/SortedMap.spec.ts +144 -0
- package/tests/SortedSet.spec.ts +84 -0
- package/tests/SortedTree.spec.ts +164 -0
- package/tests/SplayTree.spec.ts +121 -0
- package/tests/collections.spec.ts +1 -1
- package/tests/deque.spec.ts +107 -0
- package/tests/globals.spec.ts +2 -2
- package/tests/itertools.spec.ts +2 -2
- package/tests/math.spec.ts +1 -1
- package/tests/operators.spec.ts +5 -5
- package/tests/random.spec.ts +10 -10
- package/tsconfig.json +10 -10
- package/tsconfig.node.json +2 -2
- package/tsconfig.xo.json +2 -2
- package/src/datetime/index.d.ts +0 -6
- package/src/datetime/index.js +0 -6
- package/src/datetime/index.ts +0 -7
- package/src/operators/index.d.ts +0 -31
- package/src/operators/index.js +0 -171
- package/src/operators/index.ts +0 -175
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module includes classes and functions to work with Date types.
|
|
3
|
+
*
|
|
4
|
+
* @module Date
|
|
5
|
+
*/
|
|
6
|
+
export declare const MINUTE_SECONDS = 60;
|
|
7
|
+
export declare const HOUR_SECONDS: number;
|
|
8
|
+
export declare const DAY_SECONDS: number;
|
|
9
|
+
export declare const WEEK_SECONDS: number;
|
|
10
|
+
export declare const MILLISECONDS = 1000;
|
|
11
|
+
export declare const MINUTE_MILLISECONDS: number;
|
|
12
|
+
export declare const HOUR_MILLISECONDS: number;
|
|
13
|
+
export declare const DAY_MILLISECONDS: number;
|
|
14
|
+
export declare const WEEK_MILLISECONDS: number;
|
|
15
|
+
export declare const MS = "millisecond";
|
|
16
|
+
export declare const S = "second";
|
|
17
|
+
export declare const MIN = "minute";
|
|
18
|
+
export declare const H = "hour";
|
|
19
|
+
export declare const D = "day";
|
|
20
|
+
export declare const W = "week";
|
|
21
|
+
export declare const M = "month";
|
|
22
|
+
export declare const Q = "quarter";
|
|
23
|
+
export declare const Y = "year";
|
|
24
|
+
export declare const DATE = "date";
|
|
25
|
+
export declare const FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ssZ";
|
|
26
|
+
export declare const INVALID_DATE_STRING = "Invalid Date";
|
|
27
|
+
export declare const REGEX_PARSE: RegExp;
|
|
28
|
+
export declare const REGEX_FORMAT: RegExp;
|
|
29
|
+
export declare const now: number;
|
|
30
|
+
export declare type DatetimeOptions = {
|
|
31
|
+
year: number;
|
|
32
|
+
month?: number;
|
|
33
|
+
day?: number;
|
|
34
|
+
hour?: number;
|
|
35
|
+
minute?: number;
|
|
36
|
+
second?: number;
|
|
37
|
+
millisecond?: number;
|
|
38
|
+
locale?: string;
|
|
39
|
+
utc?: boolean;
|
|
40
|
+
};
|
|
41
|
+
export declare class Datetime {
|
|
42
|
+
#private;
|
|
43
|
+
constructor(opts?: string | number | Date | DatetimeOptions);
|
|
44
|
+
format(locale?: string, opts?: Intl.DateTimeFormatOptions): string;
|
|
45
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module includes classes and functions to work with Date types.
|
|
3
|
+
*
|
|
4
|
+
* @module Date
|
|
5
|
+
*/
|
|
6
|
+
import { get } from '../globals';
|
|
7
|
+
// Constants
|
|
8
|
+
export const MINUTE_SECONDS = 60;
|
|
9
|
+
export const HOUR_SECONDS = MINUTE_SECONDS * 60;
|
|
10
|
+
export const DAY_SECONDS = HOUR_SECONDS * 24;
|
|
11
|
+
export const WEEK_SECONDS = DAY_SECONDS * 7;
|
|
12
|
+
export const MILLISECONDS = 1e3;
|
|
13
|
+
export const MINUTE_MILLISECONDS = MINUTE_SECONDS * MILLISECONDS;
|
|
14
|
+
export const HOUR_MILLISECONDS = HOUR_SECONDS * MILLISECONDS;
|
|
15
|
+
export const DAY_MILLISECONDS = DAY_SECONDS * MILLISECONDS;
|
|
16
|
+
export const WEEK_MILLISECONDS = WEEK_SECONDS * MILLISECONDS;
|
|
17
|
+
// English locales
|
|
18
|
+
export const MS = 'millisecond';
|
|
19
|
+
export const S = 'second';
|
|
20
|
+
export const MIN = 'minute';
|
|
21
|
+
export const H = 'hour';
|
|
22
|
+
export const D = 'day';
|
|
23
|
+
export const W = 'week';
|
|
24
|
+
export const M = 'month';
|
|
25
|
+
export const Q = 'quarter';
|
|
26
|
+
export const Y = 'year';
|
|
27
|
+
export const DATE = 'date';
|
|
28
|
+
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ';
|
|
29
|
+
export const INVALID_DATE_STRING = 'Invalid Date';
|
|
30
|
+
// regex
|
|
31
|
+
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/;
|
|
32
|
+
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
|
|
33
|
+
export const now = new Date().getTime();
|
|
34
|
+
function parseDate(date) {
|
|
35
|
+
if (date === null)
|
|
36
|
+
return new Date(NaN);
|
|
37
|
+
if (!date)
|
|
38
|
+
return new Date();
|
|
39
|
+
if (date instanceof Date)
|
|
40
|
+
return new Date(date);
|
|
41
|
+
const utc = date.utc;
|
|
42
|
+
if (typeof date === 'string' && !/Z$/i.test(date)) {
|
|
43
|
+
const d = date.match(REGEX_PARSE);
|
|
44
|
+
if (d) {
|
|
45
|
+
const m = d[2] - 1 || 0;
|
|
46
|
+
const ms = (d[7] || '0').slice(0, 3);
|
|
47
|
+
if (utc) {
|
|
48
|
+
return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
|
|
49
|
+
}
|
|
50
|
+
return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return new Date(date);
|
|
54
|
+
}
|
|
55
|
+
export class Datetime {
|
|
56
|
+
#date = new Date(); // defaults to now
|
|
57
|
+
#UTC = false; // use current locale by default
|
|
58
|
+
constructor(opts) {
|
|
59
|
+
this.#date = parseDate(opts);
|
|
60
|
+
this.#UTC = get('utc', opts, false);
|
|
61
|
+
// this.init()
|
|
62
|
+
}
|
|
63
|
+
// Display
|
|
64
|
+
// Format
|
|
65
|
+
format(locale, opts) {
|
|
66
|
+
if (opts && opts.timeZone === undefined && this.#UTC) {
|
|
67
|
+
opts.timeZone = 'utc';
|
|
68
|
+
}
|
|
69
|
+
return this.#date.toLocaleDateString(locale, opts);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module includes classes and functions to work with Date types.
|
|
3
|
+
*
|
|
4
|
+
* @module Date
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { get } from '../globals'
|
|
8
|
+
|
|
9
|
+
// Constants
|
|
10
|
+
export const MINUTE_SECONDS = 60
|
|
11
|
+
export const HOUR_SECONDS = MINUTE_SECONDS * 60
|
|
12
|
+
export const DAY_SECONDS = HOUR_SECONDS * 24
|
|
13
|
+
export const WEEK_SECONDS = DAY_SECONDS * 7
|
|
14
|
+
|
|
15
|
+
export const MILLISECONDS = 1e3
|
|
16
|
+
export const MINUTE_MILLISECONDS = MINUTE_SECONDS * MILLISECONDS
|
|
17
|
+
export const HOUR_MILLISECONDS = HOUR_SECONDS * MILLISECONDS
|
|
18
|
+
export const DAY_MILLISECONDS = DAY_SECONDS * MILLISECONDS
|
|
19
|
+
export const WEEK_MILLISECONDS = WEEK_SECONDS * MILLISECONDS
|
|
20
|
+
|
|
21
|
+
// English locales
|
|
22
|
+
export const MS = 'millisecond'
|
|
23
|
+
export const S = 'second'
|
|
24
|
+
export const MIN = 'minute'
|
|
25
|
+
export const H = 'hour'
|
|
26
|
+
export const D = 'day'
|
|
27
|
+
export const W = 'week'
|
|
28
|
+
export const M = 'month'
|
|
29
|
+
export const Q = 'quarter'
|
|
30
|
+
export const Y = 'year'
|
|
31
|
+
export const DATE = 'date'
|
|
32
|
+
|
|
33
|
+
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
|
|
34
|
+
|
|
35
|
+
export const INVALID_DATE_STRING = 'Invalid Date'
|
|
36
|
+
|
|
37
|
+
// regex
|
|
38
|
+
export const REGEX_PARSE =
|
|
39
|
+
/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
|
|
40
|
+
export const REGEX_FORMAT =
|
|
41
|
+
/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
|
|
42
|
+
|
|
43
|
+
export const now = new Date().getTime()
|
|
44
|
+
|
|
45
|
+
export type DatetimeOptions = {
|
|
46
|
+
year: number
|
|
47
|
+
month?: number
|
|
48
|
+
day?: number
|
|
49
|
+
hour?: number
|
|
50
|
+
minute?: number
|
|
51
|
+
second?: number
|
|
52
|
+
millisecond?: number
|
|
53
|
+
locale?: string
|
|
54
|
+
utc?: boolean
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseDate(date) {
|
|
58
|
+
if (date === null) return new Date(NaN)
|
|
59
|
+
if (!date) return new Date()
|
|
60
|
+
if (date instanceof Date) return new Date(date)
|
|
61
|
+
const utc = date.utc
|
|
62
|
+
if (typeof date === 'string' && !/Z$/i.test(date)) {
|
|
63
|
+
const d = date.match(REGEX_PARSE) as any
|
|
64
|
+
if (d) {
|
|
65
|
+
const m = d[2] - 1 || 0
|
|
66
|
+
const ms = (d[7] || '0').slice(0, 3)
|
|
67
|
+
if (utc) {
|
|
68
|
+
return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
|
|
69
|
+
}
|
|
70
|
+
return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return new Date(date)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class Datetime {
|
|
77
|
+
#date = new Date() // defaults to now
|
|
78
|
+
#UTC = false // use current locale by default
|
|
79
|
+
|
|
80
|
+
constructor(opts?: string | number | Date | DatetimeOptions) {
|
|
81
|
+
this.#date = parseDate(opts)
|
|
82
|
+
this.#UTC = get('utc', opts, false)
|
|
83
|
+
// this.init()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Display
|
|
87
|
+
// Format
|
|
88
|
+
format(locale?: string, opts?: Intl.DateTimeFormatOptions) {
|
|
89
|
+
if (opts && opts.timeZone === undefined && this.#UTC) {
|
|
90
|
+
opts.timeZone = 'utc'
|
|
91
|
+
}
|
|
92
|
+
return this.#date.toLocaleDateString(locale, opts)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Time from now
|
|
96
|
+
// Time from X
|
|
97
|
+
// Difference
|
|
98
|
+
|
|
99
|
+
// Parsing
|
|
100
|
+
// Parse String + Date Format
|
|
101
|
+
// Parse String + Time Format
|
|
102
|
+
// Parse String + Format + locale
|
|
103
|
+
|
|
104
|
+
// Getter/Setters
|
|
105
|
+
// Hour/Minute/Second/Millis
|
|
106
|
+
// Day of Month
|
|
107
|
+
// Day of Week
|
|
108
|
+
// Day of Year
|
|
109
|
+
// Week of Year
|
|
110
|
+
// Days in Month
|
|
111
|
+
// Weeks in Year
|
|
112
|
+
// max of two dates
|
|
113
|
+
// min of two dates
|
|
114
|
+
|
|
115
|
+
// Manipulate
|
|
116
|
+
// Add
|
|
117
|
+
// Subtract
|
|
118
|
+
// Start of Time
|
|
119
|
+
// End of Time
|
|
120
|
+
|
|
121
|
+
// Query
|
|
122
|
+
// Is Before
|
|
123
|
+
// Is Same
|
|
124
|
+
// Is After
|
|
125
|
+
// Is Between
|
|
126
|
+
// Is Leap Year
|
|
127
|
+
// Is a Date
|
|
128
|
+
}
|
|
@@ -88,7 +88,7 @@ export declare function once(func: Function): (...args: any[]) => any;
|
|
|
88
88
|
export declare type Maybe<T> = T | undefined;
|
|
89
89
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
90
90
|
/**
|
|
91
|
-
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
91
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
92
92
|
*
|
|
93
93
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
94
94
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
@@ -101,7 +101,7 @@ export declare function result<T = any, E = unknown>(fun: Function<T>, onFinally
|
|
|
101
101
|
*
|
|
102
102
|
* @param {Function|Promise} awaitable The function or promise to await.
|
|
103
103
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
104
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a
|
|
104
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
105
105
|
* @template T,E
|
|
106
106
|
*/
|
|
107
107
|
export declare function resultAsync<T = any, E = unknown>(awaitable: Promise<T> | Function<Promise<T>>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Promise<Result<T, E>>;
|
|
@@ -171,7 +171,7 @@ export function once(func) {
|
|
|
171
171
|
};
|
|
172
172
|
}
|
|
173
173
|
/**
|
|
174
|
-
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
174
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
175
175
|
*
|
|
176
176
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
177
177
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
@@ -198,7 +198,7 @@ export function result(fun, onFinally) {
|
|
|
198
198
|
*
|
|
199
199
|
* @param {Function|Promise} awaitable The function or promise to await.
|
|
200
200
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
201
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a
|
|
201
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
202
202
|
* @template T,E
|
|
203
203
|
*/
|
|
204
204
|
export function resultAsync(awaitable, onFinally) {
|
|
@@ -204,7 +204,7 @@ export type Maybe<T> = T | undefined
|
|
|
204
204
|
export type Result<T, E = any> = [Maybe<T>, Maybe<E>]
|
|
205
205
|
|
|
206
206
|
/**
|
|
207
|
-
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
207
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
208
208
|
*
|
|
209
209
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
210
210
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
@@ -233,7 +233,7 @@ export function result<T = any, E = unknown>(
|
|
|
233
233
|
*
|
|
234
234
|
* @param {Function|Promise} awaitable The function or promise to await.
|
|
235
235
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
236
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a
|
|
236
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
237
237
|
* @template T,E
|
|
238
238
|
*/
|
|
239
239
|
export function resultAsync<T = any, E = unknown>(
|
|
@@ -3,11 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
|
-
/** Describes a function used to compare two values.
|
|
7
|
-
*
|
|
8
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
|
|
9
|
-
*/
|
|
10
|
-
export declare type Comp = (x: any, y: any) => -1 | 0 | 1;
|
|
11
6
|
/** This is just a noop function. */
|
|
12
7
|
export declare const _: () => void;
|
|
13
8
|
/**
|
|
@@ -27,7 +22,7 @@ export declare type Function<T = any> = (...args: any) => T;
|
|
|
27
22
|
* @see {@link Collections.sortedUniq}
|
|
28
23
|
* @see {@link Collections.groupBy}
|
|
29
24
|
*/
|
|
30
|
-
export declare type Iteratee<T = any> = (value: T, key?:
|
|
25
|
+
export declare type Iteratee<T = any, K = any> = (value: T, key?: K, arr?: any) => any;
|
|
31
26
|
export declare type Predicate<T = any> = (value: T) => any;
|
|
32
27
|
/** A function that always returns `true`. */
|
|
33
28
|
export declare const True: () => boolean;
|
|
@@ -130,7 +125,7 @@ export declare const isGenerator: (x: any) => x is Generator<unknown, any, unkno
|
|
|
130
125
|
export declare const isNull: (x: any) => x is null | undefined;
|
|
131
126
|
/** Check if value is not `null` or `undefined`. */
|
|
132
127
|
export declare const notNull: (x: any) => boolean;
|
|
133
|
-
/** Returns `true` for objects without length or
|
|
128
|
+
/** Returns `true` for objects without length or falsy values. */
|
|
134
129
|
export declare const isEmpty: (x: any) => boolean;
|
|
135
130
|
/** Check if value is a `Symbol` type */
|
|
136
131
|
export declare const isSymbol: (x: any) => x is symbol;
|
|
@@ -164,7 +164,7 @@ export const isGenerator = (x) => {
|
|
|
164
164
|
export const isNull = (x) => x == null;
|
|
165
165
|
/** Check if value is not `null` or `undefined`. */
|
|
166
166
|
export const notNull = (x) => x != null;
|
|
167
|
-
/** Returns `true` for objects without length or
|
|
167
|
+
/** Returns `true` for objects without length or falsy values. */
|
|
168
168
|
export const isEmpty = (x) => (len(x) === 0 ? true : !x);
|
|
169
169
|
/** Check if value is a `Symbol` type */
|
|
170
170
|
export const isSymbol = (x) => type(x) === 'symbol';
|
|
@@ -179,12 +179,15 @@ export const isSymbol = (x) => type(x) === 'symbol';
|
|
|
179
179
|
export function len(value) {
|
|
180
180
|
if (value == null)
|
|
181
181
|
return;
|
|
182
|
-
if (value.length
|
|
182
|
+
if (isNumber(value.length)) {
|
|
183
183
|
return value.length;
|
|
184
184
|
}
|
|
185
185
|
if (isFunc(value.size)) {
|
|
186
186
|
return value.size();
|
|
187
187
|
}
|
|
188
|
+
if (isNumber(value.size)) {
|
|
189
|
+
return value.size;
|
|
190
|
+
}
|
|
188
191
|
if (isObject(value)) {
|
|
189
192
|
return Object.keys(value).length;
|
|
190
193
|
}
|
|
@@ -4,12 +4,6 @@
|
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
/** Describes a function used to compare two values.
|
|
8
|
-
*
|
|
9
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
|
|
10
|
-
*/
|
|
11
|
-
export type Comp = (x, y) => -1 | 0 | 1
|
|
12
|
-
|
|
13
7
|
/** This is just a noop function. */
|
|
14
8
|
export const _ = () => {}
|
|
15
9
|
|
|
@@ -31,7 +25,7 @@ export type Function<T = any> = (...args: any) => T
|
|
|
31
25
|
* @see {@link Collections.sortedUniq}
|
|
32
26
|
* @see {@link Collections.groupBy}
|
|
33
27
|
*/
|
|
34
|
-
export type Iteratee<T = any> = (value: T, key
|
|
28
|
+
export type Iteratee<T = any, K = any> = (value: T, key?: K, arr?) => any
|
|
35
29
|
|
|
36
30
|
export type Predicate<T = any> = (value: T) => any
|
|
37
31
|
|
|
@@ -204,7 +198,7 @@ export const isNull = (x): x is null | undefined => x == null
|
|
|
204
198
|
/** Check if value is not `null` or `undefined`. */
|
|
205
199
|
export const notNull = (x) => x != null
|
|
206
200
|
|
|
207
|
-
/** Returns `true` for objects without length or
|
|
201
|
+
/** Returns `true` for objects without length or falsy values. */
|
|
208
202
|
export const isEmpty = (x) => (len(x) === 0 ? true : !x)
|
|
209
203
|
|
|
210
204
|
/** Check if value is a `Symbol` type */
|
|
@@ -220,12 +214,15 @@ export const isSymbol = (x): x is symbol => type(x) === 'symbol'
|
|
|
220
214
|
*/
|
|
221
215
|
export function len(value) {
|
|
222
216
|
if (value == null) return
|
|
223
|
-
if (value.length
|
|
217
|
+
if (isNumber(value.length)) {
|
|
224
218
|
return value.length
|
|
225
219
|
}
|
|
226
220
|
if (isFunc(value.size)) {
|
|
227
221
|
return value.size()
|
|
228
222
|
}
|
|
223
|
+
if (isNumber(value.size)) {
|
|
224
|
+
return value.size
|
|
225
|
+
}
|
|
229
226
|
if (isObject(value)) {
|
|
230
227
|
return Object.keys(value).length
|
|
231
228
|
}
|
package/{src → core}/index.d.ts
RENAMED
package/{src → core}/index.js
RENAMED
package/{src → core}/index.ts
RENAMED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Iterators
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import { add } from '../operators/index.js';
|
|
6
|
+
import { Function, Iteratee, Predicate } from '../globals/index.js';
|
|
7
|
+
import { add, Comparer } from '../operators/index.js';
|
|
8
8
|
/**
|
|
9
9
|
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
10
10
|
* @param iterable The iterable to accumulate
|
|
@@ -82,6 +82,8 @@ export declare function contains(collection: Iterable<any>, value: any): any;
|
|
|
82
82
|
* @returns Elements
|
|
83
83
|
*/
|
|
84
84
|
export declare function dropWhile<T>(iterable: Iterable<T>, predicate: Predicate): Generator<unknown, any, unknown>;
|
|
85
|
+
export declare function iforEach<T>(iterable: Iterable<T>, fun: Iteratee<T>): void;
|
|
86
|
+
export declare function IterableIterator<T = any>(next: any): Iterator<T, any, undefined> & Iterable<T>;
|
|
85
87
|
/**
|
|
86
88
|
* Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
|
|
87
89
|
* @template T
|
|
@@ -106,7 +108,7 @@ export declare function enumerate<T = any>(iterable: Iterable<T>, start?: number
|
|
|
106
108
|
*/
|
|
107
109
|
export declare function first<T>(iterable: Iterable<T>): any;
|
|
108
110
|
/**
|
|
109
|
-
* Creates a new generator that filters all
|
|
111
|
+
* Creates a new generator that filters all falsy and empty values from iterable.
|
|
110
112
|
* @param arr The array to compact
|
|
111
113
|
* @returns A new array with the filtered values.
|
|
112
114
|
* @example
|
|
@@ -120,7 +122,7 @@ export declare function icompact<T>(iterable: Iterable<T>): Generator<T, void, u
|
|
|
120
122
|
* Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
|
|
121
123
|
*/
|
|
122
124
|
export declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
|
|
123
|
-
export declare function imap<T>(iterable: Iterable<T>, mapFn: (x: T) =>
|
|
125
|
+
export declare function imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x: T) => TReturn): Generator<TReturn, void, unknown>;
|
|
124
126
|
/**
|
|
125
127
|
* Returns a generator flattening one level of nesting in a list of lists.
|
|
126
128
|
* @param iterables The iterable to flatten
|
|
@@ -187,13 +189,13 @@ export declare function reduce<T>(iterable: Iterable<T>, reducer: (prev: T, valu
|
|
|
187
189
|
export declare function reversed<T>(iterable: Iterable<T>): T[];
|
|
188
190
|
export declare function sorted(args: any[]): any[];
|
|
189
191
|
export declare function sorted(args: any[], reverse: boolean): any[];
|
|
190
|
-
export declare function sorted(args: any[], reverse: boolean, comp:
|
|
192
|
+
export declare function sorted(args: any[], reverse: boolean, comp: Comparer): any[];
|
|
191
193
|
export declare function sorted(args: any[], key: Function): any[];
|
|
192
194
|
export declare function sorted(args: any[], key: Function, reverse: boolean): any[];
|
|
193
195
|
export declare function sort(args: any[]): any[];
|
|
194
196
|
export declare function sort(args: any[], reverse: boolean): any[];
|
|
195
|
-
export declare function sort(args: any[], compareFn:
|
|
196
|
-
export declare function sort(args: any[], reverse: boolean, compareFn:
|
|
197
|
+
export declare function sort(args: any[], compareFn: Comparer): any[];
|
|
198
|
+
export declare function sort(args: any[], reverse: boolean, compareFn: Comparer): any[];
|
|
197
199
|
export declare function take<T>(n: number, iterable: Iterable<T>): unknown[];
|
|
198
200
|
/**
|
|
199
201
|
* Returns a generator that takes elements from the iterable as long as the predicate is `true`.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module Iterators
|
|
5
5
|
*/
|
|
6
6
|
import { call, isEmpty, isObject, notNull, ValueError } from '../globals/index.js';
|
|
7
|
-
import { add, bool,
|
|
7
|
+
import { add, bool, compare, eq, id } from '../operators/index.js';
|
|
8
8
|
/**
|
|
9
9
|
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
10
10
|
* @param iterable The iterable to accumulate
|
|
@@ -109,7 +109,7 @@ export function contains(collection, value) {
|
|
|
109
109
|
return includes;
|
|
110
110
|
return any(collection, (x) => eq(value, x));
|
|
111
111
|
}
|
|
112
|
-
const compKey = (
|
|
112
|
+
const compKey = (compare, key) => (x, y) => compare(key(x), key(y));
|
|
113
113
|
/**
|
|
114
114
|
* Returns a generator that drops elements from the iterable as long as the predicate is `true`; afterwards, returns every remaining element. Note, the generator does not produce any output until the predicate first becomes `false`.
|
|
115
115
|
* @param iterable The iterable to inspect.
|
|
@@ -134,6 +134,22 @@ export function* dropWhile(iterable, predicate) {
|
|
|
134
134
|
yield head.value;
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
+
export function iforEach(iterable, fun) {
|
|
138
|
+
for (const [index, value] of enumerate(iterable)) {
|
|
139
|
+
if (fun(value, index, iterable) === false) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export function IterableIterator(next) {
|
|
145
|
+
const it = {
|
|
146
|
+
next,
|
|
147
|
+
[Symbol.iterator]() {
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return it;
|
|
152
|
+
}
|
|
137
153
|
/**
|
|
138
154
|
* Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
|
|
139
155
|
* @template T
|
|
@@ -165,7 +181,7 @@ export function first(iterable) {
|
|
|
165
181
|
return iter(iterable).next().value;
|
|
166
182
|
}
|
|
167
183
|
/**
|
|
168
|
-
* Creates a new generator that filters all
|
|
184
|
+
* Creates a new generator that filters all falsy and empty values from iterable.
|
|
169
185
|
* @param arr The array to compact
|
|
170
186
|
* @returns A new array with the filtered values.
|
|
171
187
|
* @example
|
|
@@ -350,8 +366,8 @@ export function reversed(iterable) {
|
|
|
350
366
|
*
|
|
351
367
|
* @param {(Array | Object)} args
|
|
352
368
|
* @param {(boolean | Function)} [key]
|
|
353
|
-
* @param {(boolean |
|
|
354
|
-
* @param {
|
|
369
|
+
* @param {(boolean | Comparer)} [reverse]
|
|
370
|
+
* @param {Comparer} [compareFn]
|
|
355
371
|
* @return
|
|
356
372
|
*/
|
|
357
373
|
export function sorted(args, key, reverse, compareFn) {
|
|
@@ -368,7 +384,7 @@ export function sorted(args, key, reverse, compareFn) {
|
|
|
368
384
|
key = id;
|
|
369
385
|
}
|
|
370
386
|
const copy = Array.from(args);
|
|
371
|
-
const _compare = compKey(compareFn ||
|
|
387
|
+
const _compare = compKey(compareFn || compare, key || id);
|
|
372
388
|
copy.sort(_compare);
|
|
373
389
|
if (reverse)
|
|
374
390
|
copy.reverse();
|
|
@@ -381,8 +397,8 @@ export function sorted(args, key, reverse, compareFn) {
|
|
|
381
397
|
* - sort([...], true, (x, y) => number) => reverse and using custom compare
|
|
382
398
|
*
|
|
383
399
|
* @param {any[]} args
|
|
384
|
-
* @param {(boolean |
|
|
385
|
-
* @param {
|
|
400
|
+
* @param {(boolean | Comparer)} [reverse]
|
|
401
|
+
* @param {Comparer} [compareFn]
|
|
386
402
|
* @return
|
|
387
403
|
*/
|
|
388
404
|
export function sort(args, reverse, compareFn) {
|
|
@@ -390,7 +406,7 @@ export function sort(args, reverse, compareFn) {
|
|
|
390
406
|
compareFn = reverse;
|
|
391
407
|
reverse = false;
|
|
392
408
|
}
|
|
393
|
-
args.sort(compareFn ||
|
|
409
|
+
args.sort(compareFn || compare);
|
|
394
410
|
if (reverse === true)
|
|
395
411
|
args.reverse();
|
|
396
412
|
return args;
|