@naturalcycles/js-lib 14.192.1 → 14.194.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.
@@ -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.
@@ -7,13 +7,23 @@ export declare function _randomInt(minIncl: number, maxIncl: number): number;
7
7
  */
8
8
  export declare function _randomArrayItem<T>(array: T[]): T;
9
9
  /**
10
- * _inRange(-10, 1, 5) // false
11
- * _inRange(1, 1, 5) // true
12
- * _inRange(3, 1, 5) // true
13
- * _inRange(5, 1, 5) // false
14
- * _inRange(7, 1, 5) // false
10
+ * Convenience function to "throttle" some code - run it less often.
11
+ *
12
+ * @example
13
+ *
14
+ * if (_runLessOften(10)) {
15
+ * // this code will run only 10% of the time
16
+ * }
17
+ */
18
+ export declare function _runLessOften(percent: number): boolean;
19
+ /**
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
15
25
  */
16
- 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;
17
27
  export declare function _clamp(x: number, minIncl: number, maxIncl: number): number;
18
28
  /**
19
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._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
  }
@@ -14,18 +14,41 @@ function _randomArrayItem(array) {
14
14
  return array[_randomInt(0, array.length - 1)];
15
15
  }
16
16
  exports._randomArrayItem = _randomArrayItem;
17
+ /**
18
+ * Convenience function to "throttle" some code - run it less often.
19
+ *
20
+ * @example
21
+ *
22
+ * if (_runLessOften(10)) {
23
+ * // this code will run only 10% of the time
24
+ * }
25
+ */
26
+ function _runLessOften(percent) {
27
+ return Math.random() * 100 < percent;
28
+ }
29
+ exports._runLessOften = _runLessOften;
17
30
  // todo: _.random to support floats
18
31
  /**
19
- * _inRange(-10, 1, 5) // false
20
- * _inRange(1, 1, 5) // true
21
- * _inRange(3, 1, 5) // true
22
- * _inRange(5, 1, 5) // false
23
- * _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
24
37
  */
25
- function _inRange(x, minIncl, maxExcl) {
26
- 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;
27
50
  }
28
- exports._inRange = _inRange;
51
+ exports._isBetween = _isBetween;
29
52
  function _clamp(x, minIncl, maxIncl) {
30
53
  return x <= minIncl ? minIncl : x >= maxIncl ? maxIncl : x;
31
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 = '()' | '[]' | '[)' | '(]';
@@ -9,16 +9,38 @@ export function _randomInt(minIncl, maxIncl) {
9
9
  export function _randomArrayItem(array) {
10
10
  return array[_randomInt(0, array.length - 1)];
11
11
  }
12
+ /**
13
+ * Convenience function to "throttle" some code - run it less often.
14
+ *
15
+ * @example
16
+ *
17
+ * if (_runLessOften(10)) {
18
+ * // this code will run only 10% of the time
19
+ * }
20
+ */
21
+ export function _runLessOften(percent) {
22
+ return Math.random() * 100 < percent;
23
+ }
12
24
  // todo: _.random to support floats
13
25
  /**
14
- * _inRange(-10, 1, 5) // false
15
- * _inRange(1, 1, 5) // true
16
- * _inRange(3, 1, 5) // true
17
- * _inRange(5, 1, 5) // false
18
- * _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
19
31
  */
20
- export function _inRange(x, minIncl, maxExcl) {
21
- 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;
22
44
  }
23
45
  export function _clamp(x, minIncl, maxIncl) {
24
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.192.1",
3
+ "version": "14.194.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -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)
@@ -13,17 +13,43 @@ export function _randomArrayItem<T>(array: T[]): T {
13
13
  return array[_randomInt(0, array.length - 1)]!
14
14
  }
15
15
 
16
+ /**
17
+ * Convenience function to "throttle" some code - run it less often.
18
+ *
19
+ * @example
20
+ *
21
+ * if (_runLessOften(10)) {
22
+ * // this code will run only 10% of the time
23
+ * }
24
+ */
25
+ export function _runLessOften(percent: number): boolean {
26
+ return Math.random() * 100 < percent
27
+ }
28
+
16
29
  // todo: _.random to support floats
17
30
 
18
31
  /**
19
- * _inRange(-10, 1, 5) // false
20
- * _inRange(1, 1, 5) // true
21
- * _inRange(3, 1, 5) // true
22
- * _inRange(5, 1, 5) // false
23
- * _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
24
37
  */
25
- export function _inRange(x: number, minIncl: number, maxExcl: number): boolean {
26
- 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
27
53
  }
28
54
 
29
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 = '()' | '[]' | '[)' | '(]'