@naturalcycles/js-lib 14.92.0 → 14.95.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/dateInterval.d.ts +39 -0
- package/dist/datetime/dateInterval.js +80 -0
- package/dist/datetime/localDate.d.ts +2 -0
- package/dist/datetime/localDate.js +9 -0
- package/dist/datetime/localTime.d.ts +2 -1
- package/dist/datetime/localTime.js +12 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -0
- package/dist-esm/datetime/dateInterval.js +76 -0
- package/dist-esm/datetime/localDate.js +9 -0
- package/dist-esm/datetime/localTime.js +12 -0
- package/dist-esm/index.js +1 -0
- package/package.json +1 -1
- package/src/datetime/dateInterval.ts +92 -0
- package/src/datetime/localDate.ts +9 -0
- package/src/datetime/localTime.ts +12 -1
- package/src/index.ts +6 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LocalDate, LocalDateConfig } from './localDate';
|
|
2
|
+
export declare type DateIntervalConfig = DateInterval | string;
|
|
3
|
+
export declare type DateIntervalString = string;
|
|
4
|
+
/**
|
|
5
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
6
|
+
*
|
|
7
|
+
* @experimental
|
|
8
|
+
*/
|
|
9
|
+
export declare class DateInterval {
|
|
10
|
+
start: LocalDate;
|
|
11
|
+
end: LocalDate;
|
|
12
|
+
private constructor();
|
|
13
|
+
static of(start: LocalDateConfig, end: LocalDateConfig): DateInterval;
|
|
14
|
+
/**
|
|
15
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
16
|
+
*/
|
|
17
|
+
static parse(d: DateIntervalConfig): DateInterval;
|
|
18
|
+
isSame(d: DateIntervalConfig): boolean;
|
|
19
|
+
isBefore(d: DateIntervalConfig): boolean;
|
|
20
|
+
isSameOrBefore(d: DateIntervalConfig): boolean;
|
|
21
|
+
isAfter(d: DateIntervalConfig): boolean;
|
|
22
|
+
isSameOrAfter(d: DateIntervalConfig): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
25
|
+
*/
|
|
26
|
+
includes(d: LocalDateConfig): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* DateIntervals compare by start date.
|
|
29
|
+
* If it's the same - then by end date.
|
|
30
|
+
*/
|
|
31
|
+
cmp(d: DateIntervalConfig): -1 | 0 | 1;
|
|
32
|
+
/**
|
|
33
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
34
|
+
* Ranges are INCLUSIVE.
|
|
35
|
+
*/
|
|
36
|
+
getDays(): LocalDate[];
|
|
37
|
+
toString(): DateIntervalString;
|
|
38
|
+
toJSON(): DateIntervalString;
|
|
39
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateInterval = void 0;
|
|
4
|
+
const localDate_1 = require("./localDate");
|
|
5
|
+
/**
|
|
6
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
7
|
+
*
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
class DateInterval {
|
|
11
|
+
constructor(start, end) {
|
|
12
|
+
this.start = start;
|
|
13
|
+
this.end = end;
|
|
14
|
+
}
|
|
15
|
+
static of(start, end) {
|
|
16
|
+
return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
20
|
+
*/
|
|
21
|
+
static parse(d) {
|
|
22
|
+
if (d instanceof DateInterval)
|
|
23
|
+
return d;
|
|
24
|
+
const [start, end] = d.split('/');
|
|
25
|
+
if (!end || !start) {
|
|
26
|
+
throw new Error(`Cannot parse "${d}" into DateInterval`);
|
|
27
|
+
}
|
|
28
|
+
return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
|
|
29
|
+
}
|
|
30
|
+
isSame(d) {
|
|
31
|
+
return this.cmp(d) === 0;
|
|
32
|
+
}
|
|
33
|
+
isBefore(d) {
|
|
34
|
+
return this.cmp(d) === -1;
|
|
35
|
+
}
|
|
36
|
+
isSameOrBefore(d) {
|
|
37
|
+
return this.cmp(d) <= 0;
|
|
38
|
+
}
|
|
39
|
+
isAfter(d) {
|
|
40
|
+
return this.cmp(d) === 1;
|
|
41
|
+
}
|
|
42
|
+
isSameOrAfter(d) {
|
|
43
|
+
return this.cmp(d) >= 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
47
|
+
*/
|
|
48
|
+
includes(d) {
|
|
49
|
+
d = localDate_1.LocalDate.of(d);
|
|
50
|
+
return d.isSameOrAfter(this.start) && d.isSameOrBefore(this.end);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* DateIntervals compare by start date.
|
|
54
|
+
* If it's the same - then by end date.
|
|
55
|
+
*/
|
|
56
|
+
cmp(d) {
|
|
57
|
+
d = DateInterval.parse(d);
|
|
58
|
+
return this.start.cmp(d.start) || this.end.cmp(d.end);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
62
|
+
* Ranges are INCLUSIVE.
|
|
63
|
+
*/
|
|
64
|
+
getDays() {
|
|
65
|
+
const days = [];
|
|
66
|
+
let current = this.start;
|
|
67
|
+
do {
|
|
68
|
+
days.push(current);
|
|
69
|
+
current = current.add(1, 'day');
|
|
70
|
+
} while (current.isSameOrBefore(this.end));
|
|
71
|
+
return days;
|
|
72
|
+
}
|
|
73
|
+
toString() {
|
|
74
|
+
return [this.start, this.end].join('/');
|
|
75
|
+
}
|
|
76
|
+
toJSON() {
|
|
77
|
+
return this.toString();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.DateInterval = DateInterval;
|
|
@@ -2,6 +2,7 @@ import { Sequence } from '../seq/seq';
|
|
|
2
2
|
import { IsoDate, UnixTimestamp } from '../types';
|
|
3
3
|
import { LocalTime } from './localTime';
|
|
4
4
|
export declare type LocalDateUnit = 'year' | 'month' | 'day';
|
|
5
|
+
export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
|
|
5
6
|
export declare type LocalDateConfig = LocalDate | string;
|
|
6
7
|
/**
|
|
7
8
|
* @experimental
|
|
@@ -42,6 +43,7 @@ export declare class LocalDate {
|
|
|
42
43
|
isSameOrBefore(d: LocalDateConfig): boolean;
|
|
43
44
|
isAfter(d: LocalDateConfig): boolean;
|
|
44
45
|
isSameOrAfter(d: LocalDateConfig): boolean;
|
|
46
|
+
isBetween(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness): boolean;
|
|
45
47
|
/**
|
|
46
48
|
* Returns 1 if this > d
|
|
47
49
|
* returns 0 if they are equal
|
|
@@ -131,6 +131,15 @@ class LocalDate {
|
|
|
131
131
|
isSameOrAfter(d) {
|
|
132
132
|
return this.cmp(d) >= 0;
|
|
133
133
|
}
|
|
134
|
+
isBetween(min, max, incl = '[)') {
|
|
135
|
+
let r = this.cmp(min);
|
|
136
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
137
|
+
return false;
|
|
138
|
+
r = this.cmp(max);
|
|
139
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
140
|
+
return false;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
134
143
|
/**
|
|
135
144
|
* Returns 1 if this > d
|
|
136
145
|
* returns 0 if they are equal
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IsoDate, IsoDateTime, UnixTimestamp } from '../types';
|
|
2
|
-
import { LocalDate } from './localDate';
|
|
2
|
+
import { Inclusiveness, LocalDate } from './localDate';
|
|
3
3
|
export declare type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
|
|
4
4
|
export declare type LocalTimeConfig = LocalTime | Date | IsoDateTime | UnixTimestamp;
|
|
5
5
|
export interface LocalTimeComponents {
|
|
@@ -64,6 +64,7 @@ export declare class LocalTime {
|
|
|
64
64
|
isSameOrBefore(d: LocalTimeConfig): boolean;
|
|
65
65
|
isAfter(d: LocalTimeConfig): boolean;
|
|
66
66
|
isSameOrAfter(d: LocalTimeConfig): boolean;
|
|
67
|
+
isBetween(min: LocalTimeConfig, max: LocalTimeConfig, incl?: Inclusiveness): boolean;
|
|
67
68
|
/**
|
|
68
69
|
* Returns 1 if this > d
|
|
69
70
|
* returns 0 if they are equal
|
|
@@ -14,6 +14,9 @@ const localDate_1 = require("./localDate");
|
|
|
14
14
|
// .valueOf returns unix timestamp (no millis)
|
|
15
15
|
// Prevents dayjs(undefined) being dayjs.now()
|
|
16
16
|
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
17
|
+
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
18
|
+
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
19
|
+
// you only need "whole dates", no time, no timezone worry
|
|
17
20
|
/**
|
|
18
21
|
* @experimental
|
|
19
22
|
*/
|
|
@@ -260,6 +263,15 @@ class LocalTime {
|
|
|
260
263
|
isSameOrAfter(d) {
|
|
261
264
|
return this.cmp(d) >= 0;
|
|
262
265
|
}
|
|
266
|
+
isBetween(min, max, incl = '[)') {
|
|
267
|
+
let r = this.cmp(min);
|
|
268
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
269
|
+
return false;
|
|
270
|
+
r = this.cmp(max);
|
|
271
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
272
|
+
return false;
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
263
275
|
/**
|
|
264
276
|
* Returns 1 if this > d
|
|
265
277
|
* returns 0 if they are equal
|
package/dist/index.d.ts
CHANGED
|
@@ -63,7 +63,9 @@ export * from './math/stack.util';
|
|
|
63
63
|
export * from './string/leven';
|
|
64
64
|
export * from './datetime/localDate';
|
|
65
65
|
export * from './datetime/localTime';
|
|
66
|
-
|
|
66
|
+
export * from './datetime/dateInterval';
|
|
67
|
+
import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
|
|
67
68
|
import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
|
|
68
|
-
|
|
69
|
+
import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
|
|
70
|
+
export type { DateIntervalConfig, DateIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
69
71
|
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/index.js
CHANGED
|
@@ -95,3 +95,4 @@ tslib_1.__exportStar(require("./math/stack.util"), exports);
|
|
|
95
95
|
tslib_1.__exportStar(require("./string/leven"), exports);
|
|
96
96
|
tslib_1.__exportStar(require("./datetime/localDate"), exports);
|
|
97
97
|
tslib_1.__exportStar(require("./datetime/localTime"), exports);
|
|
98
|
+
tslib_1.__exportStar(require("./datetime/dateInterval"), exports);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { LocalDate } from './localDate';
|
|
2
|
+
/**
|
|
3
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
4
|
+
*
|
|
5
|
+
* @experimental
|
|
6
|
+
*/
|
|
7
|
+
export class DateInterval {
|
|
8
|
+
constructor(start, end) {
|
|
9
|
+
this.start = start;
|
|
10
|
+
this.end = end;
|
|
11
|
+
}
|
|
12
|
+
static of(start, end) {
|
|
13
|
+
return new DateInterval(LocalDate.of(start), LocalDate.of(end));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
17
|
+
*/
|
|
18
|
+
static parse(d) {
|
|
19
|
+
if (d instanceof DateInterval)
|
|
20
|
+
return d;
|
|
21
|
+
const [start, end] = d.split('/');
|
|
22
|
+
if (!end || !start) {
|
|
23
|
+
throw new Error(`Cannot parse "${d}" into DateInterval`);
|
|
24
|
+
}
|
|
25
|
+
return new DateInterval(LocalDate.of(start), LocalDate.of(end));
|
|
26
|
+
}
|
|
27
|
+
isSame(d) {
|
|
28
|
+
return this.cmp(d) === 0;
|
|
29
|
+
}
|
|
30
|
+
isBefore(d) {
|
|
31
|
+
return this.cmp(d) === -1;
|
|
32
|
+
}
|
|
33
|
+
isSameOrBefore(d) {
|
|
34
|
+
return this.cmp(d) <= 0;
|
|
35
|
+
}
|
|
36
|
+
isAfter(d) {
|
|
37
|
+
return this.cmp(d) === 1;
|
|
38
|
+
}
|
|
39
|
+
isSameOrAfter(d) {
|
|
40
|
+
return this.cmp(d) >= 0;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
44
|
+
*/
|
|
45
|
+
includes(d) {
|
|
46
|
+
d = LocalDate.of(d);
|
|
47
|
+
return d.isSameOrAfter(this.start) && d.isSameOrBefore(this.end);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* DateIntervals compare by start date.
|
|
51
|
+
* If it's the same - then by end date.
|
|
52
|
+
*/
|
|
53
|
+
cmp(d) {
|
|
54
|
+
d = DateInterval.parse(d);
|
|
55
|
+
return this.start.cmp(d.start) || this.end.cmp(d.end);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
59
|
+
* Ranges are INCLUSIVE.
|
|
60
|
+
*/
|
|
61
|
+
getDays() {
|
|
62
|
+
const days = [];
|
|
63
|
+
let current = this.start;
|
|
64
|
+
do {
|
|
65
|
+
days.push(current);
|
|
66
|
+
current = current.add(1, 'day');
|
|
67
|
+
} while (current.isSameOrBefore(this.end));
|
|
68
|
+
return days;
|
|
69
|
+
}
|
|
70
|
+
toString() {
|
|
71
|
+
return [this.start, this.end].join('/');
|
|
72
|
+
}
|
|
73
|
+
toJSON() {
|
|
74
|
+
return this.toString();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -128,6 +128,15 @@ export class LocalDate {
|
|
|
128
128
|
isSameOrAfter(d) {
|
|
129
129
|
return this.cmp(d) >= 0;
|
|
130
130
|
}
|
|
131
|
+
isBetween(min, max, incl = '[)') {
|
|
132
|
+
let r = this.cmp(min);
|
|
133
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
134
|
+
return false;
|
|
135
|
+
r = this.cmp(max);
|
|
136
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
137
|
+
return false;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
131
140
|
/**
|
|
132
141
|
* Returns 1 if this > d
|
|
133
142
|
* returns 0 if they are equal
|
|
@@ -11,6 +11,9 @@ import { LocalDate } from './localDate';
|
|
|
11
11
|
// .valueOf returns unix timestamp (no millis)
|
|
12
12
|
// Prevents dayjs(undefined) being dayjs.now()
|
|
13
13
|
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
14
|
+
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
15
|
+
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
16
|
+
// you only need "whole dates", no time, no timezone worry
|
|
14
17
|
/**
|
|
15
18
|
* @experimental
|
|
16
19
|
*/
|
|
@@ -257,6 +260,15 @@ export class LocalTime {
|
|
|
257
260
|
isSameOrAfter(d) {
|
|
258
261
|
return this.cmp(d) >= 0;
|
|
259
262
|
}
|
|
263
|
+
isBetween(min, max, incl = '[)') {
|
|
264
|
+
let r = this.cmp(min);
|
|
265
|
+
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
266
|
+
return false;
|
|
267
|
+
r = this.cmp(max);
|
|
268
|
+
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
269
|
+
return false;
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
260
272
|
/**
|
|
261
273
|
* Returns 1 if this > d
|
|
262
274
|
* returns 0 if they are equal
|
package/dist-esm/index.js
CHANGED
|
@@ -59,4 +59,5 @@ export * from './math/stack.util';
|
|
|
59
59
|
export * from './string/leven';
|
|
60
60
|
export * from './datetime/localDate';
|
|
61
61
|
export * from './datetime/localTime';
|
|
62
|
+
export * from './datetime/dateInterval';
|
|
62
63
|
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/package.json
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { LocalDate, LocalDateConfig } from './localDate'
|
|
2
|
+
|
|
3
|
+
export type DateIntervalConfig = DateInterval | string
|
|
4
|
+
export type DateIntervalString = string
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
|
|
8
|
+
*
|
|
9
|
+
* @experimental
|
|
10
|
+
*/
|
|
11
|
+
export class DateInterval {
|
|
12
|
+
private constructor(public start: LocalDate, public end: LocalDate) {}
|
|
13
|
+
|
|
14
|
+
static of(start: LocalDateConfig, end: LocalDateConfig): DateInterval {
|
|
15
|
+
return new DateInterval(LocalDate.of(start), LocalDate.of(end))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
|
|
20
|
+
*/
|
|
21
|
+
static parse(d: DateIntervalConfig): DateInterval {
|
|
22
|
+
if (d instanceof DateInterval) return d
|
|
23
|
+
|
|
24
|
+
const [start, end] = d.split('/')
|
|
25
|
+
|
|
26
|
+
if (!end || !start) {
|
|
27
|
+
throw new Error(`Cannot parse "${d}" into DateInterval`)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return new DateInterval(LocalDate.of(start), LocalDate.of(end))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isSame(d: DateIntervalConfig): boolean {
|
|
34
|
+
return this.cmp(d) === 0
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
isBefore(d: DateIntervalConfig): boolean {
|
|
38
|
+
return this.cmp(d) === -1
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
isSameOrBefore(d: DateIntervalConfig): boolean {
|
|
42
|
+
return this.cmp(d) <= 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
isAfter(d: DateIntervalConfig): boolean {
|
|
46
|
+
return this.cmp(d) === 1
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
isSameOrAfter(d: DateIntervalConfig): boolean {
|
|
50
|
+
return this.cmp(d) >= 0
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
55
|
+
*/
|
|
56
|
+
includes(d: LocalDateConfig): boolean {
|
|
57
|
+
d = LocalDate.of(d)
|
|
58
|
+
return d.isSameOrAfter(this.start) && d.isSameOrBefore(this.end)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* DateIntervals compare by start date.
|
|
63
|
+
* If it's the same - then by end date.
|
|
64
|
+
*/
|
|
65
|
+
cmp(d: DateIntervalConfig): -1 | 0 | 1 {
|
|
66
|
+
d = DateInterval.parse(d)
|
|
67
|
+
return this.start.cmp(d.start) || this.end.cmp(d.end)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns an array of LocalDates that are included in the interval.
|
|
72
|
+
* Ranges are INCLUSIVE.
|
|
73
|
+
*/
|
|
74
|
+
getDays(): LocalDate[] {
|
|
75
|
+
const days: LocalDate[] = []
|
|
76
|
+
let current = this.start
|
|
77
|
+
do {
|
|
78
|
+
days.push(current)
|
|
79
|
+
current = current.add(1, 'day')
|
|
80
|
+
} while (current.isSameOrBefore(this.end))
|
|
81
|
+
|
|
82
|
+
return days
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toString(): DateIntervalString {
|
|
86
|
+
return [this.start, this.end].join('/')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toJSON(): DateIntervalString {
|
|
90
|
+
return this.toString()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -4,6 +4,7 @@ import { END, IsoDate, UnixTimestamp } from '../types'
|
|
|
4
4
|
import { LocalTime } from './localTime'
|
|
5
5
|
|
|
6
6
|
export type LocalDateUnit = 'year' | 'month' | 'day'
|
|
7
|
+
export type Inclusiveness = '()' | '[]' | '[)' | '(]'
|
|
7
8
|
|
|
8
9
|
const m31 = new Set<number>([1, 3, 5, 7, 8, 10, 12])
|
|
9
10
|
|
|
@@ -194,6 +195,14 @@ export class LocalDate {
|
|
|
194
195
|
return this.cmp(d) >= 0
|
|
195
196
|
}
|
|
196
197
|
|
|
198
|
+
isBetween(min: LocalDateConfig, max: LocalDateConfig, incl: Inclusiveness = '[)'): boolean {
|
|
199
|
+
let r = this.cmp(min)
|
|
200
|
+
if (r < 0 || (r === 0 && incl[0] === '(')) return false
|
|
201
|
+
r = this.cmp(max)
|
|
202
|
+
if (r > 0 || (r === 0 && incl[1] === ')')) return false
|
|
203
|
+
return true
|
|
204
|
+
}
|
|
205
|
+
|
|
197
206
|
/**
|
|
198
207
|
* Returns 1 if this > d
|
|
199
208
|
* returns 0 if they are equal
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _assert } from '../error/assert'
|
|
2
2
|
import { _ms } from '../time/time.util'
|
|
3
3
|
import { IsoDate, IsoDateTime, UnixTimestamp } from '../types'
|
|
4
|
-
import { LocalDate } from './localDate'
|
|
4
|
+
import { Inclusiveness, LocalDate } from './localDate'
|
|
5
5
|
|
|
6
6
|
export type LocalTimeUnit = 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second'
|
|
7
7
|
|
|
@@ -27,6 +27,9 @@ export interface LocalTimeComponents {
|
|
|
27
27
|
// .valueOf returns unix timestamp (no millis)
|
|
28
28
|
// Prevents dayjs(undefined) being dayjs.now()
|
|
29
29
|
// Validates on parse, throws if invalid. Doesn't allow invalid objects
|
|
30
|
+
// No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
|
|
31
|
+
// Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
|
|
32
|
+
// you only need "whole dates", no time, no timezone worry
|
|
30
33
|
/**
|
|
31
34
|
* @experimental
|
|
32
35
|
*/
|
|
@@ -319,6 +322,14 @@ export class LocalTime {
|
|
|
319
322
|
return this.cmp(d) >= 0
|
|
320
323
|
}
|
|
321
324
|
|
|
325
|
+
isBetween(min: LocalTimeConfig, max: LocalTimeConfig, incl: Inclusiveness = '[)'): boolean {
|
|
326
|
+
let r = this.cmp(min)
|
|
327
|
+
if (r < 0 || (r === 0 && incl[0] === '(')) return false
|
|
328
|
+
r = this.cmp(max)
|
|
329
|
+
if (r > 0 || (r === 0 && incl[1] === ')')) return false
|
|
330
|
+
return true
|
|
331
|
+
}
|
|
332
|
+
|
|
322
333
|
/**
|
|
323
334
|
* Returns 1 if this > d
|
|
324
335
|
* returns 0 if they are equal
|
package/src/index.ts
CHANGED
|
@@ -158,12 +158,17 @@ export * from './math/stack.util'
|
|
|
158
158
|
export * from './string/leven'
|
|
159
159
|
export * from './datetime/localDate'
|
|
160
160
|
export * from './datetime/localTime'
|
|
161
|
-
|
|
161
|
+
export * from './datetime/dateInterval'
|
|
162
|
+
import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate'
|
|
162
163
|
import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime'
|
|
164
|
+
import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval'
|
|
163
165
|
|
|
164
166
|
export type {
|
|
167
|
+
DateIntervalConfig,
|
|
168
|
+
DateIntervalString,
|
|
165
169
|
LocalDateConfig,
|
|
166
170
|
LocalDateUnit,
|
|
171
|
+
Inclusiveness,
|
|
167
172
|
LocalTimeConfig,
|
|
168
173
|
LocalTimeUnit,
|
|
169
174
|
LocalTimeComponents,
|