@naturalcycles/js-lib 14.192.0 → 14.193.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.
@@ -66,13 +66,13 @@ class DateInterval {
66
66
  return this.start.cmp(d.start) || this.end.cmp(d.end);
67
67
  }
68
68
  getDays(incl = '[]') {
69
- return localDate_1.LocalDate.range(this.start, this.end, incl, 1, 'day');
69
+ return (0, localDate_1.localDateRange)(this.start, this.end, incl, 1, 'day');
70
70
  }
71
71
  /**
72
72
  * Returns an array of LocalDates that are included in the interval.
73
73
  */
74
74
  range(incl = '[]', step = 1, stepUnit = 'day') {
75
- return localDate_1.LocalDate.range(this.start, this.end, incl, step, stepUnit);
75
+ return (0, localDate_1.localDateRange)(this.start, this.end, incl, step, stepUnit);
76
76
  }
77
77
  toString() {
78
78
  return [this.start, this.end].join('/');
@@ -35,11 +35,6 @@ export declare class LocalDate {
35
35
  static earliest(items: LocalDateInput[]): LocalDate;
36
36
  static latestOrUndefined(items: LocalDateInput[]): LocalDate | undefined;
37
37
  static latest(items: LocalDateInput[]): LocalDate;
38
- static range(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
39
- /**
40
- * Experimental, returns the range as Iterable2.
41
- */
42
- static rangeIt(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
43
38
  get(unit: LocalDateUnitStrict): number;
44
39
  set(unit: LocalDateUnitStrict, v: number, mutate?: boolean): LocalDate;
45
40
  year(): number;
@@ -135,6 +130,11 @@ export declare class LocalDate {
135
130
  toJSON(): IsoDateString;
136
131
  format(fmt: Intl.DateTimeFormat | LocalDateFormatter): string;
137
132
  }
133
+ export declare function localDateRange(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
134
+ /**
135
+ * Experimental, returns the range as Iterable2.
136
+ */
137
+ export declare function localDateRangeIt(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
138
138
  /**
139
139
  * Convenience wrapper around `LocalDate.of`
140
140
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.LocalDate = void 0;
3
+ exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIt = exports.localDateRange = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
5
  const iterable2_1 = require("../iter/iterable2");
6
6
  const localTime_1 = require("./localTime");
@@ -104,39 +104,6 @@ class LocalDate {
104
104
  .map(i => LocalDate.of(i))
105
105
  .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
106
106
  }
107
- static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
108
- return this.rangeIt(min, max, incl, step, stepUnit).toArray();
109
- }
110
- /**
111
- * Experimental, returns the range as Iterable2.
112
- */
113
- static rangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
114
- if (stepUnit === 'week') {
115
- step *= 7;
116
- stepUnit = 'day';
117
- }
118
- const $min = LocalDate.of(min).startOf(stepUnit);
119
- const $max = LocalDate.of(max).startOf(stepUnit);
120
- let value = $min;
121
- // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
122
- if (value.isAfter($min, incl[0] === '[')) {
123
- // ok
124
- }
125
- else {
126
- value.plus(1, stepUnit, true);
127
- }
128
- const rightInclusive = incl[1] === ']';
129
- return iterable2_1.Iterable2.of({
130
- *[Symbol.iterator]() {
131
- while (value.isBefore($max, rightInclusive)) {
132
- yield value;
133
- // We don't mutate, because we already returned `current`
134
- // in the previous iteration
135
- value = value.plus(step, stepUnit);
136
- }
137
- },
138
- });
139
- }
140
107
  get(unit) {
141
108
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
142
109
  }
@@ -473,6 +440,41 @@ class LocalDate {
473
440
  }
474
441
  }
475
442
  exports.LocalDate = LocalDate;
443
+ function localDateRange(min, max, incl = '[)', step = 1, stepUnit = 'day') {
444
+ return localDateRangeIt(min, max, incl, step, stepUnit).toArray();
445
+ }
446
+ exports.localDateRange = localDateRange;
447
+ /**
448
+ * Experimental, returns the range as Iterable2.
449
+ */
450
+ function localDateRangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
451
+ if (stepUnit === 'week') {
452
+ step *= 7;
453
+ stepUnit = 'day';
454
+ }
455
+ const $min = LocalDate.of(min).startOf(stepUnit);
456
+ const $max = LocalDate.of(max).startOf(stepUnit);
457
+ let value = $min;
458
+ // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
459
+ if (value.isAfter($min, incl[0] === '[')) {
460
+ // ok
461
+ }
462
+ else {
463
+ value.plus(1, stepUnit, true);
464
+ }
465
+ const rightInclusive = incl[1] === ']';
466
+ return iterable2_1.Iterable2.of({
467
+ *[Symbol.iterator]() {
468
+ while (value.isBefore($max, rightInclusive)) {
469
+ yield value;
470
+ // We don't mutate, because we already returned `current`
471
+ // in the previous iteration
472
+ value = value.plus(step, stepUnit);
473
+ }
474
+ },
475
+ });
476
+ }
477
+ exports.localDateRangeIt = localDateRangeIt;
476
478
  /**
477
479
  * Convenience wrapper around `LocalDate.of`
478
480
  */
@@ -6,6 +6,16 @@ export declare function _randomInt(minIncl: number, maxIncl: number): number;
6
6
  * which is not reflected in the output type)
7
7
  */
8
8
  export declare function _randomArrayItem<T>(array: T[]): T;
9
+ /**
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;
9
19
  /**
10
20
  * _inRange(-10, 1, 5) // false
11
21
  * _inRange(1, 1, 5) // true
@@ -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._inRange = 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,6 +14,19 @@ 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
32
  * _inRange(-10, 1, 5) // false
@@ -1,4 +1,4 @@
1
- import { LocalDate } from './localDate';
1
+ import { LocalDate, localDateRange } from './localDate';
2
2
  /**
3
3
  * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
4
4
  *
@@ -63,13 +63,13 @@ export class DateInterval {
63
63
  return this.start.cmp(d.start) || this.end.cmp(d.end);
64
64
  }
65
65
  getDays(incl = '[]') {
66
- return LocalDate.range(this.start, this.end, incl, 1, 'day');
66
+ return localDateRange(this.start, this.end, incl, 1, 'day');
67
67
  }
68
68
  /**
69
69
  * Returns an array of LocalDates that are included in the interval.
70
70
  */
71
71
  range(incl = '[]', step = 1, stepUnit = 'day') {
72
- return LocalDate.range(this.start, this.end, incl, step, stepUnit);
72
+ return localDateRange(this.start, this.end, incl, step, stepUnit);
73
73
  }
74
74
  toString() {
75
75
  return [this.start, this.end].join('/');
@@ -101,39 +101,6 @@ export class LocalDate {
101
101
  .map(i => LocalDate.of(i))
102
102
  .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
103
103
  }
104
- static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
105
- return this.rangeIt(min, max, incl, step, stepUnit).toArray();
106
- }
107
- /**
108
- * Experimental, returns the range as Iterable2.
109
- */
110
- static rangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
111
- if (stepUnit === 'week') {
112
- step *= 7;
113
- stepUnit = 'day';
114
- }
115
- const $min = LocalDate.of(min).startOf(stepUnit);
116
- const $max = LocalDate.of(max).startOf(stepUnit);
117
- let value = $min;
118
- // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
119
- if (value.isAfter($min, incl[0] === '[')) {
120
- // ok
121
- }
122
- else {
123
- value.plus(1, stepUnit, true);
124
- }
125
- const rightInclusive = incl[1] === ']';
126
- return Iterable2.of({
127
- *[Symbol.iterator]() {
128
- while (value.isBefore($max, rightInclusive)) {
129
- yield value;
130
- // We don't mutate, because we already returned `current`
131
- // in the previous iteration
132
- value = value.plus(step, stepUnit);
133
- }
134
- },
135
- });
136
- }
137
104
  get(unit) {
138
105
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
139
106
  }
@@ -469,6 +436,39 @@ export class LocalDate {
469
436
  return fmt(this);
470
437
  }
471
438
  }
