@naturalcycles/js-lib 14.183.1 → 14.184.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.
@@ -1,4 +1,4 @@
1
- import type { IsoDateString, IsoDateTimeString, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
1
+ import type { IsoDateString, IsoDateTimeString, MonthId, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
2
2
  import { LocalTime } from './localTime';
3
3
  export type LocalDateUnit = LocalDateUnitStrict | 'week';
4
4
  export type LocalDateUnitStrict = 'year' | 'month' | 'day';
@@ -49,6 +49,14 @@ export declare class LocalDate {
49
49
  isAfter(d: LocalDateInput, inclusive?: boolean): boolean;
50
50
  isSameOrAfter(d: LocalDateInput): boolean;
51
51
  isBetween(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness): boolean;
52
+ /**
53
+ * Checks if this localDate is older than "today" by X units.
54
+ *
55
+ * Example:
56
+ *
57
+ * localDate(expirationDate).isOlderThan(5, 'day')
58
+ */
59
+ isOlderThan(n: number, unit: LocalDateUnitStrict): boolean;
52
60
  /**
53
61
  * Returns 1 if this > d
54
62
  * returns 0 if they are equal
@@ -88,6 +96,7 @@ export declare class LocalDate {
88
96
  toISODateTime(): IsoDateTimeString;
89
97
  toString(): IsoDateString;
90
98
  toStringCompact(): string;
99
+ toMonthId(): MonthId;
91
100
  unix(): UnixTimestampNumber;
92
101
  unixMillis(): UnixTimestampMillisNumber;
93
102
  toJSON(): IsoDateString;
@@ -179,6 +179,16 @@ class LocalDate {
179
179
  return false;
180
180
  return true;
181
181
  }
182
+ /**
183
+ * Checks if this localDate is older than "today" by X units.
184
+ *
185
+ * Example:
186
+ *
187
+ * localDate(expirationDate).isOlderThan(5, 'day')
188
+ */
189
+ isOlderThan(n, unit) {
190
+ return this.isBefore(LocalDate.today().add(-n, unit));
191
+ }
182
192
  /**
183
193
  * Returns 1 if this > d
184
194
  * returns 0 if they are equal
@@ -393,6 +403,9 @@ class LocalDate {
393
403
  String(this.$day).padStart(2, '0'),
394
404
  ].join('');
395
405
  }
406
+ toMonthId() {
407
+ return this.toString().slice(0, 7);
408
+ }
396
409
  // May be not optimal, as LocalTime better suits it
397
410
  unix() {
398
411
  return Math.floor(this.toDate().valueOf() / 1000);
@@ -1,4 +1,4 @@
1
- import type { IsoDateString, IsoDateTimeString, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
1
+ import type { IsoDateString, IsoDateTimeString, MonthId, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
2
2
  import type { Inclusiveness } from './localDate';
3
3
  import { LocalDate } from './localDate';
4
4
  export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
@@ -87,6 +87,14 @@ export declare class LocalTime {
87
87
  isAfter(d: LocalTimeInput, inclusive?: boolean): boolean;
88
88
  isSameOrAfter(d: LocalTimeInput): boolean;
89
89
  isBetween(min: LocalTimeInput, max: LocalTimeInput, incl?: Inclusiveness): boolean;
90
+ /**
91
+ * Checks if this localTime is older than "now" by X units.
92
+ *
93
+ * Example:
94
+ *
95
+ * localTime(expirationDate).isOlderThan(5, 'day')
96
+ */
97
+ isOlderThan(n: number, unit: LocalTimeUnit): boolean;
90
98
  /**
91
99
  * Returns 1 if this > d
92
100
  * returns 0 if they are equal
@@ -120,6 +128,7 @@ export declare class LocalTime {
120
128
  toStringCompact(seconds?: boolean): string;
121
129
  toString(): string;
122
130
  toJSON(): UnixTimestampNumber;
131
+ toMonthId(): MonthId;
123
132
  format(fmt: Intl.DateTimeFormat | LocalTimeFormatter): string;
124
133
  }
125
134
  /**
@@ -357,6 +357,16 @@ class LocalTime {
357
357
  return false;
358
358
  return true;
359
359
  }
360
+ /**
361
+ * Checks if this localTime is older than "now" by X units.
362
+ *
363
+ * Example:
364
+ *
365
+ * localTime(expirationDate).isOlderThan(5, 'day')
366
+ */
367
+ isOlderThan(n, unit) {
368
+ return this.isBefore(LocalTime.now().add(-n, unit));
369
+ }
360
370
  /**
361
371
  * Returns 1 if this > d
362
372
  * returns 0 if they are equal
@@ -480,6 +490,9 @@ class LocalTime {
480
490
  toJSON() {
481
491
  return this.unix();
482
492
  }
493
+ toMonthId() {
494
+ return this.$date.toISOString().slice(0, 7);
495
+ }
483
496
  format(fmt) {
484
497
  if (fmt instanceof Intl.DateTimeFormat) {
485
498
  return fmt.format(this.$date);
package/dist/types.d.ts CHANGED
@@ -153,6 +153,13 @@ export type IsoDateString = string;
153
153
  * @example '2019-06-21T05:21:73Z'
154
154
  */
155
155
  export type IsoDateTimeString = string;
156
+ /**
157
+ * Identifies the Month.
158
+ * Like IsoDateString, but without the Day token.
159
+ *
160
+ * @example '2023-09'
161
+ */
162
+ export type MonthId = string;
156
163
  /**
157
164
  * Interface explicitly states that the value is a Unix timestamp (in seconds).
158
165
  *
@@ -176,6 +176,16 @@ export class LocalDate {
176
176
  return false;
177
177
  return true;
178
178
  }
179
+ /**
180
+ * Checks if this localDate is older than "today" by X units.
181
+ *
182
+ * Example:
183
+ *
184
+ * localDate(expirationDate).isOlderThan(5, 'day')
185
+ */
186
+ isOlderThan(n, unit) {
187
+ return this.isBefore(LocalDate.today().add(-n, unit));
188
+ }
179
189
  /**
180
190
  * Returns 1 if this > d
181
191
  * returns 0 if they are equal
@@ -390,6 +400,9 @@ export class LocalDate {
390
400
  String(this.$day).padStart(2, '0'),
391
401
  ].join('');
392
402
  }
403
+ toMonthId() {
404
+ return this.toString().slice(0, 7);
405
+ }
393
406
  // May be not optimal, as LocalTime better suits it
394
407
  unix() {
395
408
  return Math.floor(this.toDate().valueOf() / 1000);
@@ -355,6 +355,16 @@ export class LocalTime {
355
355
  return false;
356
356
  return true;
357
357
  }
358
+ /**
359
+ * Checks if this localTime is older than "now" by X units.
360
+ *
361
+ * Example:
362
+ *
363
+ * localTime(expirationDate).isOlderThan(5, 'day')
364
+ */
365
+ isOlderThan(n, unit) {
366
+ return this.isBefore(LocalTime.now().add(-n, unit));
367
+ }
358
368
  /**
359
369
  * Returns 1 if this > d
360
370
  * returns 0 if they are equal
@@ -478,6 +488,9 @@ export class LocalTime {
478
488
  toJSON() {
479
489
  return this.unix();
480
490
  }
491
+ toMonthId() {
492
+ return this.$date.toISOString().slice(0, 7);
493
+ }
481
494
  format(fmt) {
482
495
  if (fmt instanceof Intl.DateTimeFormat) {
483
496
  return fmt.format(this.$date);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.183.1",
3
+ "version": "14.184.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -2,6 +2,7 @@ import { _assert } from '../error/assert'
2
2
  import type {
3
3
  IsoDateString,
4
4
  IsoDateTimeString,
5
+ MonthId,
5
6
  UnixTimestampMillisNumber,
6
7
  UnixTimestampNumber,
7
8
  } from '../types'
@@ -239,6 +240,17 @@ export class LocalDate {
239
240
  return true
240
241
  }
241
242
 
243
+ /**
244
+ * Checks if this localDate is older than "today" by X units.
245
+ *
246
+ * Example:
247
+ *
248
+ * localDate(expirationDate).isOlderThan(5, 'day')
249
+ */
250
+ isOlderThan(n: number, unit: LocalDateUnitStrict): boolean {
251
+ return this.isBefore(LocalDate.today().add(-n, unit))
252
+ }
253
+
242
254
  /**
243
255
  * Returns 1 if this > d
244
256
  * returns 0 if they are equal
@@ -482,6 +494,10 @@ export class LocalDate {
482
494
  ].join('')
483
495
  }
484
496
 
497
+ toMonthId(): MonthId {
498
+ return this.toString().slice(0, 7)
499
+ }
500
+
485
501
  // May be not optimal, as LocalTime better suits it
486
502
  unix(): UnixTimestampNumber {
487
503
  return Math.floor(this.toDate().valueOf() / 1000)
@@ -3,6 +3,7 @@ import { _ms } from '../time/time.util'
3
3
  import type {
4
4
  IsoDateString,
5
5
  IsoDateTimeString,
6
+ MonthId,
6
7
  UnixTimestampMillisNumber,
7
8
  UnixTimestampNumber,
8
9
  } from '../types'
@@ -439,6 +440,17 @@ export class LocalTime {
439
440
  return true
440
441
  }
441
442
 
443
+ /**
444
+ * Checks if this localTime is older than "now" by X units.
445
+ *
446
+ * Example:
447
+ *
448
+ * localTime(expirationDate).isOlderThan(5, 'day')
449
+ */
450
+ isOlderThan(n: number, unit: LocalTimeUnit): boolean {
451
+ return this.isBefore(LocalTime.now().add(-n, unit))
452
+ }
453
+
442
454
  /**
443
455
  * Returns 1 if this > d
444
456
  * returns 0 if they are equal
@@ -587,6 +599,10 @@ export class LocalTime {
587
599
  return this.unix()
588
600
  }
589
601
 
602
+ toMonthId(): MonthId {
603
+ return this.$date.toISOString().slice(0, 7)
604
+ }
605
+
590
606
  format(fmt: Intl.DateTimeFormat | LocalTimeFormatter): string {
591
607
  if (fmt instanceof Intl.DateTimeFormat) {
592
608
  return fmt.format(this.$date)
package/src/types.ts CHANGED
@@ -214,6 +214,14 @@ export type IsoDateString = string
214
214
  */
215
215
  export type IsoDateTimeString = string
216
216
 
217
+ /**
218
+ * Identifies the Month.
219
+ * Like IsoDateString, but without the Day token.
220
+ *
221
+ * @example '2023-09'
222
+ */
223
+ export type MonthId = string
224
+
217
225
  /**
218
226
  * Interface explicitly states that the value is a Unix timestamp (in seconds).
219
227
  *