@naturalcycles/js-lib 14.193.0 → 14.195.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/array/array.util.d.ts +4 -0
- package/dist/array/array.util.js +9 -7
- package/dist/datetime/dateInterval.d.ts +2 -1
- package/dist/datetime/localDate.d.ts +1 -2
- package/dist/datetime/localTime.d.ts +1 -2
- package/dist/datetime/timeInterval.d.ts +1 -2
- package/dist/number/number.util.d.ts +7 -7
- package/dist/number/number.util.js +19 -9
- package/dist/types.d.ts +1 -0
- package/dist-esm/array/array.util.js +7 -6
- package/dist-esm/number/number.util.js +17 -7
- package/package.json +1 -1
- package/src/array/array.util.ts +18 -5
- package/src/datetime/dateInterval.ts +2 -1
- package/src/datetime/localDate.ts +1 -1
- package/src/datetime/localTime.ts +1 -1
- package/src/datetime/timeInterval.ts +1 -2
- package/src/number/number.util.ts +21 -8
- package/src/types.ts +2 -0
|
@@ -74,6 +74,10 @@ export declare function _uniqBy<T>(arr: readonly T[], mapper: Mapper<T, any>): T
|
|
|
74
74
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
75
75
|
*/
|
|
76
76
|
export declare function _by<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T>;
|
|
77
|
+
/**
|
|
78
|
+
* Map an array of items by a key, that is calculated by a Mapper.
|
|
79
|
+
*/
|
|
80
|
+
export declare function _mapBy<ITEM, KEY>(items: readonly ITEM[], mapper: Mapper<ITEM, KEY>): Map<KEY, ITEM>;
|
|
77
81
|
/**
|
|
78
82
|
* const a = [1, 2, 3, 4, 5]
|
|
79
83
|
*
|
package/dist/array/array.util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
|
|
3
|
+
exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._mapBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
|
|
4
4
|
const is_util_1 = require("../is.util");
|
|
5
5
|
/**
|
|
6
6
|
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
|
|
@@ -115,14 +115,16 @@ exports._uniqBy = _uniqBy;
|
|
|
115
115
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
116
116
|
*/
|
|
117
117
|
function _by(items, mapper) {
|
|
118
|
-
return items.
|
|
119
|
-
const res = mapper(item, index);
|
|
120
|
-
if (res !== undefined)
|
|
121
|
-
map[res] = item;
|
|
122
|
-
return map;
|
|
123
|
-
}, {});
|
|
118
|
+
return Object.fromEntries(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
|
|
124
119
|
}
|
|
125
120
|
exports._by = _by;
|
|
121
|
+
/**
|
|
122
|
+
* Map an array of items by a key, that is calculated by a Mapper.
|
|
123
|
+
*/
|
|
124
|
+
function _mapBy(items, mapper) {
|
|
125
|
+
return new Map(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
|
|
126
|
+
}
|
|
127
|
+
exports._mapBy = _mapBy;
|
|
126
128
|
/**
|
|
127
129
|
* const a = [1, 2, 3, 4, 5]
|
|
128
130
|
*
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Inclusiveness } from '../types';
|
|
2
|
+
import type { LocalDateInput, LocalDateUnit } from './localDate';
|
|
2
3
|
import { LocalDate } from './localDate';
|
|
3
4
|
export type DateIntervalConfig = DateInterval | DateIntervalString;
|
|
4
5
|
export type DateIntervalString = string;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Iterable2 } from '../iter/iterable2';
|
|
2
|
-
import type { IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
2
|
+
import type { Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
3
3
|
import { LocalTime } from './localTime';
|
|
4
4
|
export type LocalDateUnit = LocalDateUnitStrict | 'week';
|
|
5
5
|
export type LocalDateUnitStrict = 'year' | 'month' | 'day';
|
|
6
|
-
export type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
7
6
|
export type LocalDateInput = LocalDate | Date | IsoDateString;
|
|
8
7
|
export type LocalDateFormatter = (ld: LocalDate) => string;
|
|
9
8
|
/**
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
2
|
-
import type { Inclusiveness } from './localDate';
|
|
1
|
+
import type { Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
3
2
|
import { LocalDate } from './localDate';
|
|
4
3
|
export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
|
|
5
4
|
export declare enum ISODayOfWeek {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { UnixTimestampNumber } from '../types';
|
|
2
|
-
import type { Inclusiveness } from './localDate';
|
|
1
|
+
import type { UnixTimestampNumber, Inclusiveness } from '../types';
|
|
3
2
|
import type { LocalTimeInput } from './localTime';
|
|
4
3
|
import { LocalTime } from './localTime';
|
|
5
4
|
export type TimeIntervalConfig = TimeInterval | TimeIntervalString;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SortDirection } from '../types';
|
|
1
|
+
import type { Inclusiveness, SortDirection } from '../types';
|
|
2
2
|
export declare function _randomInt(minIncl: number, maxIncl: number): number;
|
|
3
3
|
/**
|
|
4
4
|
* Returns random item from an array.
|
|
@@ -17,13 +17,13 @@ export declare function _randomArrayItem<T>(array: T[]): T;
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function _runLessOften(percent: number): boolean;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
20
|
+
* _isBetween(-10, 1, 5) // false
|
|
21
|
+
* _isBetween(1, 1, 5) // true
|
|
22
|
+
* _isBetween(3, 1, 5) // true
|
|
23
|
+
* _isBetween(5, 1, 5) // false
|
|
24
|
+
* _isBetween(7, 1, 5) // false
|
|
25
25
|
*/
|
|
26
|
-
export declare function
|
|
26
|
+
export declare function _isBetween(x: number, min: number, max: number, incl?: Inclusiveness): boolean;
|
|
27
27
|
export declare function _clamp(x: number, minIncl: number, maxIncl: number): number;
|
|
28
28
|
/**
|
|
29
29
|
* This function exists, because in JS you cannot just .sort() numbers,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._clamp = exports.
|
|
3
|
+
exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._clamp = exports._isBetween = exports._runLessOften = exports._randomArrayItem = exports._randomInt = void 0;
|
|
4
4
|
function _randomInt(minIncl, maxIncl) {
|
|
5
5
|
return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl);
|
|
6
6
|
}
|
|
@@ -29,16 +29,26 @@ function _runLessOften(percent) {
|
|
|
29
29
|
exports._runLessOften = _runLessOften;
|
|
30
30
|
// todo: _.random to support floats
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
32
|
+
* _isBetween(-10, 1, 5) // false
|
|
33
|
+
* _isBetween(1, 1, 5) // true
|
|
34
|
+
* _isBetween(3, 1, 5) // true
|
|
35
|
+
* _isBetween(5, 1, 5) // false
|
|
36
|
+
* _isBetween(7, 1, 5) // false
|
|
37
37
|
*/
|
|
38
|
-
function
|
|
39
|
-
|
|
38
|
+
function _isBetween(x, min, max, incl = '[)') {
|
|
39
|
+
if (incl === '[)') {
|
|
40
|
+
return x >= min && x < max;
|
|
41
|
+
}
|
|
42
|
+
else if (incl === '[]') {
|
|
43
|
+
return x >= min && x <= max;
|
|
44
|
+
}
|
|
45
|
+
else if (incl === '(]') {
|
|
46
|
+
return x > min && x <= max;
|
|
47
|
+
}
|
|
48
|
+
// ()
|
|
49
|
+
return x > min && x < max;
|
|
40
50
|
}
|
|
41
|
-
exports.
|
|
51
|
+
exports._isBetween = _isBetween;
|
|
42
52
|
function _clamp(x, minIncl, maxIncl) {
|
|
43
53
|
return x <= minIncl ? minIncl : x >= maxIncl ? maxIncl : x;
|
|
44
54
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -243,3 +243,4 @@ export declare const _objectAssign: <T extends AnyObject>(target: T, part: Parti
|
|
|
243
243
|
*/
|
|
244
244
|
export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [err: ERR, data: null];
|
|
245
245
|
export type SortDirection = 'asc' | 'desc';
|
|
246
|
+
export type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
@@ -107,12 +107,13 @@ export function _uniqBy(arr, mapper) {
|
|
|
107
107
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
108
108
|
*/
|
|
109
109
|
export function _by(items, mapper) {
|
|
110
|
-
return items.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
return Object.fromEntries(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Map an array of items by a key, that is calculated by a Mapper.
|
|
114
|
+
*/
|
|
115
|
+
export function _mapBy(items, mapper) {
|
|
116
|
+
return new Map(items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined));
|
|
116
117
|
}
|
|
117
118
|
/**
|
|
118
119
|
* const a = [1, 2, 3, 4, 5]
|
|
@@ -23,14 +23,24 @@ export function _runLessOften(percent) {
|
|
|
23
23
|
}
|
|
24
24
|
// todo: _.random to support floats
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
26
|
+
* _isBetween(-10, 1, 5) // false
|
|
27
|
+
* _isBetween(1, 1, 5) // true
|
|
28
|
+
* _isBetween(3, 1, 5) // true
|
|
29
|
+
* _isBetween(5, 1, 5) // false
|
|
30
|
+
* _isBetween(7, 1, 5) // false
|
|
31
31
|
*/
|
|
32
|
-
export function
|
|
33
|
-
|
|
32
|
+
export function _isBetween(x, min, max, incl = '[)') {
|
|
33
|
+
if (incl === '[)') {
|
|
34
|
+
return x >= min && x < max;
|
|
35
|
+
}
|
|
36
|
+
else if (incl === '[]') {
|
|
37
|
+
return x >= min && x <= max;
|
|
38
|
+
}
|
|
39
|
+
else if (incl === '(]') {
|
|
40
|
+
return x > min && x <= max;
|
|
41
|
+
}
|
|
42
|
+
// ()
|
|
43
|
+
return x > min && x < max;
|
|
34
44
|
}
|
|
35
45
|
export function _clamp(x, minIncl, maxIncl) {
|
|
36
46
|
return x <= minIncl ? minIncl : x >= maxIncl ? maxIncl : x;
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -114,11 +114,24 @@ export function _uniqBy<T>(arr: readonly T[], mapper: Mapper<T, any>): T[] {
|
|
|
114
114
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
115
115
|
*/
|
|
116
116
|
export function _by<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T> {
|
|
117
|
-
return
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
117
|
+
return Object.fromEntries(
|
|
118
|
+
items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined) as [any, T][],
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Map an array of items by a key, that is calculated by a Mapper.
|
|
124
|
+
*/
|
|
125
|
+
export function _mapBy<ITEM, KEY>(
|
|
126
|
+
items: readonly ITEM[],
|
|
127
|
+
mapper: Mapper<ITEM, KEY>,
|
|
128
|
+
): Map<KEY, ITEM> {
|
|
129
|
+
return new Map(
|
|
130
|
+
items.map((item, i) => [mapper(item, i), item]).filter(([k]) => k !== undefined) as [
|
|
131
|
+
KEY,
|
|
132
|
+
ITEM,
|
|
133
|
+
][],
|
|
134
|
+
)
|
|
122
135
|
}
|
|
123
136
|
|
|
124
137
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Inclusiveness } from '../types'
|
|
2
|
+
import type { LocalDateInput, LocalDateUnit } from './localDate'
|
|
2
3
|
import { LocalDate, localDateRange } from './localDate'
|
|
3
4
|
|
|
4
5
|
export type DateIntervalConfig = DateInterval | DateIntervalString
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
2
|
import { Iterable2 } from '../iter/iterable2'
|
|
3
3
|
import type {
|
|
4
|
+
Inclusiveness,
|
|
4
5
|
IsoDateString,
|
|
5
6
|
IsoDateTimeString,
|
|
6
7
|
MonthId,
|
|
@@ -12,7 +13,6 @@ import { LocalTime } from './localTime'
|
|
|
12
13
|
|
|
13
14
|
export type LocalDateUnit = LocalDateUnitStrict | 'week'
|
|
14
15
|
export type LocalDateUnitStrict = 'year' | 'month' | 'day'
|
|
15
|
-
export type Inclusiveness = '()' | '[]' | '[)' | '(]'
|
|
16
16
|
|
|
17
17
|
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
18
18
|
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
2
|
import { _ms } from '../time/time.util'
|
|
3
3
|
import type {
|
|
4
|
+
Inclusiveness,
|
|
4
5
|
IsoDateString,
|
|
5
6
|
IsoDateTimeString,
|
|
6
7
|
MonthId,
|
|
@@ -8,7 +9,6 @@ import type {
|
|
|
8
9
|
UnixTimestampMillisNumber,
|
|
9
10
|
UnixTimestampNumber,
|
|
10
11
|
} from '../types'
|
|
11
|
-
import type { Inclusiveness } from './localDate'
|
|
12
12
|
import { LocalDate } from './localDate'
|
|
13
13
|
|
|
14
14
|
export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SortDirection } from '../types'
|
|
1
|
+
import type { Inclusiveness, SortDirection } from '../types'
|
|
2
2
|
|
|
3
3
|
export function _randomInt(minIncl: number, maxIncl: number): number {
|
|
4
4
|
return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl)
|
|
@@ -29,14 +29,27 @@ export function _runLessOften(percent: number): boolean {
|
|
|
29
29
|
// todo: _.random to support floats
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
32
|
+
* _isBetween(-10, 1, 5) // false
|
|
33
|
+
* _isBetween(1, 1, 5) // true
|
|
34
|
+
* _isBetween(3, 1, 5) // true
|
|
35
|
+
* _isBetween(5, 1, 5) // false
|
|
36
|
+
* _isBetween(7, 1, 5) // false
|
|
37
37
|
*/
|
|
38
|
-
export function
|
|
39
|
-
|
|
38
|
+
export function _isBetween(
|
|
39
|
+
x: number,
|
|
40
|
+
min: number,
|
|
41
|
+
max: number,
|
|
42
|
+
incl: Inclusiveness = '[)',
|
|
43
|
+
): boolean {
|
|
44
|
+
if (incl === '[)') {
|
|
45
|
+
return x >= min && x < max
|
|
46
|
+
} else if (incl === '[]') {
|
|
47
|
+
return x >= min && x <= max
|
|
48
|
+
} else if (incl === '(]') {
|
|
49
|
+
return x > min && x <= max
|
|
50
|
+
}
|
|
51
|
+
// ()
|
|
52
|
+
return x > min && x < max
|
|
40
53
|
}
|
|
41
54
|
|
|
42
55
|
export function _clamp(x: number, minIncl: number, maxIncl: number): number {
|
package/src/types.ts
CHANGED
|
@@ -326,3 +326,5 @@ export const _objectAssign = Object.assign as <T extends AnyObject>(
|
|
|
326
326
|
export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [err: ERR, data: null]
|
|
327
327
|
|
|
328
328
|
export type SortDirection = 'asc' | 'desc'
|
|
329
|
+
|
|
330
|
+
export type Inclusiveness = '()' | '[]' | '[)' | '(]'
|