@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.
@@ -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
  *
@@ -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.reduce((map, item, index) => {
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 type { Inclusiveness, LocalDateInput, LocalDateUnit } from './localDate';
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
- * _inRange(-10, 1, 5) // false
21
- * _inRange(1, 1, 5) // true
22
- * _inRange(3, 1, 5) // true
23
- * _inRange(5, 1, 5) // false
24
- * _inRange(7, 1, 5) // false
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 _inRange(x: number, minIncl: number, maxExcl: number): boolean;
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._inRange = exports._runLessOften = exports._randomArrayItem = exports._randomInt = void 0;
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
- * _inRange(-10, 1, 5) // false
33
- * _inRange(1, 1, 5) // true
34
- * _inRange(3, 1, 5) // true
35
- * _inRange(5, 1, 5) // false
36
- * _inRange(7, 1, 5) // false
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 _inRange(x, minIncl, maxExcl) {
39
- return x >= minIncl && x < maxExcl;
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._inRange = _inRange;
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.reduce((map, item, index) => {
111
- const res = mapper(item, index);
112
- if (res !== undefined)
113
- map[res] = item;
114
- return map;
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
- * _inRange(-10, 1, 5) // false
27
- * _inRange(1, 1, 5) // true
28
- * _inRange(3, 1, 5) // true
29
- * _inRange(5, 1, 5) // false
30
- * _inRange(7, 1, 5) // false
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 _inRange(x, minIncl, maxExcl) {
33
- return x >= minIncl && x < maxExcl;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.193.0",
3
+ "version": "14.195.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -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 items.reduce((map, item, index) => {
118
- const res = mapper(item, index)
119
- if (res !== undefined) map[res] = item
120
- return map
121
- }, {} as StringMap<T>)
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 type { Inclusiveness, LocalDateInput, LocalDateUnit } from './localDate'
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,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
 
@@ -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
- * _inRange(-10, 1, 5) // false
33
- * _inRange(1, 1, 5) // true
34
- * _inRange(3, 1, 5) // true
35
- * _inRange(5, 1, 5) // false
36
- * _inRange(7, 1, 5) // false
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 _inRange(x: number, minIncl: number, maxExcl: number): boolean {
39
- return x >= minIncl && x < maxExcl
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 = '()' | '[]' | '[)' | '(]'