@autometa/datetime 0.0.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/.eslintignore +3 -0
- package/.eslintrc.cjs +4 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/esm/index.js +252 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +278 -0
- package/package.json +45 -0
- package/tsup.config.ts +13 -0
package/.eslintignore
ADDED
package/.eslintrc.cjs
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) undefined Ben Aherne
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
var __accessCheck = (obj, member, msg) => {
|
|
2
|
+
if (!member.has(obj))
|
|
3
|
+
throw TypeError("Cannot " + msg);
|
|
4
|
+
};
|
|
5
|
+
var __privateGet = (obj, member, getter) => {
|
|
6
|
+
__accessCheck(obj, member, "read from private field");
|
|
7
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
+
};
|
|
9
|
+
var __privateAdd = (obj, member, value) => {
|
|
10
|
+
if (member.has(obj))
|
|
11
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
+
};
|
|
14
|
+
var __privateMethod = (obj, member, method) => {
|
|
15
|
+
__accessCheck(obj, member, "access private method");
|
|
16
|
+
return method;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/dates/date-factory.ts
|
|
20
|
+
import { camel, convertPhrase } from "@autometa/phrases";
|
|
21
|
+
|
|
22
|
+
// src/dates/midnight.ts
|
|
23
|
+
function midnight() {
|
|
24
|
+
const date = /* @__PURE__ */ new Date();
|
|
25
|
+
date.setHours(0, 0, 0, 0);
|
|
26
|
+
return date;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/dates/date-factory.ts
|
|
30
|
+
var _matchDayPhrase, matchDayPhrase_fn, _addDays, addDays_fn, _addSeconds, addSeconds_fn, _addMilliseconds, addMilliseconds_fn, _addHours, addHours_fn, _addMinutes, addMinutes_fn;
|
|
31
|
+
var DateFactory = class {
|
|
32
|
+
constructor() {
|
|
33
|
+
__privateAdd(this, _matchDayPhrase);
|
|
34
|
+
__privateAdd(this, _addDays);
|
|
35
|
+
__privateAdd(this, _addSeconds);
|
|
36
|
+
__privateAdd(this, _addMilliseconds);
|
|
37
|
+
__privateAdd(this, _addHours);
|
|
38
|
+
__privateAdd(this, _addMinutes);
|
|
39
|
+
this.phraseMap = /* @__PURE__ */ new Map([
|
|
40
|
+
["beforeYesterday", this.make(-2, "days")],
|
|
41
|
+
["yesterday", this.make(-1, "days")],
|
|
42
|
+
["today", this.make(0, "days")],
|
|
43
|
+
["tomorrow", this.make(1, "days")],
|
|
44
|
+
["afterTomorrow", this.make(2, "days")],
|
|
45
|
+
["nextWeek", this.make(7, "days")],
|
|
46
|
+
["lastWeek", this.make(-7, "days")],
|
|
47
|
+
["nextFortnight", this.make(14, "days")],
|
|
48
|
+
["lastFortnight", this.make(-14, "days")],
|
|
49
|
+
["midnight", midnight()]
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
fromPhrase(phrase) {
|
|
53
|
+
const name = convertPhrase(phrase, camel);
|
|
54
|
+
if (this.phraseMap.has(name)) {
|
|
55
|
+
return this.phraseMap.get(name);
|
|
56
|
+
}
|
|
57
|
+
const matchDay = __privateMethod(this, _matchDayPhrase, matchDayPhrase_fn).call(this, phrase);
|
|
58
|
+
if (matchDay) {
|
|
59
|
+
return matchDay;
|
|
60
|
+
}
|
|
61
|
+
const parsed = new Date(phrase);
|
|
62
|
+
if (!isNaN(parsed.getTime()))
|
|
63
|
+
return parsed;
|
|
64
|
+
throw new Error(`Cannot find date matching '${phrase}'`);
|
|
65
|
+
}
|
|
66
|
+
make(daysOffset, timeunit) {
|
|
67
|
+
switch (timeunit) {
|
|
68
|
+
case "days":
|
|
69
|
+
return __privateMethod(this, _addDays, addDays_fn).call(this, daysOffset);
|
|
70
|
+
case "seconds":
|
|
71
|
+
return __privateMethod(this, _addSeconds, addSeconds_fn).call(this, daysOffset);
|
|
72
|
+
case "milliseconds":
|
|
73
|
+
return __privateMethod(this, _addMilliseconds, addMilliseconds_fn).call(this, daysOffset);
|
|
74
|
+
case "hours":
|
|
75
|
+
return __privateMethod(this, _addHours, addHours_fn).call(this, daysOffset);
|
|
76
|
+
case "minutes":
|
|
77
|
+
return __privateMethod(this, _addMinutes, addMinutes_fn).call(this, daysOffset);
|
|
78
|
+
default:
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Invalid timeunit ${timeunit}, options are 'days', 'seconds', 'milliseconds', 'hours', 'minutes'`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
_matchDayPhrase = new WeakSet();
|
|
86
|
+
matchDayPhrase_fn = function(phrase) {
|
|
87
|
+
if (phrase.match(/\d+ day(s)? ago?/gi)) {
|
|
88
|
+
const value = Number(phrase.match(/\d+/gi)?.pop());
|
|
89
|
+
return this.make(-value, "days");
|
|
90
|
+
}
|
|
91
|
+
if (phrase.match(/\d+ day(s)?( from now)?/gi)) {
|
|
92
|
+
const value = Number(phrase.match(/\d+/gi)?.pop());
|
|
93
|
+
return this.make(value, "days");
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
_addDays = new WeakSet();
|
|
97
|
+
addDays_fn = function(daysOffset) {
|
|
98
|
+
const date = /* @__PURE__ */ new Date();
|
|
99
|
+
date.setDate(date.getDate() + daysOffset);
|
|
100
|
+
return date;
|
|
101
|
+
};
|
|
102
|
+
_addSeconds = new WeakSet();
|
|
103
|
+
addSeconds_fn = function(secondsOffset) {
|
|
104
|
+
const date = /* @__PURE__ */ new Date();
|
|
105
|
+
date.setSeconds(date.getSeconds() + secondsOffset);
|
|
106
|
+
return date;
|
|
107
|
+
};
|
|
108
|
+
_addMilliseconds = new WeakSet();
|
|
109
|
+
addMilliseconds_fn = function(millisecondsOffset) {
|
|
110
|
+
const date = /* @__PURE__ */ new Date();
|
|
111
|
+
date.setMilliseconds(date.getMilliseconds() + millisecondsOffset);
|
|
112
|
+
return date;
|
|
113
|
+
};
|
|
114
|
+
_addHours = new WeakSet();
|
|
115
|
+
addHours_fn = function(hoursOffset) {
|
|
116
|
+
const date = /* @__PURE__ */ new Date();
|
|
117
|
+
date.setHours(date.getHours() + hoursOffset);
|
|
118
|
+
return date;
|
|
119
|
+
};
|
|
120
|
+
_addMinutes = new WeakSet();
|
|
121
|
+
addMinutes_fn = function(minutesOffset) {
|
|
122
|
+
const date = /* @__PURE__ */ new Date();
|
|
123
|
+
date.setMinutes(date.getMinutes() + minutesOffset);
|
|
124
|
+
return date;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/dates/iso-date-factory.ts
|
|
128
|
+
var IsoDateFactory = class {
|
|
129
|
+
constructor(dateFactory) {
|
|
130
|
+
this.dateFactory = dateFactory;
|
|
131
|
+
this.phraseMap = /* @__PURE__ */ new Map([
|
|
132
|
+
["beforeYesterday", this.make(-2, "days")],
|
|
133
|
+
["yesterday", this.make(-1, "days")],
|
|
134
|
+
["today", this.make(0, "days")],
|
|
135
|
+
["tomorrow", this.make(1, "days")],
|
|
136
|
+
["afterTomorrow", this.make(2, "days")],
|
|
137
|
+
["nextWeek", this.make(7, "days")],
|
|
138
|
+
["lastWeek", this.make(-7, "days")],
|
|
139
|
+
["nextFortnight", this.make(14, "days")],
|
|
140
|
+
["lastFortnight", this.make(-14, "days")],
|
|
141
|
+
["midnight", midnight().toISOString()]
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
make(daysOffset, timeunit) {
|
|
145
|
+
return this.dateFactory.make(daysOffset, timeunit).toISOString();
|
|
146
|
+
}
|
|
147
|
+
fromPhrase(phrase) {
|
|
148
|
+
return this.dateFactory.fromPhrase(phrase)?.toISOString();
|
|
149
|
+
}
|
|
150
|
+
get beforeYesterday() {
|
|
151
|
+
return this.phraseMap.get("beforeYesterday");
|
|
152
|
+
}
|
|
153
|
+
get yesterday() {
|
|
154
|
+
return this.phraseMap.get("yesterday");
|
|
155
|
+
}
|
|
156
|
+
get today() {
|
|
157
|
+
return this.phraseMap.get("today");
|
|
158
|
+
}
|
|
159
|
+
get tomorrow() {
|
|
160
|
+
return this.phraseMap.get("tomorrow");
|
|
161
|
+
}
|
|
162
|
+
get afterTomorrow() {
|
|
163
|
+
return this.phraseMap.get("afterTomorrow");
|
|
164
|
+
}
|
|
165
|
+
get midnight() {
|
|
166
|
+
return this.phraseMap.get("midnight");
|
|
167
|
+
}
|
|
168
|
+
get lastWeek() {
|
|
169
|
+
return this.phraseMap.get("lastWeek");
|
|
170
|
+
}
|
|
171
|
+
get nextWeek() {
|
|
172
|
+
return this.phraseMap.get("nextWeek");
|
|
173
|
+
}
|
|
174
|
+
get lastFortnight() {
|
|
175
|
+
return this.phraseMap.get("lastFortnight");
|
|
176
|
+
}
|
|
177
|
+
get nextFortnight() {
|
|
178
|
+
return this.phraseMap.get("nextFortnight");
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/dates/formatted-date-factory.ts
|
|
183
|
+
var FmtDateFactory = class {
|
|
184
|
+
constructor(dateFactory) {
|
|
185
|
+
this.dateFactory = dateFactory;
|
|
186
|
+
}
|
|
187
|
+
make(daysOffset, timeunit) {
|
|
188
|
+
return this.dateFactory.make(daysOffset, timeunit).toISOString().split("T")[0];
|
|
189
|
+
}
|
|
190
|
+
fromPhrase(phrase) {
|
|
191
|
+
return this.dateFactory.fromPhrase(phrase)?.toISOString().split("T")[0];
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/dates/dates.ts
|
|
196
|
+
var _factory, _iso, _fmt;
|
|
197
|
+
var DatesObject = class {
|
|
198
|
+
constructor() {
|
|
199
|
+
__privateAdd(this, _factory, new DateFactory());
|
|
200
|
+
__privateAdd(this, _iso, new IsoDateFactory(__privateGet(this, _factory)));
|
|
201
|
+
__privateAdd(this, _fmt, new FmtDateFactory(__privateGet(this, _factory)));
|
|
202
|
+
}
|
|
203
|
+
get iso() {
|
|
204
|
+
return __privateGet(this, _iso);
|
|
205
|
+
}
|
|
206
|
+
get fmt() {
|
|
207
|
+
return __privateGet(this, _fmt);
|
|
208
|
+
}
|
|
209
|
+
get beforeYesterday() {
|
|
210
|
+
return __privateGet(this, _factory).phraseMap.get("beforeYesterday");
|
|
211
|
+
}
|
|
212
|
+
get yesterday() {
|
|
213
|
+
return __privateGet(this, _factory).phraseMap.get("yesterday");
|
|
214
|
+
}
|
|
215
|
+
get today() {
|
|
216
|
+
return __privateGet(this, _factory).phraseMap.get("today");
|
|
217
|
+
}
|
|
218
|
+
get tomorrow() {
|
|
219
|
+
return __privateGet(this, _factory).phraseMap.get("tomorrow");
|
|
220
|
+
}
|
|
221
|
+
get afterTomorrow() {
|
|
222
|
+
return __privateGet(this, _factory).phraseMap.get("afterTomorrow");
|
|
223
|
+
}
|
|
224
|
+
get midnight() {
|
|
225
|
+
return __privateGet(this, _factory).phraseMap.get("midnight");
|
|
226
|
+
}
|
|
227
|
+
get lastWeek() {
|
|
228
|
+
return __privateGet(this, _factory).phraseMap.get("lastWeek");
|
|
229
|
+
}
|
|
230
|
+
get nextWeek() {
|
|
231
|
+
return __privateGet(this, _factory).phraseMap.get("nextWeek");
|
|
232
|
+
}
|
|
233
|
+
get lastFortnight() {
|
|
234
|
+
return __privateGet(this, _factory).phraseMap.get("lastFortnight");
|
|
235
|
+
}
|
|
236
|
+
get nextFortnight() {
|
|
237
|
+
return __privateGet(this, _factory).phraseMap.get("nextFortnight");
|
|
238
|
+
}
|
|
239
|
+
fromPhrase(phrase) {
|
|
240
|
+
return __privateGet(this, _factory).fromPhrase(phrase);
|
|
241
|
+
}
|
|
242
|
+
make(timeOffset, timeUnit) {
|
|
243
|
+
return __privateGet(this, _factory).make(timeOffset, timeUnit);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
_factory = new WeakMap();
|
|
247
|
+
_iso = new WeakMap();
|
|
248
|
+
_fmt = new WeakMap();
|
|
249
|
+
var Dates = new DatesObject();
|
|
250
|
+
export {
|
|
251
|
+
Dates
|
|
252
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { TimeUnit } from '@autometa/phrases';
|
|
2
|
+
|
|
3
|
+
declare class DateFactory {
|
|
4
|
+
#private;
|
|
5
|
+
phraseMap: Map<string, Date>;
|
|
6
|
+
fromPhrase(phrase: string): Date | undefined;
|
|
7
|
+
make(daysOffset: number, timeunit: TimeUnit): Date;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare class IsoDateFactory {
|
|
11
|
+
readonly dateFactory: DateFactory;
|
|
12
|
+
constructor(dateFactory: DateFactory);
|
|
13
|
+
phraseMap: Map<string, string>;
|
|
14
|
+
make(daysOffset: number, timeunit: TimeUnit): string;
|
|
15
|
+
fromPhrase(phrase: string): string | undefined;
|
|
16
|
+
get beforeYesterday(): string | undefined;
|
|
17
|
+
get yesterday(): string | undefined;
|
|
18
|
+
get today(): string | undefined;
|
|
19
|
+
get tomorrow(): string | undefined;
|
|
20
|
+
get afterTomorrow(): string | undefined;
|
|
21
|
+
get midnight(): string | undefined;
|
|
22
|
+
get lastWeek(): string | undefined;
|
|
23
|
+
get nextWeek(): string | undefined;
|
|
24
|
+
get lastFortnight(): string | undefined;
|
|
25
|
+
get nextFortnight(): string | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class FmtDateFactory {
|
|
29
|
+
readonly dateFactory: DateFactory;
|
|
30
|
+
constructor(dateFactory: DateFactory);
|
|
31
|
+
make(daysOffset: number, timeunit: TimeUnit): string;
|
|
32
|
+
fromPhrase(phrase: string): string | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare class DatesObject {
|
|
36
|
+
#private;
|
|
37
|
+
get iso(): IsoDateFactory;
|
|
38
|
+
get fmt(): FmtDateFactory;
|
|
39
|
+
get beforeYesterday(): Date | undefined;
|
|
40
|
+
get yesterday(): Date | undefined;
|
|
41
|
+
get today(): Date | undefined;
|
|
42
|
+
get tomorrow(): Date | undefined;
|
|
43
|
+
get afterTomorrow(): Date | undefined;
|
|
44
|
+
get midnight(): Date | undefined;
|
|
45
|
+
get lastWeek(): Date | undefined;
|
|
46
|
+
get nextWeek(): Date | undefined;
|
|
47
|
+
get lastFortnight(): Date | undefined;
|
|
48
|
+
get nextFortnight(): Date | undefined;
|
|
49
|
+
fromPhrase(phrase: string): Date | undefined;
|
|
50
|
+
make(timeOffset: number, timeUnit: TimeUnit): Date;
|
|
51
|
+
}
|
|
52
|
+
declare const Dates: DatesObject;
|
|
53
|
+
|
|
54
|
+
export { Dates };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __accessCheck = (obj, member, msg) => {
|
|
20
|
+
if (!member.has(obj))
|
|
21
|
+
throw TypeError("Cannot " + msg);
|
|
22
|
+
};
|
|
23
|
+
var __privateGet = (obj, member, getter) => {
|
|
24
|
+
__accessCheck(obj, member, "read from private field");
|
|
25
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
+
};
|
|
27
|
+
var __privateAdd = (obj, member, value) => {
|
|
28
|
+
if (member.has(obj))
|
|
29
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
+
};
|
|
32
|
+
var __privateMethod = (obj, member, method) => {
|
|
33
|
+
__accessCheck(obj, member, "access private method");
|
|
34
|
+
return method;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/index.ts
|
|
38
|
+
var src_exports = {};
|
|
39
|
+
__export(src_exports, {
|
|
40
|
+
Dates: () => Dates
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(src_exports);
|
|
43
|
+
|
|
44
|
+
// src/dates/date-factory.ts
|
|
45
|
+
var import_phrases = require("@autometa/phrases");
|
|
46
|
+
|
|
47
|
+
// src/dates/midnight.ts
|
|
48
|
+
function midnight() {
|
|
49
|
+
const date = /* @__PURE__ */ new Date();
|
|
50
|
+
date.setHours(0, 0, 0, 0);
|
|
51
|
+
return date;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/dates/date-factory.ts
|
|
55
|
+
var _matchDayPhrase, matchDayPhrase_fn, _addDays, addDays_fn, _addSeconds, addSeconds_fn, _addMilliseconds, addMilliseconds_fn, _addHours, addHours_fn, _addMinutes, addMinutes_fn;
|
|
56
|
+
var DateFactory = class {
|
|
57
|
+
constructor() {
|
|
58
|
+
__privateAdd(this, _matchDayPhrase);
|
|
59
|
+
__privateAdd(this, _addDays);
|
|
60
|
+
__privateAdd(this, _addSeconds);
|
|
61
|
+
__privateAdd(this, _addMilliseconds);
|
|
62
|
+
__privateAdd(this, _addHours);
|
|
63
|
+
__privateAdd(this, _addMinutes);
|
|
64
|
+
this.phraseMap = /* @__PURE__ */ new Map([
|
|
65
|
+
["beforeYesterday", this.make(-2, "days")],
|
|
66
|
+
["yesterday", this.make(-1, "days")],
|
|
67
|
+
["today", this.make(0, "days")],
|
|
68
|
+
["tomorrow", this.make(1, "days")],
|
|
69
|
+
["afterTomorrow", this.make(2, "days")],
|
|
70
|
+
["nextWeek", this.make(7, "days")],
|
|
71
|
+
["lastWeek", this.make(-7, "days")],
|
|
72
|
+
["nextFortnight", this.make(14, "days")],
|
|
73
|
+
["lastFortnight", this.make(-14, "days")],
|
|
74
|
+
["midnight", midnight()]
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
fromPhrase(phrase) {
|
|
78
|
+
const name = (0, import_phrases.convertPhrase)(phrase, import_phrases.camel);
|
|
79
|
+
if (this.phraseMap.has(name)) {
|
|
80
|
+
return this.phraseMap.get(name);
|
|
81
|
+
}
|
|
82
|
+
const matchDay = __privateMethod(this, _matchDayPhrase, matchDayPhrase_fn).call(this, phrase);
|
|
83
|
+
if (matchDay) {
|
|
84
|
+
return matchDay;
|
|
85
|
+
}
|
|
86
|
+
const parsed = new Date(phrase);
|
|
87
|
+
if (!isNaN(parsed.getTime()))
|
|
88
|
+
return parsed;
|
|
89
|
+
throw new Error(`Cannot find date matching '${phrase}'`);
|
|
90
|
+
}
|
|
91
|
+
make(daysOffset, timeunit) {
|
|
92
|
+
switch (timeunit) {
|
|
93
|
+
case "days":
|
|
94
|
+
return __privateMethod(this, _addDays, addDays_fn).call(this, daysOffset);
|
|
95
|
+
case "seconds":
|
|
96
|
+
return __privateMethod(this, _addSeconds, addSeconds_fn).call(this, daysOffset);
|
|
97
|
+
case "milliseconds":
|
|
98
|
+
return __privateMethod(this, _addMilliseconds, addMilliseconds_fn).call(this, daysOffset);
|
|
99
|
+
case "hours":
|
|
100
|
+
return __privateMethod(this, _addHours, addHours_fn).call(this, daysOffset);
|
|
101
|
+
case "minutes":
|
|
102
|
+
return __privateMethod(this, _addMinutes, addMinutes_fn).call(this, daysOffset);
|
|
103
|
+
default:
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Invalid timeunit ${timeunit}, options are 'days', 'seconds', 'milliseconds', 'hours', 'minutes'`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
_matchDayPhrase = new WeakSet();
|
|
111
|
+
matchDayPhrase_fn = function(phrase) {
|
|
112
|
+
if (phrase.match(/\d+ day(s)? ago?/gi)) {
|
|
113
|
+
const value = Number(phrase.match(/\d+/gi)?.pop());
|
|
114
|
+
return this.make(-value, "days");
|
|
115
|
+
}
|
|
116
|
+
if (phrase.match(/\d+ day(s)?( from now)?/gi)) {
|
|
117
|
+
const value = Number(phrase.match(/\d+/gi)?.pop());
|
|
118
|
+
return this.make(value, "days");
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
_addDays = new WeakSet();
|
|
122
|
+
addDays_fn = function(daysOffset) {
|
|
123
|
+
const date = /* @__PURE__ */ new Date();
|
|
124
|
+
date.setDate(date.getDate() + daysOffset);
|
|
125
|
+
return date;
|
|
126
|
+
};
|
|
127
|
+
_addSeconds = new WeakSet();
|
|
128
|
+
addSeconds_fn = function(secondsOffset) {
|
|
129
|
+
const date = /* @__PURE__ */ new Date();
|
|
130
|
+
date.setSeconds(date.getSeconds() + secondsOffset);
|
|
131
|
+
return date;
|
|
132
|
+
};
|
|
133
|
+
_addMilliseconds = new WeakSet();
|
|
134
|
+
addMilliseconds_fn = function(millisecondsOffset) {
|
|
135
|
+
const date = /* @__PURE__ */ new Date();
|
|
136
|
+
date.setMilliseconds(date.getMilliseconds() + millisecondsOffset);
|
|
137
|
+
return date;
|
|
138
|
+
};
|
|
139
|
+
_addHours = new WeakSet();
|
|
140
|
+
addHours_fn = function(hoursOffset) {
|
|
141
|
+
const date = /* @__PURE__ */ new Date();
|
|
142
|
+
date.setHours(date.getHours() + hoursOffset);
|
|
143
|
+
return date;
|
|
144
|
+
};
|
|
145
|
+
_addMinutes = new WeakSet();
|
|
146
|
+
addMinutes_fn = function(minutesOffset) {
|
|
147
|
+
const date = /* @__PURE__ */ new Date();
|
|
148
|
+
date.setMinutes(date.getMinutes() + minutesOffset);
|
|
149
|
+
return date;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// src/dates/iso-date-factory.ts
|
|
153
|
+
var IsoDateFactory = class {
|
|
154
|
+
constructor(dateFactory) {
|
|
155
|
+
this.dateFactory = dateFactory;
|
|
156
|
+
this.phraseMap = /* @__PURE__ */ new Map([
|
|
157
|
+
["beforeYesterday", this.make(-2, "days")],
|
|
158
|
+
["yesterday", this.make(-1, "days")],
|
|
159
|
+
["today", this.make(0, "days")],
|
|
160
|
+
["tomorrow", this.make(1, "days")],
|
|
161
|
+
["afterTomorrow", this.make(2, "days")],
|
|
162
|
+
["nextWeek", this.make(7, "days")],
|
|
163
|
+
["lastWeek", this.make(-7, "days")],
|
|
164
|
+
["nextFortnight", this.make(14, "days")],
|
|
165
|
+
["lastFortnight", this.make(-14, "days")],
|
|
166
|
+
["midnight", midnight().toISOString()]
|
|
167
|
+
]);
|
|
168
|
+
}
|
|
169
|
+
make(daysOffset, timeunit) {
|
|
170
|
+
return this.dateFactory.make(daysOffset, timeunit).toISOString();
|
|
171
|
+
}
|
|
172
|
+
fromPhrase(phrase) {
|
|
173
|
+
return this.dateFactory.fromPhrase(phrase)?.toISOString();
|
|
174
|
+
}
|
|
175
|
+
get beforeYesterday() {
|
|
176
|
+
return this.phraseMap.get("beforeYesterday");
|
|
177
|
+
}
|
|
178
|
+
get yesterday() {
|
|
179
|
+
return this.phraseMap.get("yesterday");
|
|
180
|
+
}
|
|
181
|
+
get today() {
|
|
182
|
+
return this.phraseMap.get("today");
|
|
183
|
+
}
|
|
184
|
+
get tomorrow() {
|
|
185
|
+
return this.phraseMap.get("tomorrow");
|
|
186
|
+
}
|
|
187
|
+
get afterTomorrow() {
|
|
188
|
+
return this.phraseMap.get("afterTomorrow");
|
|
189
|
+
}
|
|
190
|
+
get midnight() {
|
|
191
|
+
return this.phraseMap.get("midnight");
|
|
192
|
+
}
|
|
193
|
+
get lastWeek() {
|
|
194
|
+
return this.phraseMap.get("lastWeek");
|
|
195
|
+
}
|
|
196
|
+
get nextWeek() {
|
|
197
|
+
return this.phraseMap.get("nextWeek");
|
|
198
|
+
}
|
|
199
|
+
get lastFortnight() {
|
|
200
|
+
return this.phraseMap.get("lastFortnight");
|
|
201
|
+
}
|
|
202
|
+
get nextFortnight() {
|
|
203
|
+
return this.phraseMap.get("nextFortnight");
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// src/dates/formatted-date-factory.ts
|
|
208
|
+
var FmtDateFactory = class {
|
|
209
|
+
constructor(dateFactory) {
|
|
210
|
+
this.dateFactory = dateFactory;
|
|
211
|
+
}
|
|
212
|
+
make(daysOffset, timeunit) {
|
|
213
|
+
return this.dateFactory.make(daysOffset, timeunit).toISOString().split("T")[0];
|
|
214
|
+
}
|
|
215
|
+
fromPhrase(phrase) {
|
|
216
|
+
return this.dateFactory.fromPhrase(phrase)?.toISOString().split("T")[0];
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/dates/dates.ts
|
|
221
|
+
var _factory, _iso, _fmt;
|
|
222
|
+
var DatesObject = class {
|
|
223
|
+
constructor() {
|
|
224
|
+
__privateAdd(this, _factory, new DateFactory());
|
|
225
|
+
__privateAdd(this, _iso, new IsoDateFactory(__privateGet(this, _factory)));
|
|
226
|
+
__privateAdd(this, _fmt, new FmtDateFactory(__privateGet(this, _factory)));
|
|
227
|
+
}
|
|
228
|
+
get iso() {
|
|
229
|
+
return __privateGet(this, _iso);
|
|
230
|
+
}
|
|
231
|
+
get fmt() {
|
|
232
|
+
return __privateGet(this, _fmt);
|
|
233
|
+
}
|
|
234
|
+
get beforeYesterday() {
|
|
235
|
+
return __privateGet(this, _factory).phraseMap.get("beforeYesterday");
|
|
236
|
+
}
|
|
237
|
+
get yesterday() {
|
|
238
|
+
return __privateGet(this, _factory).phraseMap.get("yesterday");
|
|
239
|
+
}
|
|
240
|
+
get today() {
|
|
241
|
+
return __privateGet(this, _factory).phraseMap.get("today");
|
|
242
|
+
}
|
|
243
|
+
get tomorrow() {
|
|
244
|
+
return __privateGet(this, _factory).phraseMap.get("tomorrow");
|
|
245
|
+
}
|
|
246
|
+
get afterTomorrow() {
|
|
247
|
+
return __privateGet(this, _factory).phraseMap.get("afterTomorrow");
|
|
248
|
+
}
|
|
249
|
+
get midnight() {
|
|
250
|
+
return __privateGet(this, _factory).phraseMap.get("midnight");
|
|
251
|
+
}
|
|
252
|
+
get lastWeek() {
|
|
253
|
+
return __privateGet(this, _factory).phraseMap.get("lastWeek");
|
|
254
|
+
}
|
|
255
|
+
get nextWeek() {
|
|
256
|
+
return __privateGet(this, _factory).phraseMap.get("nextWeek");
|
|
257
|
+
}
|
|
258
|
+
get lastFortnight() {
|
|
259
|
+
return __privateGet(this, _factory).phraseMap.get("lastFortnight");
|
|
260
|
+
}
|
|
261
|
+
get nextFortnight() {
|
|
262
|
+
return __privateGet(this, _factory).phraseMap.get("nextFortnight");
|
|
263
|
+
}
|
|
264
|
+
fromPhrase(phrase) {
|
|
265
|
+
return __privateGet(this, _factory).fromPhrase(phrase);
|
|
266
|
+
}
|
|
267
|
+
make(timeOffset, timeUnit) {
|
|
268
|
+
return __privateGet(this, _factory).make(timeOffset, timeUnit);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
_factory = new WeakMap();
|
|
272
|
+
_iso = new WeakMap();
|
|
273
|
+
_fmt = new WeakMap();
|
|
274
|
+
var Dates = new DatesObject();
|
|
275
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
276
|
+
0 && (module.exports = {
|
|
277
|
+
Dates
|
|
278
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@autometa/datetime",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Date and Time utilities for Autometa",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./dist/esm/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/esm/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^18.11.18",
|
|
18
|
+
"@types/uuid": "^9.0.1",
|
|
19
|
+
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
20
|
+
"@typescript-eslint/parser": "^5.54.1",
|
|
21
|
+
"eslint": "^8.37.0",
|
|
22
|
+
"eslint-config-custom": "0.5.1",
|
|
23
|
+
"eslint-config-prettier": "^8.3.0",
|
|
24
|
+
"rimraf": "^4.1.2",
|
|
25
|
+
"tsconfig": " *",
|
|
26
|
+
"tsup": "^6.7.0",
|
|
27
|
+
"typescript": "^4.9.5",
|
|
28
|
+
"vitest": "^0.29.8"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@autometa/asserters": "^0.0.0",
|
|
32
|
+
"@autometa/errors": "^0.0.0",
|
|
33
|
+
"@autometa/phrases": "^0.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "vitest run --passWithNoTests",
|
|
37
|
+
"prettify": "prettier --config .prettierrc 'src/**/*.ts' --write",
|
|
38
|
+
"lint": "eslint . --max-warnings 0",
|
|
39
|
+
"lint:fix": "eslint . --fix",
|
|
40
|
+
"clean": "rimraf dist",
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"build:watch": "tsup --watch"
|
|
43
|
+
},
|
|
44
|
+
"readme": "# Introduction\n\nThere's nothing here yet"
|
|
45
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
clean: true, // clean up the dist folder
|
|
5
|
+
format: ["cjs", "esm"], // generate cjs and esm files
|
|
6
|
+
dts: true,
|
|
7
|
+
skipNodeModulesBundle: true,
|
|
8
|
+
entryPoints: ["src/index.ts"],
|
|
9
|
+
target: "es2020",
|
|
10
|
+
outDir: "dist",
|
|
11
|
+
legacyOutput: true,
|
|
12
|
+
external: ["dist"],
|
|
13
|
+
});
|