@naturalcycles/js-lib 14.98.3 → 14.99.2

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.
@@ -137,7 +137,16 @@ export declare function _mapToObject<T, V>(array: T[], mapper: (item: T) => [key
137
137
  * Based on: https://stackoverflow.com/a/12646864/4919972
138
138
  */
139
139
  export declare function _shuffle<T>(array: T[], mutate?: boolean): T[];
140
+ /**
141
+ * Returns last item of non-empty array.
142
+ * Throws if array is empty.
143
+ */
144
+ export declare function _last<T>(array: T[]): T;
140
145
  /**
141
146
  * Returns last item of the array (or undefined if array is empty).
142
147
  */
143
- export declare function _last<T>(array: T[]): T | undefined;
148
+ export declare function _lastOrUndefined<T>(array: T[]): T | undefined;
149
+ export declare function _minOrUndefined<T>(array: T[]): T | undefined;
150
+ export declare function _min<T>(array: T[]): T;
151
+ export declare function _maxOrUndefined<T>(array: T[]): T | undefined;
152
+ export declare function _max<T>(array: T[]): T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._uniq = exports._flattenDeep = exports._flatten = exports._chunk = void 0;
3
+ 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._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._uniq = exports._flattenDeep = exports._flatten = exports._chunk = void 0;
4
4
  /**
5
5
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
6
6
  * final chunk will be the remaining elements.
@@ -260,9 +260,43 @@ function _shuffle(array, mutate = false) {
260
260
  }
261
261
  exports._shuffle = _shuffle;
262
262
  /**
263
- * Returns last item of the array (or undefined if array is empty).
263
+ * Returns last item of non-empty array.
264
+ * Throws if array is empty.
264
265
  */
265
266
  function _last(array) {
267
+ if (!array.length)
268
+ throw new Error('_last called on empty array');
266
269
  return array[array.length - 1];
267
270
  }
268
271
  exports._last = _last;
272
+ /**
273
+ * Returns last item of the array (or undefined if array is empty).
274
+ */
275
+ function _lastOrUndefined(array) {
276
+ return array[array.length - 1];
277
+ }
278
+ exports._lastOrUndefined = _lastOrUndefined;
279
+ function _minOrUndefined(array) {
280
+ if (!array.length)
281
+ return;
282
+ return _min(array);
283
+ }
284
+ exports._minOrUndefined = _minOrUndefined;
285
+ function _min(array) {
286
+ if (!array.length)
287
+ throw new Error('_min called on empty array');
288
+ return array.reduce((min, item) => (min <= item ? min : item));
289
+ }
290
+ exports._min = _min;
291
+ function _maxOrUndefined(array) {
292
+ if (!array.length)
293
+ return;
294
+ return _max(array);
295
+ }
296
+ exports._maxOrUndefined = _maxOrUndefined;
297
+ function _max(array) {
298
+ if (!array.length)
299
+ throw new Error('_max called on empty array');
300
+ return array.reduce((max, item) => (max >= item ? max : item));
301
+ }
302
+ exports._max = _max;
@@ -1,8 +1,10 @@
1
1
  import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types';
2
2
  import { LocalTime } from './localTime';
3
- export declare type LocalDateUnit = 'year' | 'month' | 'day';
3
+ export declare type LocalDateUnit = LocalDateUnitStrict | 'week';
4
+ export declare type LocalDateUnitStrict = 'year' | 'month' | 'day';
4
5
  export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
5
6
  export declare type LocalDateConfig = LocalDate | IsoDateString;
7
+ export declare type LocalDateFormatter = (ld: LocalDate) => string;
6
8
  /**
7
9
  * @experimental
8
10
  */
@@ -28,13 +30,13 @@ export declare class LocalDate {
28
30
  static today(): LocalDate;
29
31
  static todayUTC(): LocalDate;
30
32
  static sort(items: LocalDate[], mutate?: boolean, descending?: boolean): LocalDate[];
31
- static earliestOrUndefined(items: LocalDate[]): LocalDate | undefined;
32
- static earliest(items: LocalDate[]): LocalDate;
33
- static latestOrUndefined(items: LocalDate[]): LocalDate | undefined;
34
- static latest(items: LocalDate[]): LocalDate;
33
+ static earliestOrUndefined(items: LocalDateConfig[]): LocalDate | undefined;
34
+ static earliest(items: LocalDateConfig[]): LocalDate;
35
+ static latestOrUndefined(items: LocalDateConfig[]): LocalDate | undefined;
36
+ static latest(items: LocalDateConfig[]): LocalDate;
35
37
  static range(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
36
- get(unit: LocalDateUnit): number;
37
- set(unit: LocalDateUnit, v: number, mutate?: boolean): LocalDate;
38
+ get(unit: LocalDateUnitStrict): number;
39
+ set(unit: LocalDateUnitStrict, v: number, mutate?: boolean): LocalDate;
38
40
  year(): number;
39
41
  year(v: number): LocalDate;
40
42
  month(): number;
@@ -58,15 +60,15 @@ export declare class LocalDate {
58
60
  */
59
61
  absDiff(d: LocalDateConfig, unit: LocalDateUnit): number;
60
62
  /**
61
- * Returns the number of **full** units difference (aka `Math.ceil`).
63
+ * Returns the number of **full** units difference (aka `Math.floor`).
62
64
  *
63
65
  * a.diff(b) means "a minus b"
64
66
  */
65
67
  diff(d: LocalDateConfig, unit: LocalDateUnit): number;
66
68
  add(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
67
69
  subtract(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
68
- startOf(unit: LocalDateUnit): LocalDate;
69
- endOf(unit: LocalDateUnit): LocalDate;
70
+ startOf(unit: LocalDateUnitStrict): LocalDate;
71
+ endOf(unit: LocalDateUnitStrict): LocalDate;
70
72
  static getYearLength(year: number): number;
71
73
  static getMonthLength(year: number, month: number): number;
72
74
  static isLeapYear(year: number): boolean;
@@ -89,6 +91,7 @@ export declare class LocalDate {
89
91
  unix(): UnixTimestampNumber;
90
92
  unixMillis(): number;
91
93
  toJSON(): IsoDateString;
94
+ format(fmt: LocalDateFormatter): string;
92
95
  }
93
96
  /**
94
97
  * Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
@@ -90,16 +90,24 @@ class LocalDate {
90
90
  }
91
91
  static earliest(items) {
92
92
  (0, assert_1._assert)(items.length, 'LocalDate.earliest called on empty array');
93
- return items.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
93
+ return items
94
+ .map(i => LocalDate.of(i))
95
+ .reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
94
96
  }
95
97
  static latestOrUndefined(items) {
96
98
  return items.length ? LocalDate.latest(items) : undefined;
97
99
  }
98
100
  static latest(items) {
99
101
  (0, assert_1._assert)(items.length, 'LocalDate.latest called on empty array');
100
- return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
102
+ return items
103
+ .map(i => LocalDate.of(i))
104
+ .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
101
105
  }
102
106
  static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
107
+ if (stepUnit === 'week') {
108
+ step *= 7;
109
+ stepUnit = 'day';
110
+ }
103
111
  const dates = [];
104
112
  const $min = LocalDate.of(min);
105
113
  const $max = LocalDate.of(max).startOf(stepUnit);
@@ -197,44 +205,58 @@ class LocalDate {
197
205
  return Math.abs(this.diff(d, unit));
198
206
  }
199
207
  /**
200
- * Returns the number of **full** units difference (aka `Math.ceil`).
208
+ * Returns the number of **full** units difference (aka `Math.floor`).
201
209
  *
202
210
  * a.diff(b) means "a minus b"
203
211
  */
204
212
  diff(d, unit) {
205
213
  d = LocalDate.of(d);
214
+ const sign = this.cmp(d);
215
+ if (!sign)
216
+ return 0;
217
+ // Put items in descending order: "big minus small"
218
+ const [big, small] = sign === 1 ? [this, d] : [d, this];
206
219
  if (unit === 'year') {
207
- return this.$year - d.$year;
220
+ let years = big.$year - small.$year;
221
+ if (big.$month < small.$month || (big.$month === small.$month && big.$day < small.$day)) {
222
+ years--;
223
+ }
224
+ return years * sign || 0;
208
225
  }
209
226
  if (unit === 'month') {
210
- return (this.$year - d.$year) * 12 + (this.$month - d.$month);
227
+ let months = (big.$year - small.$year) * 12 + (big.$month - small.$month);
228
+ if (big.$day < small.$day)
229
+ months--;
230
+ return months * sign || 0;
211
231
  }
212
- // unit is 'day'
213
- let days = this.$day - d.$day;
214
- if (d.$year < this.$year) {
215
- for (let year = d.$year; year < this.$year; year++) {
216
- days += LocalDate.getYearLength(year);
217
- }
232
+ // unit is 'day' or 'week'
233
+ let days = big.$day - small.$day;
234
+ // If small date is after 1st of March - next year's "leapness" should be used
235
+ const offsetYear = small.$month >= 3 ? 1 : 0;
236
+ for (let year = small.$year; year < big.$year; year++) {
237
+ days += LocalDate.getYearLength(year + offsetYear);
218
238
  }
219
- else if (this.$year < d.$year) {
220
- for (let year = this.$year; year < d.$year; year++) {
221
- days -= LocalDate.getYearLength(year);
239
+ if (small.$month < big.$month) {
240
+ for (let month = small.$month; month < big.$month; month++) {
241
+ days += LocalDate.getMonthLength(big.$year, month);
222
242
  }
223
243
  }
224
- if (d.$month < this.$month) {
225
- for (let month = d.$month; month < this.$month; month++) {
226
- days += LocalDate.getMonthLength(this.$year, month);
244
+ else if (big.$month < small.$month) {
245
+ for (let month = big.$month; month < small.$month; month++) {
246
+ days -= LocalDate.getMonthLength(big.$year, month);
227
247
  }
228
248
  }
229
- else if (this.$month < d.$month) {
230
- for (let month = this.$month; month < d.$month; month++) {
231
- days -= LocalDate.getMonthLength(d.$year, month);
232
- }
249
+ if (unit === 'week') {
250
+ return Math.trunc(days / 7) * sign || 0;
233
251
  }
234
- return days;
252
+ return days * sign || 0;
235
253
  }
236
254
  add(num, unit, mutate = false) {
237
255
  let { $day, $month, $year } = this;
256
+ if (unit === 'week') {
257
+ num *= 7;
258
+ unit = 'day';
259
+ }
238
260
  if (unit === 'day') {
239
261
  $day += num;
240
262
  }
@@ -364,6 +386,9 @@ class LocalDate {
364
386
  toJSON() {
365
387
  return this.toString();
366
388
  }
389
+ format(fmt) {
390
+ return fmt(this);
391
+ }
367
392
  }
368
393
  exports.LocalDate = LocalDate;
369
394
  /**
@@ -1,7 +1,17 @@
1
1
  import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types';
2
2
  import { Inclusiveness, LocalDate } from './localDate';
3
- export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
3
+ export declare type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
4
+ export declare enum ISODayOfWeek {
5
+ MONDAY = 1,
6
+ TUESDAY = 2,
7
+ WEDNESDAY = 3,
8
+ THURSDAY = 4,
9
+ FRIDAY = 5,
10
+ SATURDAY = 6,
11
+ SUNDAY = 7
12
+ }
4
13
  export declare type LocalTimeConfig = LocalTime | Date | IsoDateTimeString | UnixTimestampNumber;
14
+ export declare type LocalTimeFormatter = (ld: LocalTime) => string;
5
15
  export interface LocalTimeComponents {
6
16
  year: number;
7
17
  month: number;
@@ -15,15 +25,12 @@ export interface LocalTimeComponents {
15
25
  */
16
26
  export declare class LocalTime {
17
27
  private $date;
18
- utcMode: boolean;
19
28
  private constructor();
20
29
  /**
21
30
  * Parses input String into LocalDate.
22
31
  * Input can already be a LocalDate - it is returned as-is in that case.
23
32
  */
24
33
  static of(d: LocalTimeConfig): LocalTime;
25
- utc(): this;
26
- local(): this;
27
34
  /**
28
35
  * Returns null if invalid
29
36
  */
@@ -42,8 +49,15 @@ export declare class LocalTime {
42
49
  year(v: number): LocalTime;
43
50
  month(): number;
44
51
  month(v: number): LocalTime;
52
+ week(): number;
53
+ week(v: number): LocalTime;
45
54
  day(): number;
46
55
  day(v: number): LocalTime;
56
+ /**
57
+ * Based on ISO: 1-7 is Mon-Sun.
58
+ */
59
+ dayOfWeek(): ISODayOfWeek;
60
+ dayOfWeek(v: ISODayOfWeek): LocalTime;
47
61
  hour(): number;
48
62
  hour(v: number): LocalTime;
49
63
  minute(): number;
@@ -56,11 +70,12 @@ export declare class LocalTime {
56
70
  absDiff(other: LocalTimeConfig, unit: LocalTimeUnit): number;
57
71
  diff(other: LocalTimeConfig, unit: LocalTimeUnit): number;
58
72
  startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
73
+ endOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
59
74
  static sort(items: LocalTime[], mutate?: boolean, descending?: boolean): LocalTime[];
60
- static earliestOrUndefined(items: LocalTime[]): LocalTime | undefined;
61
- static earliest(items: LocalTime[]): LocalTime;
62
- static latestOrUndefined(items: LocalTime[]): LocalTime | undefined;
63
- static latest(items: LocalTime[]): LocalTime;
75
+ static earliestOrUndefined(items: LocalTimeConfig[]): LocalTime | undefined;
76
+ static earliest(items: LocalTimeConfig[]): LocalTime;
77
+ static latestOrUndefined(items: LocalTimeConfig[]): LocalTime | undefined;
78
+ static latest(items: LocalTimeConfig[]): LocalTime;
64
79
  isSame(d: LocalTimeConfig): boolean;
65
80
  isBefore(d: LocalTimeConfig, inclusive?: boolean): boolean;
66
81
  isSameOrBefore(d: LocalTimeConfig): boolean;
@@ -100,6 +115,7 @@ export declare class LocalTime {
100
115
  toStringCompact(seconds?: boolean): string;
101
116
  toString(): string;
102
117
  toJSON(): UnixTimestampNumber;
118
+ format(fmt: LocalTimeFormatter): string;
103
119
  }
104
120
  /**
105
121
  * Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`