@naturalcycles/js-lib 14.193.0 → 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.
@@ -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 = '()' | '[]' | '[)' | '(]';
@@ -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.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)
@@ -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 = '()' | '[]' | '[)' | '(]'