@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.
- package/dist/datetime/localDate.d.ts +80 -0
- package/dist/datetime/localDate.js +310 -0
- package/dist/datetime/localTime.d.ts +87 -0
- package/dist/datetime/localTime.js +339 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +47 -45
- package/dist-esm/datetime/localDate.js +305 -0
- package/dist-esm/datetime/localTime.js +334 -0
- package/dist-esm/index.js +2 -0
- package/dist-esm/vendor/is.js +4 -4
- package/package.json +2 -1
- package/src/__exclude/lazyLocalDate.ts +73 -0
- package/src/datetime/localDate.ts +385 -0
- package/src/datetime/localTime.ts +401 -0
- package/src/index.ts +9 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { _assert } from '../error/assert';
|
|
2
|
+
import { Sequence } from '../seq/seq';
|
|
3
|
+
import { END } from '../types';
|
|
4
|
+
const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
5
|
+
/**
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
export class LocalDate {
|
|
9
|
+
constructor(year, month, day) {
|
|
10
|
+
this.year = year;
|
|
11
|
+
this.month = month;
|
|
12
|
+
this.day = day;
|
|
13
|
+
}
|
|
14
|
+
static create(year, month, day) {
|
|
15
|
+
return new LocalDate(year, month, day);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parses input String into LocalDate.
|
|
19
|
+
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
20
|
+
*/
|
|
21
|
+
static of(d) {
|
|
22
|
+
const t = this.parseOrNull(d);
|
|
23
|
+
if (t === null) {
|
|
24
|
+
throw new Error(`Cannot parse "${d}" into LocalDate`);
|
|
25
|
+
}
|
|
26
|
+
return t;
|
|
27
|
+
}
|
|
28
|
+
static parseCompact(d) {
|
|
29
|
+
const [year, month, day] = [d.slice(0, 4), d.slice(4, 2), d.slice(6, 2)].map(Number);
|
|
30
|
+
if (!day || !month || !year) {
|
|
31
|
+
throw new Error(`Cannot parse "${d}" into LocalDate`);
|
|
32
|
+
}
|
|
33
|
+
return new LocalDate(year, month, day);
|
|
34
|
+
}
|
|
35
|
+
static fromDate(d) {
|
|
36
|
+
return new LocalDate(d.getFullYear(), d.getMonth() + 1, d.getDate());
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns null if invalid.
|
|
40
|
+
*/
|
|
41
|
+
static parseOrNull(d) {
|
|
42
|
+
if (d instanceof LocalDate)
|
|
43
|
+
return d;
|
|
44
|
+
// todo: explore more performant options
|
|
45
|
+
const [year, month, day] = d.slice(0, 10).split('-').map(Number);
|
|
46
|
+
if (!year ||
|
|
47
|
+
!month ||
|
|
48
|
+
month < 1 ||
|
|
49
|
+
month > 12 ||
|
|
50
|
+
!day ||
|
|
51
|
+
day < 1 ||
|
|
52
|
+
day > this.getMonthLength(year, month)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return new LocalDate(year, month, day);
|
|
56
|
+
}
|
|
57
|
+
static isValid(iso) {
|
|
58
|
+
return this.parseOrNull(iso) !== null;
|
|
59
|
+
}
|
|
60
|
+
static today() {
|
|
61
|
+
return this.fromDate(new Date());
|
|
62
|
+
}
|
|
63
|
+
static sort(items, mutate = false, descending = false) {
|
|
64
|
+
const mod = descending ? -1 : 1;
|
|
65
|
+
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
|
|
66
|
+
}
|
|
67
|
+
static earliestOrUndefined(items) {
|
|
68
|
+
return items.length ? LocalDate.earliest(items) : undefined;
|
|
69
|
+
}
|
|
70
|
+
static earliest(items) {
|
|
71
|
+
_assert(items.length, 'LocalDate.earliest called on empty array');
|
|
72
|
+
return items.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
73
|
+
}
|
|
74
|
+
static latestOrUndefined(items) {
|
|
75
|
+
return items.length ? LocalDate.latest(items) : undefined;
|
|
76
|
+
}
|
|
77
|
+
static latest(items) {
|
|
78
|
+
_assert(items.length, 'LocalDate.latest called on empty array');
|
|
79
|
+
return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
80
|
+
}
|
|
81
|
+
static range(minIncl, maxExcl, step = 1, stepUnit = 'day') {
|
|
82
|
+
const days = [];
|
|
83
|
+
let current = LocalDate.of(minIncl).startOf(stepUnit);
|
|
84
|
+
const max = LocalDate.of(maxExcl).startOf(stepUnit);
|
|
85
|
+
do {
|
|
86
|
+
days.push(current);
|
|
87
|
+
current = current.add(step, stepUnit);
|
|
88
|
+
} while (current.isBefore(max));
|
|
89
|
+
return days;
|
|
90
|
+
}
|
|
91
|
+
static rangeSeq(minIncl, maxExcl, step = 1, stepUnit = 'day') {
|
|
92
|
+
const min = LocalDate.of(minIncl).startOf(stepUnit);
|
|
93
|
+
const max = LocalDate.of(maxExcl).startOf(stepUnit);
|
|
94
|
+
return Sequence.create(min, d => {
|
|
95
|
+
const next = d.add(step, stepUnit);
|
|
96
|
+
return next.isAfter(max) ? END : next;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
static rangeString(minIncl, maxExcl, step = 1, stepUnit = 'day') {
|
|
100
|
+
return LocalDate.range(minIncl, maxExcl, step, stepUnit).map(ld => ld.toString());
|
|
101
|
+
}
|
|
102
|
+
static rangeIncl(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
103
|
+
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit);
|
|
104
|
+
}
|
|
105
|
+
static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
|
|
106
|
+
return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
|
|
107
|
+
}
|
|
108
|
+
isSame(d) {
|
|
109
|
+
d = LocalDate.of(d);
|
|
110
|
+
return this.day === d.day && this.month === d.month && this.year === d.year;
|
|
111
|
+
}
|
|
112
|
+
isBefore(d) {
|
|
113
|
+
return this.cmp(d) === -1;
|
|
114
|
+
}
|
|
115
|
+
isSameOrBefore(d) {
|
|
116
|
+
return this.cmp(d) <= 0;
|
|
117
|
+
}
|
|
118
|
+
isAfter(d) {
|
|
119
|
+
return this.cmp(d) === 1;
|
|
120
|
+
}
|
|
121
|
+
isSameOrAfter(d) {
|
|
122
|
+
return this.cmp(d) >= 0;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns 1 if this > d
|
|
126
|
+
* returns 0 if they are equal
|
|
127
|
+
* returns -1 if this < d
|
|
128
|
+
*/
|
|
129
|
+
cmp(d) {
|
|
130
|
+
d = LocalDate.of(d);
|
|
131
|
+
if (this.year < d.year)
|
|
132
|
+
return -1;
|
|
133
|
+
if (this.year > d.year)
|
|
134
|
+
return 1;
|
|
135
|
+
if (this.month < d.month)
|
|
136
|
+
return -1;
|
|
137
|
+
if (this.month > d.month)
|
|
138
|
+
return 1;
|
|
139
|
+
if (this.day < d.day)
|
|
140
|
+
return -1;
|
|
141
|
+
if (this.day > d.day)
|
|
142
|
+
return 1;
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Same as Math.abs( diff )
|
|
147
|
+
*/
|
|
148
|
+
absDiff(d, unit) {
|
|
149
|
+
return Math.abs(this.diff(d, unit));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Returns the number of **full** units difference (aka `Math.ceil`).
|
|
153
|
+
*
|
|
154
|
+
* a.diff(b) means "a minus b"
|
|
155
|
+
*/
|
|
156
|
+
diff(d, unit) {
|
|
157
|
+
d = LocalDate.of(d);
|
|
158
|
+
if (unit === 'year') {
|
|
159
|
+
return this.year - d.year;
|
|
160
|
+
}
|
|
161
|
+
if (unit === 'month') {
|
|
162
|
+
return (this.year - d.year) * 12 + (this.month - d.month);
|
|
163
|
+
}
|
|
164
|
+
// unit is 'day'
|
|
165
|
+
let days = this.day - d.day;
|
|
166
|
+
if (d.year < this.year) {
|
|
167
|
+
for (let year = d.year; year < this.year; year++) {
|
|
168
|
+
days += LocalDate.getYearLength(year);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (this.year < d.year) {
|
|
172
|
+
for (let year = this.year; year < d.year; year++) {
|
|
173
|
+
days -= LocalDate.getYearLength(year);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (d.month < this.month) {
|
|
177
|
+
for (let month = d.month; month < this.month; month++) {
|
|
178
|
+
days += LocalDate.getMonthLength(this.year, month);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else if (this.month < d.month) {
|
|
182
|
+
for (let month = this.month; month < d.month; month++) {
|
|
183
|
+
days -= LocalDate.getMonthLength(d.year, month);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return days;
|
|
187
|
+
}
|
|
188
|
+
add(num, unit, mutate = false) {
|
|
189
|
+
let { day, month, year } = this;
|
|
190
|
+
if (unit === 'day') {
|
|
191
|
+
day += num;
|
|
192
|
+
}
|
|
193
|
+
else if (unit === 'month') {
|
|
194
|
+
month += num;
|
|
195
|
+
}
|
|
196
|
+
else if (unit === 'year') {
|
|
197
|
+
year += num;
|
|
198
|
+
}
|
|
199
|
+
// check day overflow
|
|
200
|
+
let monLen = LocalDate.getMonthLength(year, month);
|
|
201
|
+
while (day > monLen) {
|
|
202
|
+
day -= monLen;
|
|
203
|
+
month += 1;
|
|
204
|
+
if (month > 12) {
|
|
205
|
+
year += 1;
|
|
206
|
+
month -= 12;
|
|
207
|
+
}
|
|
208
|
+
monLen = LocalDate.getMonthLength(year, month);
|
|
209
|
+
}
|
|
210
|
+
while (day < 1) {
|
|
211
|
+
day += monLen;
|
|
212
|
+
month -= 1;
|
|
213
|
+
if (month < 1) {
|
|
214
|
+
year -= 1;
|
|
215
|
+
month += 12;
|
|
216
|
+
}
|
|
217
|
+
monLen = LocalDate.getMonthLength(year, month);
|
|
218
|
+
}
|
|
219
|
+
// check month overflow
|
|
220
|
+
while (month > 12) {
|
|
221
|
+
year += 1;
|
|
222
|
+
month -= 12;
|
|
223
|
+
}
|
|
224
|
+
while (month < 1) {
|
|
225
|
+
year -= 1;
|
|
226
|
+
month += 12;
|
|
227
|
+
}
|
|
228
|
+
if (mutate) {
|
|
229
|
+
this.year = year;
|
|
230
|
+
this.month = month;
|
|
231
|
+
this.day = day;
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
return new LocalDate(year, month, day);
|
|
235
|
+
}
|
|
236
|
+
subtract(num, unit, mutate = false) {
|
|
237
|
+
return this.add(-num, unit, mutate);
|
|
238
|
+
}
|
|
239
|
+
startOf(unit) {
|
|
240
|
+
if (unit === 'day')
|
|
241
|
+
return this;
|
|
242
|
+
if (unit === 'month')
|
|
243
|
+
return LocalDate.create(this.year, this.month, 1);
|
|
244
|
+
// year
|
|
245
|
+
return LocalDate.create(this.year, 1, 1);
|
|
246
|
+
}
|
|
247
|
+
endOf(unit) {
|
|
248
|
+
if (unit === 'day')
|
|
249
|
+
return this;
|
|
250
|
+
if (unit === 'month')
|
|
251
|
+
return LocalDate.create(this.year, this.month, LocalDate.getMonthLength(this.year, this.month));
|
|
252
|
+
// year
|
|
253
|
+
return LocalDate.create(this.year, 12, 31);
|
|
254
|
+
}
|
|
255
|
+
static getYearLength(year) {
|
|
256
|
+
return this.isLeapYear(year) ? 366 : 365;
|
|
257
|
+
}
|
|
258
|
+
static getMonthLength(year, month) {
|
|
259
|
+
if (month === 2)
|
|
260
|
+
return this.isLeapYear(year) ? 29 : 28;
|
|
261
|
+
return m31.has(month) ? 31 : 30;
|
|
262
|
+
}
|
|
263
|
+
static isLeapYear(year) {
|
|
264
|
+
if (year % 4 !== 0)
|
|
265
|
+
return false;
|
|
266
|
+
if (year % 100 !== 0)
|
|
267
|
+
return true;
|
|
268
|
+
return year % 400 === 0;
|
|
269
|
+
}
|
|
270
|
+
clone() {
|
|
271
|
+
return new LocalDate(this.year, this.month, this.day);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Converts LocalDate into instance of Date.
|
|
275
|
+
* Year, month and day will match.
|
|
276
|
+
* Hour, minute, second, ms will be 0.
|
|
277
|
+
* Timezone will match local timezone.
|
|
278
|
+
*/
|
|
279
|
+
toDate() {
|
|
280
|
+
return new Date(this.year, this.month - 1, this.day);
|
|
281
|
+
}
|
|
282
|
+
toString() {
|
|
283
|
+
return [
|
|
284
|
+
String(this.year).padStart(4, '0'),
|
|
285
|
+
String(this.month).padStart(2, '0'),
|
|
286
|
+
String(this.day).padStart(2, '0'),
|
|
287
|
+
].join('-');
|
|
288
|
+
}
|
|
289
|
+
toStringCompact() {
|
|
290
|
+
return [
|
|
291
|
+
String(this.year).padStart(4, '0'),
|
|
292
|
+
String(this.month).padStart(2, '0'),
|
|
293
|
+
String(this.day).padStart(2, '0'),
|
|
294
|
+
].join('');
|
|
295
|
+
}
|
|
296
|
+
toJSON() {
|
|
297
|
+
return this.toString();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
302
|
+
*/
|
|
303
|
+
export function localDate(d) {
|
|
304
|
+
return d ? LocalDate.of(d) : LocalDate.today();
|
|
305
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { _assert } from '../error/assert';
|
|
2
|
+
/* eslint-disable no-dupe-class-members */
|
|
3
|
+
// Design choices:
|
|
4
|
+
// No milliseconds
|
|
5
|
+
// No timezone support, ISO8601 is parsed as LocalDateTime, discarding Timezone information
|
|
6
|
+
// Formats as unix timestamp, ISO8601 or "pretty string"
|
|
7
|
+
// toString and .toJSON formats as unix timestamp
|
|
8
|
+
// No "unixMillis", just pure unixtimestamp
|
|
9
|
+
// .valueOf returns unix timestamp (no millis)
|
|
10
|
+
// Prevents dayjs(undefined) being dayjs.now()
|
|
11
|
+
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
12
|
+
/**
|
|
13
|
+
* @experimental
|
|
14
|
+
*/
|
|
15
|
+
export class LocalTime {
|
|
16
|
+
constructor($date) {
|
|
17
|
+
this.$date = $date;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parses input String into LocalDate.
|
|
21
|
+
* Input can already be a LocalDate - it is returned as-is in that case.
|
|
22
|
+
*/
|
|
23
|
+
static of(d) {
|
|
24
|
+
const t = this.parseOrNull(d);
|
|
25
|
+
if (t === null) {
|
|
26
|
+
throw new TypeError(`Cannot parse "${d}" into LocalTime`);
|
|
27
|
+
}
|
|
28
|
+
return t;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns null if invalid
|
|
32
|
+
*/
|
|
33
|
+
static parseOrNull(d) {
|
|
34
|
+
if (d instanceof LocalTime)
|
|
35
|
+
return d;
|
|
36
|
+
let date;
|
|
37
|
+
if (d instanceof Date) {
|
|
38
|
+
date = d;
|
|
39
|
+
}
|
|
40
|
+
else if (typeof d === 'number') {
|
|
41
|
+
date = new Date(d * 1000);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
date = new Date(d);
|
|
45
|
+
}
|
|
46
|
+
// validation
|
|
47
|
+
if (isNaN(date.getDate())) {
|
|
48
|
+
// throw new TypeError(`Cannot parse "${d}" into LocalTime`)
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return new LocalTime(date);
|
|
52
|
+
}
|
|
53
|
+
static isValid(d) {
|
|
54
|
+
return this.parseOrNull(d) !== null;
|
|
55
|
+
}
|
|
56
|
+
static unix(ts) {
|
|
57
|
+
return new LocalTime(new Date(ts * 1000));
|
|
58
|
+
}
|
|
59
|
+
static now() {
|
|
60
|
+
return this.of(new Date());
|
|
61
|
+
}
|
|
62
|
+
static fromComponents(c) {
|
|
63
|
+
return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second));
|
|
64
|
+
}
|
|
65
|
+
get(unit) {
|
|
66
|
+
if (unit === 'year') {
|
|
67
|
+
return this.$date.getFullYear();
|
|
68
|
+
}
|
|
69
|
+
if (unit === 'month') {
|
|
70
|
+
return this.$date.getMonth() + 1;
|
|
71
|
+
}
|
|
72
|
+
if (unit === 'day') {
|
|
73
|
+
return this.$date.getDate();
|
|
74
|
+
}
|
|
75
|
+
if (unit === 'hour') {
|
|
76
|
+
return this.$date.getHours();
|
|
77
|
+
}
|
|
78
|
+
if (unit === 'minute') {
|
|
79
|
+
return this.$date.getMinutes();
|
|
80
|
+
}
|
|
81
|
+
// second
|
|
82
|
+
return this.$date.getSeconds();
|
|
83
|
+
}
|
|
84
|
+
set(unit, v, mutate = false) {
|
|
85
|
+
const t = mutate ? this : this.clone();
|
|
86
|
+
if (unit === 'year') {
|
|
87
|
+
t.$date.setFullYear(v);
|
|
88
|
+
}
|
|
89
|
+
else if (unit === 'month') {
|
|
90
|
+
t.$date.setMonth(v - 1);
|
|
91
|
+
}
|
|
92
|
+
else if (unit === 'day') {
|
|
93
|
+
t.$date.setDate(v);
|
|
94
|
+
}
|
|
95
|
+
else if (unit === 'hour') {
|
|
96
|
+
t.$date.setHours(v);
|
|
97
|
+
}
|
|
98
|
+
else if (unit === 'minute') {
|
|
99
|
+
t.$date.setMinutes(v);
|
|
100
|
+
}
|
|
101
|
+
else if (unit === 'second') {
|
|
102
|
+
t.$date.setSeconds(v);
|
|
103
|
+
}
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
year(v) {
|
|
107
|
+
return v === undefined ? this.$date.getFullYear() : this.set('year', v);
|
|
108
|
+
}
|
|
109
|
+
month(v) {
|
|
110
|
+
return v === undefined ? this.$date.getMonth() + 1 : this.set('month', v);
|
|
111
|
+
}
|
|
112
|
+
date(v) {
|
|
113
|
+
return v === undefined ? this.$date.getDate() : this.set('day', v);
|
|
114
|
+
}
|
|
115
|
+
hour(v) {
|
|
116
|
+
return v === undefined ? this.$date.getHours() : this.set('hour', v);
|
|
117
|
+
}
|
|
118
|
+
minute(v) {
|
|
119
|
+
return v === undefined ? this.$date.getMinutes() : this.set('minute', v);
|
|
120
|
+
}
|
|
121
|
+
second(v) {
|
|
122
|
+
return v === undefined ? this.$date.getSeconds() : this.set('second', v);
|
|
123
|
+
}
|
|
124
|
+
setComponents(c, mutate = false) {
|
|
125
|
+
const d = mutate ? this.$date : new Date(this.$date);
|
|
126
|
+
if (c.year) {
|
|
127
|
+
d.setFullYear(c.year);
|
|
128
|
+
}
|
|
129
|
+
if (c.month) {
|
|
130
|
+
d.setMonth(c.month - 1);
|
|
131
|
+
}
|
|
132
|
+
if (c.day) {
|
|
133
|
+
d.setDate(c.day);
|
|
134
|
+
}
|
|
135
|
+
if (c.hour !== undefined) {
|
|
136
|
+
d.setHours(c.hour);
|
|
137
|
+
}
|
|
138
|
+
if (c.minute !== undefined) {
|
|
139
|
+
d.setMinutes(c.minute);
|
|
140
|
+
}
|
|
141
|
+
if (c.second !== undefined) {
|
|
142
|
+
d.setSeconds(c.second);
|
|
143
|
+
}
|
|
144
|
+
return mutate ? this : new LocalTime(d);
|
|
145
|
+
}
|
|
146
|
+
add(num, unit, mutate = false) {
|
|
147
|
+
return this.set(unit, this.get(unit) + num, mutate);
|
|
148
|
+
}
|
|
149
|
+
subtract(num, unit, mutate = false) {
|
|
150
|
+
return this.add(-num, unit, mutate);
|
|
151
|
+
}
|
|
152
|
+
absDiff(other, unit) {
|
|
153
|
+
return Math.abs(this.diff(other, unit));
|
|
154
|
+
}
|
|
155
|
+
diff(other, unit) {
|
|
156
|
+
const date2 = LocalTime.of(other).$date;
|
|
157
|
+
if (unit === 'year') {
|
|
158
|
+
return this.$date.getFullYear() - date2.getFullYear();
|
|
159
|
+
}
|
|
160
|
+
if (unit === 'month') {
|
|
161
|
+
return ((this.$date.getFullYear() - date2.getFullYear()) * 12 +
|
|
162
|
+
this.$date.getMonth() -
|
|
163
|
+
date2.getMonth());
|
|
164
|
+
}
|
|
165
|
+
const secDiff = (this.$date.valueOf() - date2.valueOf()) / 1000;
|
|
166
|
+
let r;
|
|
167
|
+
if (unit === 'day') {
|
|
168
|
+
r = secDiff / (24 * 60 * 60);
|
|
169
|
+
}
|
|
170
|
+
else if (unit === 'hour') {
|
|
171
|
+
r = secDiff / (60 * 60);
|
|
172
|
+
}
|
|
173
|
+
else if (unit === 'minute') {
|
|
174
|
+
r = secDiff / 60;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// unit === 'second'
|
|
178
|
+
r = secDiff;
|
|
179
|
+
}
|
|
180
|
+
r = r < 0 ? -Math.floor(-r) : Math.floor(r);
|
|
181
|
+
if (Object.is(r, -0))
|
|
182
|
+
return 0;
|
|
183
|
+
return r;
|
|
184
|
+
}
|
|
185
|
+
startOf(unit, mutate = false) {
|
|
186
|
+
if (unit === 'second')
|
|
187
|
+
return this;
|
|
188
|
+
if (mutate) {
|
|
189
|
+
const d = this.$date;
|
|
190
|
+
d.setSeconds(0);
|
|
191
|
+
if (unit === 'minute')
|
|
192
|
+
return this;
|
|
193
|
+
d.setMinutes(0);
|
|
194
|
+
if (unit === 'hour')
|
|
195
|
+
return this;
|
|
196
|
+
d.setHours(0);
|
|
197
|
+
if (unit === 'day')
|
|
198
|
+
return this;
|
|
199
|
+
d.setDate(0);
|
|
200
|
+
if (unit === 'month')
|
|
201
|
+
return this;
|
|
202
|
+
d.setMonth(0);
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
const c = this.components();
|
|
206
|
+
c.second = 0;
|
|
207
|
+
if (unit === 'year') {
|
|
208
|
+
c.month = c.day = 1;
|
|
209
|
+
c.hour = c.minute = 0;
|
|
210
|
+
}
|
|
211
|
+
else if (unit === 'month') {
|
|
212
|
+
c.day = 1;
|
|
213
|
+
c.hour = c.minute = 0;
|
|
214
|
+
}
|
|
215
|
+
else if (unit === 'day') {
|
|
216
|
+
c.hour = c.minute = 0;
|
|
217
|
+
}
|
|
218
|
+
else if (unit === 'hour') {
|
|
219
|
+
c.minute = 0;
|
|
220
|
+
}
|
|
221
|
+
return LocalTime.fromComponents(c);
|
|
222
|
+
}
|
|
223
|
+
static sort(items, mutate = false, descending = false) {
|
|
224
|
+
const mod = descending ? -1 : 1;
|
|
225
|
+
return (mutate ? items : [...items]).sort((a, b) => {
|
|
226
|
+
const v1 = a.$date.valueOf();
|
|
227
|
+
const v2 = b.$date.valueOf();
|
|
228
|
+
if (v1 === v2)
|
|
229
|
+
return 0;
|
|
230
|
+
return (v1 < v2 ? -1 : 1) * mod;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
static earliestOrUndefined(items) {
|
|
234
|
+
return items.length ? LocalTime.earliest(items) : undefined;
|
|
235
|
+
}
|
|
236
|
+
static earliest(items) {
|
|
237
|
+
_assert(items.length, 'LocalTime.earliest called on empty array');
|
|
238
|
+
return items.reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
|
|
239
|
+
}
|
|
240
|
+
static latestOrUndefined(items) {
|
|
241
|
+
return items.length ? LocalTime.latest(items) : undefined;
|
|
242
|
+
}
|
|
243
|
+
static latest(items) {
|
|
244
|
+
_assert(items.length, 'LocalTime.latest called on empty array');
|
|
245
|
+
return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
|
|
246
|
+
}
|
|
247
|
+
isSame(d) {
|
|
248
|
+
return this.cmp(d) === 0;
|
|
249
|
+
}
|
|
250
|
+
isBefore(d) {
|
|
251
|
+
return this.cmp(d) === -1;
|
|
252
|
+
}
|
|
253
|
+
isSameOrBefore(d) {
|
|
254
|
+
return this.cmp(d) <= 0;
|
|
255
|
+
}
|
|
256
|
+
isAfter(d) {
|
|
257
|
+
return this.cmp(d) === 1;
|
|
258
|
+
}
|
|
259
|
+
isSameOrAfter(d) {
|
|
260
|
+
return this.cmp(d) >= 0;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Returns 1 if this > d
|
|
264
|
+
* returns 0 if they are equal
|
|
265
|
+
* returns -1 if this < d
|
|
266
|
+
*/
|
|
267
|
+
cmp(d) {
|
|
268
|
+
const t1 = this.$date.valueOf();
|
|
269
|
+
const t2 = LocalTime.of(d).$date.valueOf();
|
|
270
|
+
if (t1 === t2)
|
|
271
|
+
return 0;
|
|
272
|
+
return t1 < t2 ? -1 : 1;
|
|
273
|
+
}
|
|
274
|
+
// todo: endOf
|
|
275
|
+
components() {
|
|
276
|
+
return {
|
|
277
|
+
year: this.$date.getFullYear(),
|
|
278
|
+
month: this.$date.getMonth() + 1,
|
|
279
|
+
day: this.$date.getDate(),
|
|
280
|
+
hour: this.$date.getHours(),
|
|
281
|
+
minute: this.$date.getMinutes(),
|
|
282
|
+
second: this.$date.getSeconds(),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
getDate() {
|
|
286
|
+
return this.$date;
|
|
287
|
+
}
|
|
288
|
+
clone() {
|
|
289
|
+
return new LocalTime(new Date(this.$date));
|
|
290
|
+
}
|
|
291
|
+
unix() {
|
|
292
|
+
return Math.floor(this.$date.valueOf() / 1000);
|
|
293
|
+
}
|
|
294
|
+
valueOf() {
|
|
295
|
+
return Math.floor(this.$date.valueOf() / 1000);
|
|
296
|
+
}
|
|
297
|
+
toISO8601() {
|
|
298
|
+
return this.$date.toISOString().slice(0, 19);
|
|
299
|
+
}
|
|
300
|
+
toPretty(seconds = true) {
|
|
301
|
+
return this.$date
|
|
302
|
+
.toISOString()
|
|
303
|
+
.slice(0, seconds ? 19 : 16)
|
|
304
|
+
.split('T')
|
|
305
|
+
.join(' ');
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns e.g: `19840621_1705`
|
|
309
|
+
*/
|
|
310
|
+
toStringCompact(seconds = false) {
|
|
311
|
+
return [
|
|
312
|
+
String(this.$date.getFullYear()).padStart(4, '0'),
|
|
313
|
+
String(this.$date.getMonth() + 1).padStart(2, '0'),
|
|
314
|
+
String(this.$date.getDate()).padStart(2, '0'),
|
|
315
|
+
'_',
|
|
316
|
+
String(this.$date.getHours()).padStart(2, '0'),
|
|
317
|
+
String(this.$date.getMinutes()).padStart(2, '0'),
|
|
318
|
+
seconds ? String(this.$date.getSeconds()).padStart(2, '0') : '',
|
|
319
|
+
].join('');
|
|
320
|
+
}
|
|
321
|
+
toString() {
|
|
322
|
+
return String(this.unix());
|
|
323
|
+
}
|
|
324
|
+
toJSON() {
|
|
325
|
+
return this.unix();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Shortcut wrapper around `LocalDate.parse` / `LocalDate.today`
|
|
330
|
+
*/
|
|
331
|
+
export function localTime(d) {
|
|
332
|
+
return d ? LocalTime.of(d) : LocalTime.now();
|
|
333
|
+
}
|
|
334
|
+
// todo: range
|
package/dist-esm/index.js
CHANGED
|
@@ -57,4 +57,6 @@ import { PQueue } from './promise/pQueue';
|
|
|
57
57
|
export * from './seq/seq';
|
|
58
58
|
export * from './math/stack.util';
|
|
59
59
|
export * from './string/leven';
|
|
60
|
+
export * from './datetime/localDate';
|
|
61
|
+
export * from './datetime/localTime';
|
|
60
62
|
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pRetryFn, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
package/dist-esm/vendor/is.js
CHANGED
|
@@ -138,15 +138,15 @@ is.array = (value, assertion) => {
|
|
|
138
138
|
}
|
|
139
139
|
return value.every(assertion);
|
|
140
140
|
};
|
|
141
|
-
is.buffer = (value) => { var _a, _b, _c
|
|
141
|
+
is.buffer = (value) => { var _a, _b, _c; return (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.isBuffer) === null || _b === void 0 ? void 0 : _b.call(_a, value)) !== null && _c !== void 0 ? _c : false; };
|
|
142
142
|
is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
|
|
143
143
|
is.object = (value) => !is.null_(value) && (typeof value === 'object' || is.function_(value));
|
|
144
|
-
is.iterable = (value) =>
|
|
145
|
-
is.asyncIterable = (value) =>
|
|
144
|
+
is.iterable = (value) => is.function_(value === null || value === void 0 ? void 0 : value[Symbol.iterator]);
|
|
145
|
+
is.asyncIterable = (value) => is.function_(value === null || value === void 0 ? void 0 : value[Symbol.asyncIterator]);
|
|
146
146
|
is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
|
|
147
147
|
is.asyncGenerator = (value) => is.asyncIterable(value) && is.function_(value.next) && is.function_(value.throw);
|
|
148
148
|
is.nativePromise = (value) => isObjectOfType('Promise')(value);
|
|
149
|
-
const hasPromiseAPI = (value) =>
|
|
149
|
+
const hasPromiseAPI = (value) => is.function_(value === null || value === void 0 ? void 0 : value.then) && is.function_(value === null || value === void 0 ? void 0 : value.catch);
|
|
150
150
|
is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
|
|
151
151
|
is.generatorFunction = isObjectOfType('GeneratorFunction');
|
|
152
152
|
is.asyncGeneratorFunction = (value) => getObjectType(value) === 'AsyncGeneratorFunction';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.91.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"build-prod": "build-prod-esm-cjs",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"@naturalcycles/bench-lib": "^1.5.0",
|
|
15
15
|
"@naturalcycles/dev-lib": "^12.0.0",
|
|
16
16
|
"@naturalcycles/nodejs-lib": "^12.33.4",
|
|
17
|
+
"@naturalcycles/time-lib": "^3.5.1",
|
|
17
18
|
"@types/node": "^17.0.4",
|
|
18
19
|
"jest": "^27.0.1",
|
|
19
20
|
"patch-package": "^6.2.1",
|