@naturalcycles/js-lib 14.88.0 → 14.91.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.
@@ -0,0 +1,80 @@
1
+ import { Sequence } from '../seq/seq';
2
+ import { IsoDate } from '../types';
3
+ export declare type LocalDateUnit = 'year' | 'month' | 'day';
4
+ export declare type LocalDateConfig = LocalDate | string;
5
+ /**
6
+ * @experimental
7
+ */
8
+ export declare class LocalDate {
9
+ year: number;
10
+ month: number;
11
+ day: number;
12
+ private constructor();
13
+ static create(year: number, month: number, day: number): LocalDate;
14
+ /**
15
+ * Parses input String into LocalDate.
16
+ * Input can already be a LocalDate - it is returned as-is in that case.
17
+ */
18
+ static of(d: LocalDateConfig): LocalDate;
19
+ static parseCompact(d: string): LocalDate;
20
+ static fromDate(d: Date): LocalDate;
21
+ /**
22
+ * Returns null if invalid.
23
+ */
24
+ static parseOrNull(d: LocalDateConfig): LocalDate | null;
25
+ static isValid(iso: string): boolean;
26
+ static today(): LocalDate;
27
+ static sort(items: LocalDate[], mutate?: boolean, descending?: boolean): LocalDate[];
28
+ static earliestOrUndefined(items: LocalDate[]): LocalDate | undefined;
29
+ static earliest(items: LocalDate[]): LocalDate;
30
+ static latestOrUndefined(items: LocalDate[]): LocalDate | undefined;
31
+ static latest(items: LocalDate[]): LocalDate;
32
+ static range(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
33
+ static rangeSeq(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): Sequence<LocalDate>;
34
+ static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDate[];
35
+ static rangeIncl(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
36
+ static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDate[];
37
+ isSame(d: LocalDateConfig): boolean;
38
+ isBefore(d: LocalDateConfig): boolean;
39
+ isSameOrBefore(d: LocalDateConfig): boolean;
40
+ isAfter(d: LocalDateConfig): boolean;
41
+ isSameOrAfter(d: LocalDateConfig): boolean;
42
+ /**
43
+ * Returns 1 if this > d
44
+ * returns 0 if they are equal
45
+ * returns -1 if this < d
46
+ */
47
+ cmp(d: LocalDateConfig): -1 | 0 | 1;
48
+ /**
49
+ * Same as Math.abs( diff )
50
+ */
51
+ absDiff(d: LocalDateConfig, unit: LocalDateUnit): number;
52
+ /**
53
+ * Returns the number of **full** units difference (aka `Math.ceil`).
54
+ *
55
+ * a.diff(b) means "a minus b"
56
+ */
57
+ diff(d: LocalDateConfig, unit: LocalDateUnit): number;
58
+ add(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
59
+ subtract(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
60
+ startOf(unit: LocalDateUnit): LocalDate;
61
+ endOf(unit: LocalDateUnit): LocalDate;
62
+ static getYearLength(year: number): number;
63
+ static getMonthLength(year: number, month: number): number;
64
+ static isLeapYear(year: number): boolean;
65
+ clone(): LocalDate;
66
+ /**
67
+ * Converts LocalDate into instance of Date.
68
+ * Year, month and day will match.
69
+ * Hour, minute, second, ms will be 0.
70
+ * Timezone will match local timezone.
71
+ */
72
+ toDate(): Date;
73
+ toString(): IsoDate;
74
+ toStringCompact(): string;
75
+ toJSON(): IsoDate;
76
+ }
77
+ /**
78
+ * Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
79
+ */
80
+ export declare function localDate(d?: LocalDateConfig): LocalDate;
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.localDate = exports.LocalDate = void 0;
4
+ const assert_1 = require("../error/assert");
5
+ const seq_1 = require("../seq/seq");
6
+ const types_1 = require("../types");
7
+ const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
8
+ /**
9
+ * @experimental
10
+ */
11
+ class LocalDate {
12
+ constructor(year, month, day) {
13
+ this.year = year;
14
+ this.month = month;
15
+ this.day = day;
16
+ }
17
+ static create(year, month, day) {
18
+ return new LocalDate(year, month, day);
19
+ }
20
+ /**
21
+ * Parses input String into LocalDate.
22
+ * Input can already be a LocalDate - it is returned as-is in that case.
23
+ */
24
+ static of(d) {
25
+ const t = this.parseOrNull(d);
26
+ if (t === null) {
27
+ throw new Error(`Cannot parse "${d}" into LocalDate`);
28
+ }
29
+ return t;
30
+ }
31
+ static parseCompact(d) {
32
+ const [year, month, day] = [d.slice(0, 4), d.slice(4, 2), d.slice(6, 2)].map(Number);
33
+ if (!day || !month || !year) {
34
+ throw new Error(`Cannot parse "${d}" into LocalDate`);
35
+ }
36
+ return new LocalDate(year, month, day);
37
+ }
38
+ static fromDate(d) {
39
+ return new LocalDate(d.getFullYear(), d.getMonth() + 1, d.getDate());
40
+ }
41
+ /**
42
+ * Returns null if invalid.
43
+ */
44
+ static parseOrNull(d) {
45
+ if (d instanceof LocalDate)
46
+ return d;
47
+ // todo: explore more performant options
48
+ const [year, month, day] = d.slice(0, 10).split('-').map(Number);
49
+ if (!year ||
50
+ !month ||
51
+ month < 1 ||
52
+ month > 12 ||
53
+ !day ||
54
+ day < 1 ||
55
+ day > this.getMonthLength(year, month)) {
56
+ return null;
57
+ }
58
+ return new LocalDate(year, month, day);
59
+ }
60
+ static isValid(iso) {
61
+ return this.parseOrNull(iso) !== null;
62
+ }
63
+ static today() {
64
+ return this.fromDate(new Date());
65
+ }
66
+ static sort(items, mutate = false, descending = false) {
67
+ const mod = descending ? -1 : 1;
68
+ return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
69
+ }
70
+ static earliestOrUndefined(items) {
71
+ return items.length ? LocalDate.earliest(items) : undefined;
72
+ }
73
+ static earliest(items) {
74
+ (0, assert_1._assert)(items.length, 'LocalDate.earliest called on empty array');
75
+ return items.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
76
+ }
77
+ static latestOrUndefined(items) {
78
+ return items.length ? LocalDate.latest(items) : undefined;
79
+ }
80
+ static latest(items) {
81
+ (0, assert_1._assert)(items.length, 'LocalDate.latest called on empty array');
82
+ return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
83
+ }
84
+ static range(minIncl, maxExcl, step = 1, stepUnit = 'day') {
85
+ const days = [];
86
+ let current = LocalDate.of(minIncl).startOf(stepUnit);
87
+ const max = LocalDate.of(maxExcl).startOf(stepUnit);
88
+ do {
89
+ days.push(current);
90
+ current = current.add(step, stepUnit);
91
+ } while (current.isBefore(max));
92
+ return days;
93
+ }
94
+ static rangeSeq(minIncl, maxExcl, step = 1, stepUnit = 'day') {
95
+ const min = LocalDate.of(minIncl).startOf(stepUnit);
96
+ const max = LocalDate.of(maxExcl).startOf(stepUnit);
97
+ return seq_1.Sequence.create(min, d => {
98
+ const next = d.add(step, stepUnit);
99
+ return next.isAfter(max) ? types_1.END : next;
100
+ });
101
+ }
102
+ static rangeString(minIncl, maxExcl, step = 1, stepUnit = 'day') {
103
+ return LocalDate.range(minIncl, maxExcl, step, stepUnit).map(ld => ld.toString());
104
+ }
105
+ static rangeIncl(minIncl, maxIncl, step = 1, stepUnit = 'day') {
106
+ return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit);
107
+ }
108
+ static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
109
+ return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
110
+ }
111
+ isSame(d) {
112
+ d = LocalDate.of(d);
113
+ return this.day === d.day && this.month === d.month && this.year === d.year;
114
+ }
115
+ isBefore(d) {
116
+ return this.cmp(d) === -1;
117
+ }
118
+ isSameOrBefore(d) {
119
+ return this.cmp(d) <= 0;
120
+ }
121
+ isAfter(d) {
122
+ return this.cmp(d) === 1;
123
+ }
124
+ isSameOrAfter(d) {
125
+ return this.cmp(d) >= 0;
126
+ }
127
+ /**
128
+ * Returns 1 if this > d
129
+ * returns 0 if they are equal
130
+ * returns -1 if this < d
131
+ */
132
+ cmp(d) {
133
+ d = LocalDate.of(d);
134
+ if (this.year < d.year)
135
+ return -1;
136
+ if (this.year > d.year)
137
+ return 1;
138
+ if (this.month < d.month)
139
+ return -1;
140
+ if (this.month > d.month)
141
+ return 1;
142
+ if (this.day < d.day)
143
+ return -1;
144
+ if (this.day > d.day)
145
+ return 1;
146
+ return 0;
147
+ }
148
+ /**
149
+ * Same as Math.abs( diff )
150
+ */
151
+ absDiff(d, unit) {
152
+ return Math.abs(this.diff(d, unit));
153
+ }
154
+ /**
155
+ * Returns the number of **full** units difference (aka `Math.ceil`).
156
+ *
157
+ * a.diff(b) means "a minus b"
158
+ */
159
+ diff(d, unit) {
160
+ d = LocalDate.of(d);
161
+ if (unit === 'year') {
162
+ return this.year - d.year;
163
+ }
164
+ if (unit === 'month') {
165
+ return (this.year - d.year) * 12 + (this.month - d.month);
166
+ }
167
+ // unit is 'day'
168
+ let days = this.day - d.day;
169
+ if (d.year < this.year) {
170
+ for (let year = d.year; year < this.year; year++) {
171
+ days += LocalDate.getYearLength(year);
172
+ }
173
+ }
174
+ else if (this.year < d.year) {
175
+ for (let year = this.year; year < d.year; year++) {
176
+ days -= LocalDate.getYearLength(year);
177
+ }
178
+ }
179
+ if (d.month < this.month) {
180
+ for (let month = d.month; month < this.month; month++) {
181
+ days += LocalDate.getMonthLength(this.year, month);
182
+ }
183
+ }
184
+ else if (this.month < d.month) {
185
+ for (let month = this.month; month < d.month; month++) {
186
+ days -= LocalDate.getMonthLength(d.year, month);
187
+ }
188
+ }
189
+ return days;
190
+ }
191
+ add(num, unit, mutate = false) {
192
+ let { day, month, year } = this;
193
+ if (unit === 'day') {
194
+ day += num;
195
+ }
196
+ else if (unit === 'month') {
197
+ month += num;
198
+ }
199
+ else if (unit === 'year') {
200
+ year += num;
201
+ }
202
+ // check day overflow
203
+ let monLen = LocalDate.getMonthLength(year, month);
204
+ while (day > monLen) {
205
+ day -= monLen;
206
+ month += 1;
207
+ if (month > 12) {
208
+ year += 1;
209
+ month -= 12;
210
+ }
211
+ monLen = LocalDate.getMonthLength(year, month);
212
+ }
213
+ while (day < 1) {
214
+ day += monLen;
215
+ month -= 1;
216
+ if (month < 1) {
217
+ year -= 1;
218
+ month += 12;
219
+ }
220
+ monLen = LocalDate.getMonthLength(year, month);
221
+ }
222
+ // check month overflow
223
+ while (month > 12) {
224
+ year += 1;
225
+ month -= 12;
226
+ }
227
+ while (month < 1) {
228
+ year -= 1;
229
+ month += 12;
230
+ }
231
+ if (mutate) {
232
+ this.year = year;
233
+ this.month = month;
234
+ this.day = day;
235
+ return this;
236
+ }
237
+ return new LocalDate(year, month, day);
238
+ }
239
+ subtract(num, unit, mutate = false) {
240
+ return this.add(-num, unit, mutate);
241
+ }
242
+ startOf(unit) {
243
+ if (unit === 'day')
244
+ return this;
245
+ if (unit === 'month')
246
+ return LocalDate.create(this.year, this.month, 1);
247
+ // year
248
+ return LocalDate.create(this.year, 1, 1);
249
+ }
250
+ endOf(unit) {
251
+ if (unit === 'day')
252
+ return this;
253
+ if (unit === 'month')
254
+ return LocalDate.create(this.year, this.month, LocalDate.getMonthLength(this.year, this.month));
255
+ // year
256
+ return LocalDate.create(this.year, 12, 31);
257
+ }
258
+ static getYearLength(year) {
259
+ return this.isLeapYear(year) ? 366 : 365;
260
+ }
261
+ static getMonthLength(year, month) {
262
+ if (month === 2)
263
+ return this.isLeapYear(year) ? 29 : 28;
264
+ return m31.has(month) ? 31 : 30;
265
+ }
266
+ static isLeapYear(year) {
267
+ if (year % 4 !== 0)
268
+ return false;
269
+ if (year % 100 !== 0)
270
+ return true;
271
+ return year % 400 === 0;
272
+ }
273
+ clone() {
274
+ return new LocalDate(this.year, this.month, this.day);
275
+ }
276
+ /**
277
+ * Converts LocalDate into instance of Date.
278
+ * Year, month and day will match.
279
+ * Hour, minute, second, ms will be 0.
280
+ * Timezone will match local timezone.
281
+ */
282
+ toDate() {
283
+ return new Date(this.year, this.month - 1, this.day);
284
+ }
285
+ toString() {
286
+ return [
287
+ String(this.year).padStart(4, '0'),
288
+ String(this.month).padStart(2, '0'),
289
+ String(this.day).padStart(2, '0'),
290
+ ].join('-');
291
+ }
292
+ toStringCompact() {
293
+ return [
294
+ String(this.year).padStart(4, '0'),
295
+ String(this.month).padStart(2, '0'),
296
+ String(this.day).padStart(2, '0'),
297
+ ].join('');
298
+ }
299
+ toJSON() {
300
+ return this.toString();
301
+ }
302
+ }
303
+ exports.LocalDate = LocalDate;
304
+ /**
305
+ * Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
306
+ */
307
+ function localDate(d) {
308
+ return d ? LocalDate.of(d) : LocalDate.today();
309
+ }
310
+ exports.localDate = localDate;
@@ -0,0 +1,87 @@
1
+ import { IsoDateTime, UnixTimestamp } from '../types';
2
+ export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
3
+ export declare type LocalTimeConfig = LocalTime | Date | IsoDateTime | UnixTimestamp;
4
+ export interface LocalTimeComponents {
5
+ year: number;
6
+ month: number;
7
+ day: number;
8
+ hour: number;
9
+ minute: number;
10
+ second: number;
11
+ }
12
+ /**
13
+ * @experimental
14
+ */
15
+ export declare class LocalTime {
16
+ private $date;
17
+ private constructor();
18
+ /**
19
+ * Parses input String into LocalDate.
20
+ * Input can already be a LocalDate - it is returned as-is in that case.
21
+ */
22
+ static of(d: LocalTimeConfig): LocalTime;
23
+ /**
24
+ * Returns null if invalid
25
+ */
26
+ static parseOrNull(d: LocalTimeConfig): LocalTime | null;
27
+ static isValid(d: LocalTimeConfig): boolean;
28
+ static unix(ts: UnixTimestamp): LocalTime;
29
+ static now(): LocalTime;
30
+ static fromComponents(c: {
31
+ year: number;
32
+ month: number;
33
+ } & Partial<LocalTimeComponents>): LocalTime;
34
+ get(unit: LocalTimeUnit): number;
35
+ set(unit: LocalTimeUnit, v: number, mutate?: boolean): LocalTime;
36
+ year(): number;
37
+ year(v: number): LocalTime;
38
+ month(): number;
39
+ month(v: number): LocalTime;
40
+ date(): number;
41
+ date(v: number): LocalTime;
42
+ hour(): number;
43
+ hour(v: number): LocalTime;
44
+ minute(): number;
45
+ minute(v: number): LocalTime;
46
+ second(): number;
47
+ second(v: number): LocalTime;
48
+ setComponents(c: Partial<LocalTimeComponents>, mutate?: boolean): LocalTime;
49
+ add(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
50
+ subtract(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
51
+ absDiff(other: LocalTimeConfig, unit: LocalTimeUnit): number;
52
+ diff(other: LocalTimeConfig, unit: LocalTimeUnit): number;
53
+ startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
54
+ static sort(items: LocalTime[], mutate?: boolean, descending?: boolean): LocalTime[];
55
+ static earliestOrUndefined(items: LocalTime[]): LocalTime | undefined;
56
+ static earliest(items: LocalTime[]): LocalTime;
57
+ static latestOrUndefined(items: LocalTime[]): LocalTime | undefined;
58
+ static latest(items: LocalTime[]): LocalTime;
59
+ isSame(d: LocalTimeConfig): boolean;
60
+ isBefore(d: LocalTimeConfig): boolean;
61
+ isSameOrBefore(d: LocalTimeConfig): boolean;
62
+ isAfter(d: LocalTimeConfig): boolean;
63
+ isSameOrAfter(d: LocalTimeConfig): boolean;
64
+ /**
65
+ * Returns 1 if this > d
66
+ * returns 0 if they are equal
67
+ * returns -1 if this < d
68
+ */
69
+ cmp(d: LocalTimeConfig): -1 | 0 | 1;
70
+ components(): LocalTimeComponents;
71
+ getDate(): Date;
72
+ clone(): LocalTime;
73
+ unix(): UnixTimestamp;
74
+ valueOf(): UnixTimestamp;
75
+ toISO8601(): IsoDateTime;
76
+ toPretty(seconds?: boolean): IsoDateTime;
77
+ /**
78
+ * Returns e.g: `19840621_1705`
79
+ */
80
+ toStringCompact(seconds?: boolean): string;
81
+ toString(): string;
82
+ toJSON(): UnixTimestamp;
83
+ }
84
+ /**
85
+ * Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
86
+ */
87
+ export declare function localTime(d?: LocalTimeConfig): LocalTime;