439
+ export function localDateRange(min, max, incl = '[)', step = 1, stepUnit = 'day') {
440
+ return localDateRangeIt(min, max, incl, step, stepUnit).toArray();
441
+ }
442
+ /**
443
+ * Experimental, returns the range as Iterable2.
444
+ */
445
+ export function localDateRangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
446
+ if (stepUnit === 'week') {
447
+ step *= 7;
448
+ stepUnit = 'day';
449
+ }
450
+ const $min = LocalDate.of(min).startOf(stepUnit);
451
+ const $max = LocalDate.of(max).startOf(stepUnit);
452
+ let value = $min;
453
+ // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
454
+ if (value.isAfter($min, incl[0] === '[')) {
455
+ // ok
456
+ }
457
+ else {
458
+ value.plus(1, stepUnit, true);
459
+ }
460
+ const rightInclusive = incl[1] === ']';
461
+ return Iterable2.of({
462
+ *[Symbol.iterator]() {
463
+ while (value.isBefore($max, rightInclusive)) {
464
+ yield value;
465
+ // We don't mutate, because we already returned `current`
466
+ // in the previous iteration
467
+ value = value.plus(step, stepUnit);
468
+ }
469
+ },
470
+ });
471
+ }
472
472
  /**
473
473
  * Convenience wrapper around `LocalDate.of`
474
474
  */
@@ -9,6 +9,18 @@ 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
26
  * _inRange(-10, 1, 5) // false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.192.0",
3
+ "version": "14.193.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,5 +1,5 @@
1
1
  import type { Inclusiveness, LocalDateInput, LocalDateUnit } from './localDate'
