@naturalcycles/js-lib 15.9.0 → 15.9.1

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.
@@ -96,7 +96,7 @@ export interface MutateOptions {
96
96
  */
97
97
  mutate?: boolean;
98
98
  }
99
- export interface SortByOptions extends MutateOptions {
99
+ export interface SortOptions extends MutateOptions {
100
100
  /**
101
101
  * Defaults to 'asc'.
102
102
  */
@@ -109,7 +109,7 @@ export interface SortByOptions extends MutateOptions {
109
109
  * Same:
110
110
  * _sortBy([{age: 20}, {age: 10}], o => o.age)
111
111
  */
112
- export declare function _sortBy<T, COMPARE_TYPE extends string | number>(items: T[], mapper: Mapper<T, COMPARE_TYPE>, opt?: SortByOptions): T[];
112
+ export declare function _sortBy<T, COMPARE_TYPE extends string | number>(items: T[], mapper: Mapper<T, COMPARE_TYPE>, opt?: SortOptions): T[];
113
113
  /**
114
114
  * Similar to `Array.find`, but the `predicate` may return `END` to stop the iteration early.
115
115
  *
@@ -1,5 +1,6 @@
1
+ import type { MutateOptions, SortOptions } from '../array/array.util.js';
1
2
  import { Iterable2 } from '../iter/iterable2.js';
2
- import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types.js';
3
+ import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, UnixTimestamp, UnixTimestampMillis } from '../types.js';
3
4
  import type { DateObject, ISODayOfWeek, LocalTime } from './localTime.js';
4
5
  export type LocalDateUnit = LocalDateUnitStrict | 'week';
5
6
  export type LocalDateUnitStrict = 'year' | 'month' | 'day';
@@ -16,7 +17,7 @@ export declare class LocalDate {
16
17
  day: number;
17
18
  constructor(year: number, month: number, day: number);
18
19
  get(unit: LocalDateUnitStrict): number;
19
- set(unit: LocalDateUnitStrict, v: number, mutate?: boolean): LocalDate;
20
+ set(unit: LocalDateUnitStrict, v: number, opt?: MutateOptions): LocalDate;
20
21
  setYear(v: number): LocalDate;
21
22
  setMonth(v: number): LocalDate;
22
23
  setDay(v: number): LocalDate;
@@ -100,8 +101,8 @@ export declare class LocalDate {
100
101
  minusWeeks(num: number): LocalDate;
101
102
  minusMonths(num: number): LocalDate;
102
103
  minusYears(num: number): LocalDate;
103
- plus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
104
- minus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
104
+ plus(num: number, unit: LocalDateUnit, opt?: MutateOptions): LocalDate;
105
+ minus(num: number, unit: LocalDateUnit, opt?: MutateOptions): LocalDate;
105
106
  startOf(unit: LocalDateUnitStrict): LocalDate;
106
107
  endOf(unit: LocalDateUnitStrict): LocalDate;
107
108
  /**
@@ -246,7 +247,7 @@ declare class LocalDateFactory {
246
247
  /**
247
248
  * Sorts an array of LocalDates in `dir` order (ascending by default).
248
249
  */
249
- sort(items: LocalDate[], dir?: SortDirection, mutate?: boolean): LocalDate[];
250
+ sort(items: LocalDate[], opt?: SortOptions): LocalDate[];
250
251
  /**
251
252
  * Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
252
253
  */
@@ -23,8 +23,8 @@ export class LocalDate {
23
23
  get(unit) {
24
24
  return unit === 'year' ? this.year : unit === 'month' ? this.month : this.day;
25
25
  }
26
- set(unit, v, mutate = false) {
27
- const t = mutate ? this : this.clone();
26
+ set(unit, v, opt = {}) {
27
+ const t = opt.mutate ? this : this.clone();
28
28
  if (unit === 'year') {
29
29
  t.year = v;
30
30
  }
@@ -266,7 +266,7 @@ export class LocalDate {
266
266
  minusYears(num) {
267
267
  return this.plus(-num, 'year');
268
268
  }
269
- plus(num, unit, mutate = false) {
269
+ plus(num, unit, opt = {}) {
270
270
  num = Math.floor(num); // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
271
271
  let { day, month, year } = this;
272
272
  if (unit === 'week') {
@@ -323,7 +323,7 @@ export class LocalDate {
323
323
  }
324
324
  }
325
325
  }
326
- if (mutate) {
326
+ if (opt.mutate) {
327
327
  this.year = year;
328
328
  this.month = month;
329
329
  this.day = day;
@@ -331,8 +331,8 @@ export class LocalDate {
331
331
  }
332
332
  return new LocalDate(year, month, day);
333
333
  }
334
- minus(num, unit, mutate = false) {
335
- return this.plus(-num, unit, mutate);
334
+ minus(num, unit, opt = {}) {
335
+ return this.plus(-num, unit, opt);
336
336
  }
337
337
  startOf(unit) {
338
338
  if (unit === 'day')
@@ -612,9 +612,9 @@ class LocalDateFactory {
612
612
  /**
613
613
  * Sorts an array of LocalDates in `dir` order (ascending by default).
614
614
  */
615
- sort(items, dir = 'asc', mutate = false) {
616
- const mod = dir === 'desc' ? -1 : 1;
617
- return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
615
+ sort(items, opt = {}) {
616
+ const mod = opt.dir === 'desc' ? -1 : 1;
617
+ return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
618
618
  }
619
619
  /**
620
620
  * Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
@@ -687,7 +687,7 @@ class LocalDateFactory {
687
687
  // ok
688
688
  }
689
689
  else {
690
- value.plus(1, stepUnit, true);
690
+ value.plus(1, stepUnit, { mutate: true });
691
691
  }
692
692
  const rightInclusive = incl[1] === ']';
693
693
  return Iterable2.of({
@@ -1,3 +1,4 @@
1
+ import type { MutateOptions } from '../array/array.util.js';
1
2
  import type { IANATimezone, Inclusiveness, IsoDate, IsoDateTime, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types.js';
2
3
  import type { LocalDate } from './localDate.js';
3
4
  import { WallTime } from './wallTime.js';
@@ -86,7 +87,7 @@ export declare class LocalTime {
86
87
  */
87
88
  getUTCOffsetString(tz: IANATimezone): string;
88
89
  get(unit: LocalTimeUnit): number;
89
- set(unit: LocalTimeUnit, v: number, mutate?: boolean): LocalTime;
90
+ set(unit: LocalTimeUnit, v: number, opt?: MutateOptions): LocalTime;
90
91
  get year(): number;
91
92
  setYear(v: number): LocalTime;
92
93
  get month(): number;
@@ -117,7 +118,7 @@ export declare class LocalTime {
117
118
  * If this LocalTime is Monday, and desired DoW is also Monday - `this` is returned.
118
119
  */
119
120
  setNextDayOfWeek(dow: ISODayOfWeek): LocalTime;
120
- setComponents(c: Partial<DateTimeObject>, mutate?: boolean): LocalTime;
121
+ setComponents(c: Partial<DateTimeObject>, opt?: MutateOptions): LocalTime;
121
122
  plusSeconds(num: number): LocalTime;
122
123
  plusMinutes(num: number): LocalTime;
123
124
  plusHours(num: number): LocalTime;
@@ -132,12 +133,12 @@ export declare class LocalTime {
132
133
  minusWeeks(num: number): LocalTime;
133
134
  minusMonths(num: number): LocalTime;
134
135
  minusYears(num: number): LocalTime;
135
- plus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
136
- minus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
136
+ plus(num: number, unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
137
+ minus(num: number, unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
137
138
  absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number;
138
139
  diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
139
- startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
140
- endOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
140
+ startOf(unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
141
+ endOf(unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
141
142
  /**
142
143
  * Returns how many days are in the current month.
143
144
  * E.g 31 for January.
@@ -314,7 +315,7 @@ declare class LocalTimeFactory {
314
315
  * consider caching the Intl.supportedValuesOf values as Set and reuse that.
315
316
  */
316
317
  isTimezoneValid(tz: string): boolean;
317
- sort(items: LocalTime[], dir?: SortDirection, mutate?: boolean): LocalTime[];
318
+ sort(items: LocalTime[], dir?: SortDirection, opt?: MutateOptions): LocalTime[];
318
319
  minOrUndefined(items: LocalTimeInputNullable[]): LocalTime | undefined;
319
320
  min(items: LocalTimeInputNullable[]): LocalTime;
320
321
  maxOrUndefined(items: LocalTimeInputNullable[]): LocalTime | undefined;
@@ -146,8 +146,8 @@ export class LocalTime {
146
146
  // second
147
147
  return this.$date.getSeconds();
148
148
  }
149
- set(unit, v, mutate = false) {
150
- const t = mutate ? this : this.clone();
149
+ set(unit, v, opt = {}) {
150
+ const t = opt.mutate ? this : this.clone();
151
151
  if (unit === 'year') {
152
152
  t.$date.setFullYear(v);
153
153
  }
@@ -167,7 +167,7 @@ export class LocalTime {
167
167
  t.$date.setSeconds(v);
168
168
  }
169
169
  else if (unit === 'week') {
170
- setWeek(t.$date, v, true);
170
+ setWeek(t.$date, v, { mutate: true });
171
171
  }
172
172
  return t;
173
173
  }
@@ -241,8 +241,8 @@ export class LocalTime {
241
241
  delta += 7;
242
242
  return this.plus(delta, 'day');
243
243
  }
244
- setComponents(c, mutate = false) {
245
- const d = mutate ? this.$date : new Date(this.$date);
244
+ setComponents(c, opt = {}) {
245
+ const d = opt.mutate ? this.$date : new Date(this.$date);
246
246
  // Year, month and day set all-at-once, to avoid 30/31 (and 28/29) mishap
247
247
  if (c.day || c.month !== undefined || c.year !== undefined) {
248
248
  d.setFullYear(c.year ?? d.getFullYear(), c.month ? c.month - 1 : d.getMonth(), c.day || d.getDate());
@@ -256,7 +256,7 @@ export class LocalTime {
256
256
  if (c.second !== undefined) {
257
257
  d.setSeconds(c.second);
258
258
  }
259
- return mutate ? this : new LocalTime(d);
259
+ return opt.mutate ? this : new LocalTime(d);
260
260
  }
261
261
  plusSeconds(num) {
262
262
  return this.plus(num, 'second');
@@ -300,19 +300,19 @@ export class LocalTime {
300
300
  minusYears(num) {
301
301
  return this.plus(-num, 'year');
302
302
  }
303
- plus(num, unit, mutate = false) {
303
+ plus(num, unit, opt = {}) {
304
304
  if (unit === 'week') {
305
305
  num *= 7;
306
306
  unit = 'day';
307
307
  }
308
308
  if (unit === 'year' || unit === 'month') {
309
- const d = addMonths(this.$date, unit === 'month' ? num : num * 12, mutate);
310
- return mutate ? this : localTime.fromInput(d);
309
+ const d = addMonths(this.$date, unit === 'month' ? num : num * 12, opt);
310
+ return opt.mutate ? this : localTime.fromInput(d);
311
311
  }
312
- return this.set(unit, this.get(unit) + num, mutate);
312
+ return this.set(unit, this.get(unit) + num, opt);
313
313
  }
314
- minus(num, unit, mutate = false) {
315
- return this.plus(num * -1, unit, mutate);
314
+ minus(num, unit, opt = {}) {
315
+ return this.plus(num * -1, unit, opt);
316
316
  }
317
317
  absDiff(other, unit) {
318
318
  return Math.abs(this.diff(other, unit));
@@ -348,10 +348,10 @@ export class LocalTime {
348
348
  // `|| 0` is to avoid returning -0
349
349
  return Math.trunc(r) || 0;
350
350
  }
351
- startOf(unit, mutate = false) {
351
+ startOf(unit, opt = {}) {
352
352
  if (unit === 'second')
353
353
  return this;
354
- const d = mutate ? this.$date : new Date(this.$date);
354
+ const d = opt.mutate ? this.$date : new Date(this.$date);
355
355
  d.setSeconds(0, 0);
356
356
  if (unit !== 'minute') {
357
357
  d.setMinutes(0);
@@ -368,17 +368,17 @@ export class LocalTime {
368
368
  }
369
369
  else {
370
370
  // week
371
- startOfWeek(d, true);
371
+ startOfWeek(d, { mutate: true });
372
372
  }
373
373
  }
374
374
  }
375
375
  }
376
- return mutate ? this : new LocalTime(d);
376
+ return opt.mutate ? this : new LocalTime(d);
377
377
  }
378
- endOf(unit, mutate = false) {
378
+ endOf(unit, opt = {}) {
379
379
  if (unit === 'second')
380
380
  return this;
381
- const d = mutate ? this.$date : new Date(this.$date);
381
+ const d = opt.mutate ? this.$date : new Date(this.$date);
382
382
  d.setSeconds(59, 0);
383
383
  if (unit !== 'minute') {
384
384
  d.setMinutes(59);
@@ -390,7 +390,7 @@ export class LocalTime {
390
390
  d.setMonth(11);
391
391
  }
392
392
  if (unit === 'week') {
393
- endOfWeek(d, true);
393
+ endOfWeek(d, { mutate: true });
394
394
  }
395
395
  else {
396
396
  // year or month
@@ -400,7 +400,7 @@ export class LocalTime {
400
400
  }
401
401
  }
402
402
  }
403
- return mutate ? this : new LocalTime(d);
403
+ return opt.mutate ? this : new LocalTime(d);
404
404
  }
405
405
  /**
406
406
  * Returns how many days are in the current month.
@@ -856,9 +856,9 @@ class LocalTimeFactory {
856
856
  return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
857
857
  return Intl.supportedValuesOf('timeZone').includes(tz);
858
858
  }
859
- sort(items, dir = 'asc', mutate = false) {
859
+ sort(items, dir = 'asc', opt = {}) {
860
860
  const mod = dir === 'desc' ? -1 : 1;
861
- return (mutate ? items : [...items]).sort((a, b) => {
861
+ return (opt.mutate ? items : [...items]).sort((a, b) => {
862
862
  const v1 = a.$date.valueOf();
863
863
  const v2 = b.$date.valueOf();
864
864
  if (v1 === v2)
@@ -906,8 +906,8 @@ function getWeek(date) {
906
906
  const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime();
907
907
  return Math.round(diff / MILLISECONDS_IN_WEEK) + 1;
908
908
  }
909
- function setWeek(date, week, mutate = false) {
910
- const d = mutate ? date : new Date(date);
909
+ function setWeek(date, week, opt = {}) {
910
+ const d = opt.mutate ? date : new Date(date);
911
911
  const diff = getWeek(d) - week;
912
912
  d.setDate(d.getDate() - diff * 7);
913
913
  return d;
@@ -918,7 +918,7 @@ function startOfWeekYear(date) {
918
918
  const fourthOfJanuary = new Date(0);
919
919
  fourthOfJanuary.setFullYear(year, 0, 4);
920
920
  fourthOfJanuary.setHours(0, 0, 0, 0);
921
- return startOfWeek(fourthOfJanuary, true);
921
+ return startOfWeek(fourthOfJanuary, { mutate: true });
922
922
  }
923
923
  // based on: https://github.com/date-fns/date-fns/blob/fd6bb1a0bab143f2da068c05a9c562b9bee1357d/src/getISOWeekYear/index.ts
924
924
  function getWeekYear(date) {
@@ -926,11 +926,11 @@ function getWeekYear(date) {
926
926
  const fourthOfJanuaryOfNextYear = new Date(0);
927
927
  fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
928
928
  fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
929
- const startOfNextYear = startOfWeek(fourthOfJanuaryOfNextYear, true);
929
+ const startOfNextYear = startOfWeek(fourthOfJanuaryOfNextYear, { mutate: true });
930
930
  const fourthOfJanuaryOfThisYear = new Date(0);
931
931
  fourthOfJanuaryOfThisYear.setFullYear(year, 0, 4);
932
932
  fourthOfJanuaryOfThisYear.setHours(0, 0, 0, 0);
933
- const startOfThisYear = startOfWeek(fourthOfJanuaryOfThisYear, true);
933
+ const startOfThisYear = startOfWeek(fourthOfJanuaryOfThisYear, { mutate: true });
934
934
  if (date.getTime() >= startOfNextYear.getTime()) {
935
935
  return year + 1;
936
936
  }
@@ -940,8 +940,8 @@ function getWeekYear(date) {
940
940
  return year - 1;
941
941
  }
942
942
  // based on: https://github.com/date-fns/date-fns/blob/fd6bb1a0bab143f2da068c05a9c562b9bee1357d/src/startOfWeek/index.ts
943
- function startOfWeek(date, mutate = false) {
944
- const d = mutate ? date : new Date(date);
943
+ function startOfWeek(date, opt = {}) {
944
+ const d = opt.mutate ? date : new Date(date);
945
945
  const day = d.getDay();
946
946
  const diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn;
947
947
  d.setDate(d.getDate() - diff);
@@ -949,15 +949,15 @@ function startOfWeek(date, mutate = false) {
949
949
  return d;
950
950
  }
951
951
  // based on: https://github.com/date-fns/date-fns/blob/master/src/endOfWeek/index.ts
952
- function endOfWeek(date, mutate = false) {
953
- const d = mutate ? date : new Date(date);
952
+ function endOfWeek(date, opt = {}) {
953
+ const d = opt.mutate ? date : new Date(date);
954
954
  const day = d.getDay();
955
955
  const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
956
956
  d.setDate(d.getDate() + diff);
957
957
  return d;
958
958
  }
959
- function addMonths(d, num, mutate = false) {
960
- if (!mutate)
959
+ function addMonths(d, num, opt = {}) {
960
+ if (!opt.mutate)
961
961
  d = new Date(d);
962
962
  let day = d.getDate();
963
963
  let month = d.getMonth() + 1 + num;
@@ -1,3 +1,4 @@
1
+ import type { MutateOptions } from '../array/array.util.js';
1
2
  /**
2
3
  * These levels follow console.* naming,
3
4
  * so you can use console[level] safely.
@@ -38,7 +39,7 @@ export declare const commonLoggerNoop: CommonLogger;
38
39
  /**
39
40
  * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
40
41
  */
41
- export declare function commonLoggerMinLevel(logger: CommonLogger, minLevel: CommonLogLevel, mutate?: boolean): CommonLogger;
42
+ export declare function commonLoggerMinLevel(logger: CommonLogger, minLevel: CommonLogLevel, opt?: MutateOptions): CommonLogger;
42
43
  /**
43
44
  * Creates a "proxy" CommonLogger that pipes log messages to all provided sub-loggers.
44
45
  */
@@ -1,4 +1,3 @@
1
- // copy-pasted to avoid weird circular dependency
2
1
  const _noop = (..._args) => undefined;
3
2
  export const commonLogLevelNumber = {
4
3
  log: 10,
@@ -18,9 +17,9 @@ export const commonLoggerNoop = {
18
17
  /**
19
18
  * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
20
19
  */
21
- export function commonLoggerMinLevel(logger, minLevel, mutate = false) {
20
+ export function commonLoggerMinLevel(logger, minLevel, opt = {}) {
22
21
  const level = commonLogLevelNumber[minLevel];
23
- if (mutate) {
22
+ if (opt.mutate) {
24
23
  if (level > commonLogLevelNumber['log']) {
25
24
  logger.log = _noop;
26
25
  if (level > commonLogLevelNumber['warn']) {
@@ -1,4 +1,5 @@
1
- import type { Inclusiveness, SortDirection } from '../types.js';
1
+ import type { SortOptions } from '../array/array.util.js';
2
+ import type { Inclusiveness } from '../types.js';
2
3
  export declare function _randomInt(minIncl: number, maxIncl: number): number;
3
4
  /**
4
5
  * Returns random item from an array.
@@ -37,7 +38,7 @@ export declare function _clamp(x: number, minIncl: number, maxIncl: number): num
37
38
  * _sortNumbers([1, 3, 2])
38
39
  * // [1, 2, 3]
39
40
  */
40
- export declare function _sortNumbers(numbers: number[], mutate?: boolean, dir?: SortDirection): number[];
41
+ export declare function _sortNumbers(numbers: number[], opt?: SortOptions): number[];
41
42
  /**
42
43
  * Same as .toFixed(), but conveniently casts the output to Number.
43
44
  *
@@ -52,9 +52,9 @@ export function _clamp(x, minIncl, maxIncl) {
52
52
  * _sortNumbers([1, 3, 2])
53
53
  * // [1, 2, 3]
54
54
  */
55
- export function _sortNumbers(numbers, mutate = false, dir = 'asc') {
56
- const mod = dir === 'desc' ? -1 : 1;
57
- return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
55
+ export function _sortNumbers(numbers, opt = {}) {
56
+ const mod = opt.dir === 'desc' ? -1 : 1;
57
+ return (opt.mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
58
58
  }
59
59
  /**
60
60
  * Same as .toFixed(), but conveniently casts the output to Number.
package/dist/semver.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SortDirection } from './types.js';
1
+ import type { SortOptions } from './array/array.util.js';
2
2
  export type SemverInput = string | Semver;
3
3
  export type SemverInputNullable = SemverInput | null | undefined;
4
4
  export type SemverTokens = [major: number, minor: number, patch: number];
@@ -62,7 +62,7 @@ declare class SemverFactory {
62
62
  /**
63
63
  * Sorts an array of Semvers in `dir` order (ascending by default).
64
64
  */
65
- sort(items: Semver[], dir?: SortDirection, mutate?: boolean): Semver[];
65
+ sort(items: Semver[], opt?: SortOptions): Semver[];
66
66
  }
67
67
  interface SemverFn extends SemverFactory {
68
68
  (input: SemverInput): Semver;
package/dist/semver.js CHANGED
@@ -118,9 +118,9 @@ class SemverFactory {
118
118
  /**
119
119
  * Sorts an array of Semvers in `dir` order (ascending by default).
120
120
  */
121
- sort(items, dir = 'asc', mutate = false) {
122
- const mod = dir === 'desc' ? -1 : 1;
123
- return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
121
+ sort(items, opt = {}) {
122
+ const mod = opt.dir === 'desc' ? -1 : 1;
123
+ return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
124
124
  }
125
125
  }
126
126
  const semverFactory = new SemverFactory();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.9.0",
4
+ "version": "15.9.1",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "zod": "^3"
@@ -181,7 +181,7 @@ export interface MutateOptions {
181
181
  mutate?: boolean
182
182
  }
183
183
 
184
- export interface SortByOptions extends MutateOptions {
184
+ export interface SortOptions extends MutateOptions {
185
185
  /**
186
186
  * Defaults to 'asc'.
187
187
  */
@@ -198,7 +198,7 @@ export interface SortByOptions extends MutateOptions {
198
198
  export function _sortBy<T, COMPARE_TYPE extends string | number>(
199
199
  items: T[],
200
200
  mapper: Mapper<T, COMPARE_TYPE>,
201
- opt: SortByOptions = {},
201
+ opt: SortOptions = {},
202
202
  ): T[] {
203
203
  const mod = opt.dir === 'desc' ? -1 : 1
204
204
 
@@ -1,3 +1,4 @@
1
+ import type { MutateOptions, SortOptions } from '../array/array.util.js'
1
2
  import { _assert } from '../error/assert.js'
2
3
  import { Iterable2 } from '../iter/iterable2.js'
3
4
  import type {
@@ -5,7 +6,6 @@ import type {
5
6
  IsoDate,
6
7
  IsoDateTime,
7
8
  MonthId,
8
- SortDirection,
9
9
  UnixTimestamp,
10
10
  UnixTimestampMillis,
11
11
  } from '../types.js'
@@ -41,8 +41,8 @@ export class LocalDate {
41
41
  return unit === 'year' ? this.year : unit === 'month' ? this.month : this.day
42
42
  }
43
43
 
44
- set(unit: LocalDateUnitStrict, v: number, mutate = false): LocalDate {
45
- const t = mutate ? this : this.clone()
44
+ set(unit: LocalDateUnitStrict, v: number, opt: MutateOptions = {}): LocalDate {
45
+ const t = opt.mutate ? this : this.clone()
46
46
 
47
47
  if (unit === 'year') {
48
48
  t.year = v
@@ -325,7 +325,7 @@ export class LocalDate {
325
325
  return this.plus(-num, 'year')
326
326
  }
327
327
 
328
- plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
328
+ plus(num: number, unit: LocalDateUnit, opt: MutateOptions = {}): LocalDate {
329
329
  num = Math.floor(num) // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
330
330
  let { day, month, year } = this
331
331
 
@@ -386,7 +386,7 @@ export class LocalDate {
386
386
  }
387
387
  }
388
388
 
389
- if (mutate) {
389
+ if (opt.mutate) {
390
390
  this.year = year
391
391
  this.month = month
392
392
  this.day = day
@@ -396,8 +396,8 @@ export class LocalDate {
396
396
  return new LocalDate(year, month, day)
397
397
  }
398
398
 
399
- minus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
400
- return this.plus(-num, unit, mutate)
399
+ minus(num: number, unit: LocalDateUnit, opt: MutateOptions = {}): LocalDate {
400
+ return this.plus(-num, unit, opt)
401
401
  }
402
402
 
403
403
  startOf(unit: LocalDateUnitStrict): LocalDate {
@@ -716,9 +716,9 @@ class LocalDateFactory {
716
716
  /**
717
717
  * Sorts an array of LocalDates in `dir` order (ascending by default).
718
718
  */
719
- sort(items: LocalDate[], dir: SortDirection = 'asc', mutate = false): LocalDate[] {
720
- const mod = dir === 'desc' ? -1 : 1
721
- return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
719
+ sort(items: LocalDate[], opt: SortOptions = {}): LocalDate[] {
720
+ const mod = opt.dir === 'desc' ? -1 : 1
721
+ return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
722
722
  }
723
723
 
724
724
  /**
@@ -809,7 +809,7 @@ class LocalDateFactory {
809
809
  if (value.isSameOrAfter($min)) {
810
810
  // ok
811
811
  } else {
812
- value.plus(1, stepUnit, true)
812
+ value.plus(1, stepUnit, { mutate: true })
813
813
  }
814
814
 
815
815
  const rightInclusive = incl[1] === ']'
@@ -1,3 +1,4 @@
1
+ import type { MutateOptions } from '../array/array.util.js'
1
2
  import { _assert } from '../error/assert.js'
2
3
  import { _ms } from '../time/time.util.js'
3
4
  import type {
@@ -189,8 +190,8 @@ export class LocalTime {
189
190
  return this.$date.getSeconds()
190
191
  }
191
192
 
192
- set(unit: LocalTimeUnit, v: number, mutate = false): LocalTime {
193
- const t = mutate ? this : this.clone()
193
+ set(unit: LocalTimeUnit, v: number, opt: MutateOptions = {}): LocalTime {
194
+ const t = opt.mutate ? this : this.clone()
194
195
 
195
196
  if (unit === 'year') {
196
197
  t.$date.setFullYear(v)
@@ -205,7 +206,7 @@ export class LocalTime {
205
206
  } else if (unit === 'second') {
206
207
  t.$date.setSeconds(v)
207
208
  } else if (unit === 'week') {
208
- setWeek(t.$date, v, true)
209
+ setWeek(t.$date, v, { mutate: true })
209
210
  }
210
211
 
211
212
  return t
@@ -297,8 +298,8 @@ export class LocalTime {
297
298
  return this.plus(delta, 'day')
298
299
  }
299
300
 
300
- setComponents(c: Partial<DateTimeObject>, mutate = false): LocalTime {
301
- const d = mutate ? this.$date : new Date(this.$date)
301
+ setComponents(c: Partial<DateTimeObject>, opt: MutateOptions = {}): LocalTime {
302
+ const d = opt.mutate ? this.$date : new Date(this.$date)
302
303
 
303
304
  // Year, month and day set all-at-once, to avoid 30/31 (and 28/29) mishap
304
305
  if (c.day || c.month !== undefined || c.year !== undefined) {
@@ -319,7 +320,7 @@ export class LocalTime {
319
320
  d.setSeconds(c.second)
320
321
  }
321
322
 
322
- return mutate ? this : new LocalTime(d)
323
+ return opt.mutate ? this : new LocalTime(d)
323
324
  }
324
325
 
325
326
  plusSeconds(num: number): LocalTime {
@@ -378,22 +379,22 @@ export class LocalTime {
378
379
  return this.plus(-num, 'year')
379
380
  }
380
381
 
381
- plus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
382
+ plus(num: number, unit: LocalTimeUnit, opt: MutateOptions = {}): LocalTime {
382
383
  if (unit === 'week') {
383
384
  num *= 7
384
385
  unit = 'day'
385
386
  }
386
387
 
387
388
  if (unit === 'year' || unit === 'month') {
388
- const d = addMonths(this.$date, unit === 'month' ? num : num * 12, mutate)
389
- return mutate ? this : localTime.fromInput(d)
389
+ const d = addMonths(this.$date, unit === 'month' ? num : num * 12, opt)
390
+ return opt.mutate ? this : localTime.fromInput(d)
390
391
  }
391
392
 
392
- return this.set(unit, this.get(unit) + num, mutate)
393
+ return this.set(unit, this.get(unit) + num, opt)
393
394
  }
394
395
 
395
- minus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
396
- return this.plus(num * -1, unit, mutate)
396
+ minus(num: number, unit: LocalTimeUnit, opt: MutateOptions = {}): LocalTime {
397
+ return this.plus(num * -1, unit, opt)
397
398
  }
398
399
 
399
400
  absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number {
@@ -429,9 +430,9 @@ export class LocalTime {
429
430
  return Math.trunc(r) || 0
430
431
  }
431
432
 
432
- startOf(unit: LocalTimeUnit, mutate = false): LocalTime {
433
+ startOf(unit: LocalTimeUnit, opt: MutateOptions = {}): LocalTime {
433
434
  if (unit === 'second') return this
434
- const d = mutate ? this.$date : new Date(this.$date)
435
+ const d = opt.mutate ? this.$date : new Date(this.$date)
435
436
  d.setSeconds(0, 0)
436
437
 
437
438
  if (unit !== 'minute') {
@@ -448,18 +449,18 @@ export class LocalTime {
448
449
  d.setDate(1)
449
450
  } else {
450
451
  // week
451
- startOfWeek(d, true)
452
+ startOfWeek(d, { mutate: true })
452
453
  }
453
454
  }
454
455
  }
455
456
  }
456
457
 
457
- return mutate ? this : new LocalTime(d)
458
+ return opt.mutate ? this : new LocalTime(d)
458
459
  }
459
460
 
460
- endOf(unit: LocalTimeUnit, mutate = false): LocalTime {
461
+ endOf(unit: LocalTimeUnit, opt: MutateOptions = {}): LocalTime {
461
462
  if (unit === 'second') return this
462
- const d = mutate ? this.$date : new Date(this.$date)
463
+ const d = opt.mutate ? this.$date : new Date(this.$date)
463
464
  d.setSeconds(59, 0)
464
465
 
465
466
  if (unit !== 'minute') {
@@ -474,7 +475,7 @@ export class LocalTime {
474
475
  }
475
476
 
476
477
  if (unit === 'week') {
477
- endOfWeek(d, true)
478
+ endOfWeek(d, { mutate: true })
478
479
  } else {
479
480
  // year or month
480
481
  const lastDay = localDate.getMonthLength(d.getFullYear(), d.getMonth() + 1)
@@ -484,7 +485,7 @@ export class LocalTime {
484
485
  }
485
486
  }
486
487
 
487
- return mutate ? this : new LocalTime(d)
488
+ return opt.mutate ? this : new LocalTime(d)
488
489
  }
489
490
 
490
491
  /**
@@ -1003,9 +1004,9 @@ class LocalTimeFactory {
1003
1004
  return Intl.supportedValuesOf('timeZone').includes(tz)
1004
1005
  }
1005
1006
 
1006
- sort(items: LocalTime[], dir: SortDirection = 'asc', mutate = false): LocalTime[] {
1007
+ sort(items: LocalTime[], dir: SortDirection = 'asc', opt: MutateOptions = {}): LocalTime[] {
1007
1008
  const mod = dir === 'desc' ? -1 : 1
1008
- return (mutate ? items : [...items]).sort((a, b) => {
1009
+ return (opt.mutate ? items : [...items]).sort((a, b) => {
1009
1010
  const v1 = a.$date.valueOf()
1010
1011
  const v2 = b.$date.valueOf()
1011
1012
  if (v1 === v2) return 0
@@ -1056,8 +1057,8 @@ function getWeek(date: Date): number {
1056
1057
  return Math.round(diff / MILLISECONDS_IN_WEEK) + 1
1057
1058
  }
1058
1059
 
1059
- function setWeek(date: Date, week: number, mutate = false): Date {
1060
- const d = mutate ? date : new Date(date)
1060
+ function setWeek(date: Date, week: number, opt: MutateOptions = {}): Date {
1061
+ const d = opt.mutate ? date : new Date(date)
1061
1062
  const diff = getWeek(d) - week
1062
1063
  d.setDate(d.getDate() - diff * 7)
1063
1064
  return d
@@ -1069,7 +1070,7 @@ function startOfWeekYear(date: Date): Date {
1069
1070
  const fourthOfJanuary = new Date(0)
1070
1071
  fourthOfJanuary.setFullYear(year, 0, 4)
1071
1072
  fourthOfJanuary.setHours(0, 0, 0, 0)
1072
- return startOfWeek(fourthOfJanuary, true)
1073
+ return startOfWeek(fourthOfJanuary, { mutate: true })
1073
1074
  }
1074
1075
 
1075
1076
  // based on: https://github.com/date-fns/date-fns/blob/fd6bb1a0bab143f2da068c05a9c562b9bee1357d/src/getISOWeekYear/index.ts
@@ -1079,12 +1080,12 @@ function getWeekYear(date: Date): number {
1079
1080
  const fourthOfJanuaryOfNextYear = new Date(0)
1080
1081
  fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4)
1081
1082
  fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0)
1082
- const startOfNextYear = startOfWeek(fourthOfJanuaryOfNextYear, true)
1083
+ const startOfNextYear = startOfWeek(fourthOfJanuaryOfNextYear, { mutate: true })
1083
1084
 
1084
1085
  const fourthOfJanuaryOfThisYear = new Date(0)
1085
1086
  fourthOfJanuaryOfThisYear.setFullYear(year, 0, 4)
1086
1087
  fourthOfJanuaryOfThisYear.setHours(0, 0, 0, 0)
1087
- const startOfThisYear = startOfWeek(fourthOfJanuaryOfThisYear, true)
1088
+ const startOfThisYear = startOfWeek(fourthOfJanuaryOfThisYear, { mutate: true })
1088
1089
 
1089
1090
  if (date.getTime() >= startOfNextYear.getTime()) {
1090
1091
  return year + 1
@@ -1096,8 +1097,8 @@ function getWeekYear(date: Date): number {
1096
1097
  }
1097
1098
 
1098
1099
  // based on: https://github.com/date-fns/date-fns/blob/fd6bb1a0bab143f2da068c05a9c562b9bee1357d/src/startOfWeek/index.ts
1099
- function startOfWeek(date: Date, mutate = false): Date {
1100
- const d = mutate ? date : new Date(date)
1100
+ function startOfWeek(date: Date, opt: MutateOptions = {}): Date {
1101
+ const d = opt.mutate ? date : new Date(date)
1101
1102
 
1102
1103
  const day = d.getDay()
1103
1104
  const diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn
@@ -1108,8 +1109,8 @@ function startOfWeek(date: Date, mutate = false): Date {
1108
1109
  }
1109
1110
 
1110
1111
  // based on: https://github.com/date-fns/date-fns/blob/master/src/endOfWeek/index.ts
1111
- function endOfWeek(date: Date, mutate = false): Date {
1112
- const d = mutate ? date : new Date(date)
1112
+ function endOfWeek(date: Date, opt: MutateOptions = {}): Date {
1113
+ const d = opt.mutate ? date : new Date(date)
1113
1114
 
1114
1115
  const day = d.getDay()
1115
1116
  const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn)
@@ -1118,8 +1119,8 @@ function endOfWeek(date: Date, mutate = false): Date {
1118
1119
  return d
1119
1120
  }
1120
1121
 
1121
- function addMonths(d: Date, num: number, mutate = false): Date {
1122
- if (!mutate) d = new Date(d)
1122
+ function addMonths(d: Date, num: number, opt: MutateOptions = {}): Date {
1123
+ if (!opt.mutate) d = new Date(d)
1123
1124
 
1124
1125
  let day = d.getDate()
1125
1126
  let month = d.getMonth() + 1 + num
@@ -1,4 +1,6 @@
1
1
  // copy-pasted to avoid weird circular dependency
2
+ import type { MutateOptions } from '../array/array.util.js'
3
+
2
4
  const _noop = (..._args: any[]): undefined => undefined
3
5
 
4
6
  /**
@@ -57,10 +59,10 @@ export const commonLoggerNoop: CommonLogger = {
57
59
  export function commonLoggerMinLevel(
58
60
  logger: CommonLogger,
59
61
  minLevel: CommonLogLevel,
60
- mutate = false,
62
+ opt: MutateOptions = {},
61
63
  ): CommonLogger {
62
64
  const level = commonLogLevelNumber[minLevel]
63
- if (mutate) {
65
+ if (opt.mutate) {
64
66
  if (level > commonLogLevelNumber['log']) {
65
67
  logger.log = _noop
66
68
  if (level > commonLogLevelNumber['warn']) {
@@ -1,4 +1,5 @@
1
- import type { Inclusiveness, SortDirection } from '../types.js'
1
+ import type { SortOptions } from '../array/array.util.js'
2
+ import type { Inclusiveness } from '../types.js'
2
3
 
3
4
  export function _randomInt(minIncl: number, maxIncl: number): number {
4
5
  return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl)
@@ -63,13 +64,9 @@ export function _clamp(x: number, minIncl: number, maxIncl: number): number {
63
64
  * _sortNumbers([1, 3, 2])
64
65
  * // [1, 2, 3]
65
66
  */
66
- export function _sortNumbers(
67
- numbers: number[],
68
- mutate = false,
69
- dir: SortDirection = 'asc',
70
- ): number[] {
71
- const mod = dir === 'desc' ? -1 : 1
72
- return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
67
+ export function _sortNumbers(numbers: number[], opt: SortOptions = {}): number[] {
68
+ const mod = opt.dir === 'desc' ? -1 : 1
69
+ return (opt.mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
73
70
  }
74
71
 
75
72
  /**
package/src/semver.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import type { SortOptions } from './array/array.util.js'
1
2
  import { _range } from './array/range.js'
2
3
  import { _assert } from './error/assert.js'
3
- import type { SortDirection } from './types.js'
4
4
 
5
5
  export type SemverInput = string | Semver
6
6
  export type SemverInputNullable = SemverInput | null | undefined
@@ -134,9 +134,9 @@ class SemverFactory {
134
134
  /**
135
135
  * Sorts an array of Semvers in `dir` order (ascending by default).
136
136
  */
137
- sort(items: Semver[], dir: SortDirection = 'asc', mutate = false): Semver[] {
138
- const mod = dir === 'desc' ? -1 : 1
139
- return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
137
+ sort(items: Semver[], opt: SortOptions = {}): Semver[] {
138
+ const mod = opt.dir === 'desc' ? -1 : 1
139
+ return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
140
140
  }
141
141
  }
142
142