@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.
- package/dist/array/array.util.d.ts +10 -1
- package/dist/array/array.util.js +36 -2
- package/dist/datetime/localDate.d.ts +13 -10
- package/dist/datetime/localDate.js +47 -22
- package/dist/datetime/localTime.d.ts +24 -8
- package/dist/datetime/localTime.js +228 -78
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -0
- package/dist/vendor/is.d.ts +2 -0
- package/dist-esm/array/array.util.js +30 -1
- package/dist-esm/datetime/localDate.js +47 -22
- package/dist-esm/datetime/localTime.js +227 -77
- package/dist-esm/index.js +1 -0
- package/package.json +1 -1
- package/src/array/array.util.ts +30 -1
- package/src/datetime/localDate.ts +65 -31
- package/src/datetime/localTime.ts +260 -91
- package/src/index.ts +18 -2
|
@@ -2,13 +2,15 @@ import { _assert } from '../error/assert'
|
|
|
2
2
|
import { IsoDateString, IsoDateTimeString, UnixTimestampNumber } from '../types'
|
|
3
3
|
import { LocalTime } from './localTime'
|
|
4
4
|
|
|
5
|
-
export type LocalDateUnit =
|
|
5
|
+
export type LocalDateUnit = LocalDateUnitStrict | 'week'
|
|
6
|
+
export type LocalDateUnitStrict = 'year' | 'month' | 'day'
|
|
6
7
|
export type Inclusiveness = '()' | '[]' | '[)' | '(]'
|
|
7
8
|
|
|
8
9
|
const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
9
10
|
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
|
|
10
11
|
|
|
11
12
|
export type LocalDateConfig = LocalDate | IsoDateString
|
|
13
|
+
export type LocalDateFormatter = (ld: LocalDate) => string
|
|
12
14
|
|
|
13
15
|
/* eslint-disable no-dupe-class-members */
|
|
14
16
|
|
|
@@ -106,24 +108,28 @@ export class LocalDate {
|
|
|
106
108
|
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod)
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
static earliestOrUndefined(items:
|
|
111
|
+
static earliestOrUndefined(items: LocalDateConfig[]): LocalDate | undefined {
|
|
110
112
|
return items.length ? LocalDate.earliest(items) : undefined
|
|
111
113
|
}
|
|
112
114
|
|
|
113
|
-
static earliest(items:
|
|
115
|
+
static earliest(items: LocalDateConfig[]): LocalDate {
|
|
114
116
|
_assert(items.length, 'LocalDate.earliest called on empty array')
|
|
115
117
|
|
|
116
|
-
return items
|
|
118
|
+
return items
|
|
119
|
+
.map(i => LocalDate.of(i))
|
|
120
|
+
.reduce((min, item) => (min.isSameOrBefore(item) ? min : item))
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
static latestOrUndefined(items:
|
|
123
|
+
static latestOrUndefined(items: LocalDateConfig[]): LocalDate | undefined {
|
|
120
124
|
return items.length ? LocalDate.latest(items) : undefined
|
|
121
125
|
}
|
|
122
126
|
|
|
123
|
-
static latest(items:
|
|
127
|
+
static latest(items: LocalDateConfig[]): LocalDate {
|
|
124
128
|
_assert(items.length, 'LocalDate.latest called on empty array')
|
|
125
129
|
|
|
126
|
-
return items
|
|
130
|
+
return items
|
|
131
|
+
.map(i => LocalDate.of(i))
|
|
132
|
+
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
|
|
127
133
|
}
|
|
128
134
|
|
|
129
135
|
static range(
|
|
@@ -133,6 +139,11 @@ export class LocalDate {
|
|
|
133
139
|
step = 1,
|
|
134
140
|
stepUnit: LocalDateUnit = 'day',
|
|
135
141
|
): LocalDate[] {
|
|
142
|
+
if (stepUnit === 'week') {
|
|
143
|
+
step *= 7
|
|
144
|
+
stepUnit = 'day'
|
|
145
|
+
}
|
|
146
|
+
|
|
136
147
|
const dates: LocalDate[] = []
|
|
137
148
|
const $min = LocalDate.of(min)
|
|
138
149
|
const $max = LocalDate.of(max).startOf(stepUnit)
|
|
@@ -153,11 +164,11 @@ export class LocalDate {
|
|
|
153
164
|
return dates
|
|
154
165
|
}
|
|
155
166
|
|
|
156
|
-
get(unit:
|
|
167
|
+
get(unit: LocalDateUnitStrict): number {
|
|
157
168
|
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day
|
|
158
169
|
}
|
|
159
170
|
|
|
160
|
-
set(unit:
|
|
171
|
+
set(unit: LocalDateUnitStrict, v: number, mutate = false): LocalDate {
|
|
161
172
|
const t = mutate ? this : this.clone()
|
|
162
173
|
|
|
163
174
|
if (unit === 'year') {
|
|
@@ -242,50 +253,69 @@ export class LocalDate {
|
|
|
242
253
|
}
|
|
243
254
|
|
|
244
255
|
/**
|
|
245
|
-
* Returns the number of **full** units difference (aka `Math.
|
|
256
|
+
* Returns the number of **full** units difference (aka `Math.floor`).
|
|
246
257
|
*
|
|
247
258
|
* a.diff(b) means "a minus b"
|
|
248
259
|
*/
|
|
249
260
|
diff(d: LocalDateConfig, unit: LocalDateUnit): number {
|
|
250
261
|
d = LocalDate.of(d)
|
|
251
262
|
|
|
263
|
+
const sign = this.cmp(d)
|
|
264
|
+
if (!sign) return 0
|
|
265
|
+
|
|
266
|
+
// Put items in descending order: "big minus small"
|
|
267
|
+
const [big, small] = sign === 1 ? [this, d] : [d, this]
|
|
268
|
+
|
|
252
269
|
if (unit === 'year') {
|
|
253
|
-
|
|
270
|
+
let years = big.$year - small.$year
|
|
271
|
+
|
|
272
|
+
if (big.$month < small.$month || (big.$month === small.$month && big.$day < small.$day)) {
|
|
273
|
+
years--
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return years * sign || 0
|
|
254
277
|
}
|
|
255
278
|
|
|
256
279
|
if (unit === 'month') {
|
|
257
|
-
|
|
280
|
+
let months = (big.$year - small.$year) * 12 + (big.$month - small.$month)
|
|
281
|
+
if (big.$day < small.$day) months--
|
|
282
|
+
return months * sign || 0
|
|
258
283
|
}
|
|
259
284
|
|
|
260
|
-
// unit is 'day'
|
|
261
|
-
let days =
|
|
285
|
+
// unit is 'day' or 'week'
|
|
286
|
+
let days = big.$day - small.$day
|
|
262
287
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
} else if (this.$year < d.$year) {
|
|
268
|
-
for (let year = this.$year; year < d.$year; year++) {
|
|
269
|
-
days -= LocalDate.getYearLength(year)
|
|
270
|
-
}
|
|
288
|
+
// If small date is after 1st of March - next year's "leapness" should be used
|
|
289
|
+
const offsetYear = small.$month >= 3 ? 1 : 0
|
|
290
|
+
for (let year = small.$year; year < big.$year; year++) {
|
|
291
|
+
days += LocalDate.getYearLength(year + offsetYear)
|
|
271
292
|
}
|
|
272
293
|
|
|
273
|
-
if (
|
|
274
|
-
for (let month =
|
|
275
|
-
days += LocalDate.getMonthLength(
|
|
294
|
+
if (small.$month < big.$month) {
|
|
295
|
+
for (let month = small.$month; month < big.$month; month++) {
|
|
296
|
+
days += LocalDate.getMonthLength(big.$year, month)
|
|
276
297
|
}
|
|
277
|
-
} else if (
|
|
278
|
-
for (let month =
|
|
279
|
-
days -= LocalDate.getMonthLength(
|
|
298
|
+
} else if (big.$month < small.$month) {
|
|
299
|
+
for (let month = big.$month; month < small.$month; month++) {
|
|
300
|
+
days -= LocalDate.getMonthLength(big.$year, month)
|
|
280
301
|
}
|
|
281
302
|
}
|
|
282
303
|
|
|
283
|
-
|
|
304
|
+
if (unit === 'week') {
|
|
305
|
+
return Math.trunc(days / 7) * sign || 0
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return days * sign || 0
|
|
284
309
|
}
|
|
285
310
|
|
|
286
311
|
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
287
312
|
let { $day, $month, $year } = this
|
|
288
313
|
|
|
314
|
+
if (unit === 'week') {
|
|
315
|
+
num *= 7
|
|
316
|
+
unit = 'day'
|
|
317
|
+
}
|
|
318
|
+
|
|
289
319
|
if (unit === 'day') {
|
|
290
320
|
$day += num
|
|
291
321
|
} else if (unit === 'month') {
|
|
@@ -346,14 +376,14 @@ export class LocalDate {
|
|
|
346
376
|
return this.add(-num, unit, mutate)
|
|
347
377
|
}
|
|
348
378
|
|
|
349
|
-
startOf(unit:
|
|
379
|
+
startOf(unit: LocalDateUnitStrict): LocalDate {
|
|
350
380
|
if (unit === 'day') return this
|
|
351
381
|
if (unit === 'month') return LocalDate.create(this.$year, this.$month, 1)
|
|
352
382
|
// year
|
|
353
383
|
return LocalDate.create(this.$year, 1, 1)
|
|
354
384
|
}
|
|
355
385
|
|
|
356
|
-
endOf(unit:
|
|
386
|
+
endOf(unit: LocalDateUnitStrict): LocalDate {
|
|
357
387
|
if (unit === 'day') return this
|
|
358
388
|
if (unit === 'month')
|
|
359
389
|
return LocalDate.create(
|
|
@@ -435,6 +465,10 @@ export class LocalDate {
|
|
|
435
465
|
toJSON(): IsoDateString {
|
|
436
466
|
return this.toString()
|
|
437
467
|
}
|
|
468
|
+
|
|
469
|
+
format(fmt: LocalDateFormatter): string {
|
|
470
|
+
return fmt(this)
|
|
471
|
+
}
|
|
438
472
|
}
|
|
439
473
|
|
|
440
474
|
/**
|