2
- import { LocalDate } from './localDate'
2
+ import { LocalDate, localDateRange } from './localDate'
3
3
 
4
4
  export type DateIntervalConfig = DateInterval | DateIntervalString
5
5
  export type DateIntervalString = string
@@ -81,14 +81,14 @@ export class DateInterval {
81
81
  }
82
82
 
83
83
  getDays(incl: Inclusiveness = '[]'): LocalDate[] {
84
- return LocalDate.range(this.start, this.end, incl, 1, 'day')
84
+ return localDateRange(this.start, this.end, incl, 1, 'day')
85
85
  }
86
86
 
87
87
  /**
88
88
  * Returns an array of LocalDates that are included in the interval.
89
89
  */
90
90
  range(incl: Inclusiveness = '[]', step = 1, stepUnit: LocalDateUnit = 'day'): LocalDate[] {
91
- return LocalDate.range(this.start, this.end, incl, step, stepUnit)
91
+ return localDateRange(this.start, this.end, incl, step, stepUnit)
92
92
  }
93
93
 
94
94
  toString(): DateIntervalString {
@@ -143,57 +143,6 @@ export class LocalDate {
143
143
  .reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
144
144
  }
145
145
 
146
- static range(
147
- min: LocalDateInput,
148
- max: LocalDateInput,
149
- incl: Inclusiveness = '[)',
150
- step = 1,
151
- stepUnit: LocalDateUnit = 'day',
152
- ): LocalDate[] {
153
- return this.rangeIt(min, max, incl, step, stepUnit).toArray()
154
- }
155
-
156
- /**
157
- * Experimental, returns the range as Iterable2.
158
- */
159
- static rangeIt(
160
- min: LocalDateInput,
161
- max: LocalDateInput,
162
- incl: Inclusiveness = '[)',
163
- step = 1,
164
- stepUnit: LocalDateUnit = 'day',
165
- ): Iterable2<LocalDate> {
166
- if (stepUnit === 'week') {
167
- step *= 7
168
- stepUnit = 'day'
169
- }
170
-
171
- const $min = LocalDate.of(min).startOf(stepUnit)
172
- const $max = LocalDate.of(max).startOf(stepUnit)
173
-
174
- let value = $min
175
- // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
176
- if (value.isAfter($min, incl[0] === '[')) {
177
- // ok
178
- } else {
179
- value.plus(1, stepUnit, true)
180
- }
181
-
182
- const rightInclusive = incl[1] === ']'
183
-
184
- return Iterable2.of({
185
- *[Symbol.iterator]() {
186
- while (value.isBefore($max, rightInclusive)) {
187
- yield value
188
-
189
- // We don't mutate, because we already returned `current`
190
- // in the previous iteration
191
- value = value.plus(step, stepUnit)
192
- }
193
- },
194
- })
195
- }
196
-
197
146
  get(unit: LocalDateUnitStrict): number {
198
147
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day
199
148
  }
@@ -583,6 +532,57 @@ export class LocalDate {
583
532
  }
584
533
  }
585
534
 
535
+ export function localDateRange(
536
+ min: LocalDateInput,
537
+ max: LocalDateInput,
538
+ incl: Inclusiveness = '[)',
539
+ step = 1,
540
+ stepUnit: LocalDateUnit = 'day',
541
+ ): LocalDate[] {
542
+ return localDateRangeIt(min, max, incl, step, stepUnit).toArray()
543
+ }
544
+
545
+ /**
546
+ * Experimental, returns the range as Iterable2.
547
+ */
548
+ export function localDateRangeIt(
549
+ min: LocalDateInput,
550
+ max: LocalDateInput,
551
+ incl: Inclusiveness = '[)',
552
+ step = 1,
553
+ stepUnit: LocalDateUnit = 'day',
554
+ ): Iterable2<LocalDate> {
555
+ if (stepUnit === 'week') {
556
+ step *= 7
557
+ stepUnit = 'day'
558
+ }
559
+
560
+ const $min = LocalDate.of(min).startOf(stepUnit)
561
+ const $max = LocalDate.of(max).startOf(stepUnit)
562
+
563
+ let value = $min
564
+ // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
565
+ if (value.isAfter($min, incl[0] === '[')) {
566
+ // ok
567
+ } else {
568
+ value.plus(1, stepUnit, true)
569
+ }
570
+
571
+ const rightInclusive = incl[1] === ']'
572
+
573
+ return Iterable2.of({
574
+ *[Symbol.iterator]() {
575
+ while (value.isBefore($max, rightInclusive)) {
576
+ yield value
577
+
578
+ // We don't mutate, because we already returned `current`
579
+ // in the previous iteration
580
+ value = value.plus(step, stepUnit)
581
+ }
582
+ },
583
+ })
584
+ }
585
+
586
586
  /**
587
587
  * Convenience wrapper around `LocalDate.of`
588
588
  */
@@ -13,6 +13,19 @@ 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
  /**