esday 0.1.1 → 0.3.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.
@@ -1,3 +1,33 @@
1
+ type DateType = Date | string | number | EsDay | undefined;
2
+ type PrettyUnitType = 'date' | 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute' | 'second' | 'millisecond';
3
+ type PrettyUnit<T extends UnitType> = T extends 'D' ? 'date' : T extends 'w' ? 'week' : T extends 'M' ? 'month' : T extends 'y' ? 'year' : T extends 'H' ? 'hour' : T extends 'm' ? 'minute' : T extends 's' ? 'second' : T extends 'ms' ? 'millisecond' : T;
4
+ type UnitType = 'y' | 'M' | 'D' | 'w' | 'H' | 'm' | 's' | 'ms' | PrettyUnitType;
5
+ interface AllDateFields {
6
+ year: number;
7
+ month: number;
8
+ /**
9
+ * date in month
10
+ * 1 ~ 31
11
+ */
12
+ date: number;
13
+ /**
14
+ * day in week
15
+ * 0 ~ 6
16
+ */
17
+ day: number;
18
+ hour: number;
19
+ minute: number;
20
+ second: number;
21
+ millisecond: number;
22
+ }
23
+ type EsDayFactoryParserFn = (d?: DateType, ...others: any[]) => EsDay;
24
+ interface EsDayFactory {
25
+ (...args: Parameters<EsDayFactoryParserFn>): ReturnType<EsDayFactoryParserFn>;
26
+ utc: EsDayFactoryParserFn;
27
+ extend: <T extends {}>(plugin: EsDayPlugin<T>, option?: T) => EsDayFactory;
28
+ }
29
+ type EsDayPlugin<T extends {}> = (option: T, dayTsClass: typeof EsDay, esday: EsDayFactory) => void;
30
+
1
31
  declare interface EsDay {
2
32
  year: (() => number) & ((value: number) => EsDay);
3
33
  month: (() => number) & ((value: number) => EsDay);
@@ -11,10 +41,8 @@ declare interface EsDay {
11
41
  declare class EsDay {
12
42
  private $d;
13
43
  private $u;
14
- constructor(cfg: {
15
- d: Date;
16
- utc: boolean;
17
- });
44
+ constructor(d: Exclude<DateType, EsDay>, utc: boolean, ...others: any[]);
45
+ private parse;
18
46
  utc(): EsDay;
19
47
  isSame(that: DateType, units: UnitType): boolean;
20
48
  isAfter(that: DateType, units: UnitType): boolean;
@@ -27,6 +55,7 @@ declare class EsDay {
27
55
  startOf(units: UnitType): EsDay;
28
56
  endOf(units: UnitType): EsDay;
29
57
  add(number: number, units: UnitType): EsDay;
58
+ subtract(number: number, units: UnitType): EsDay;
30
59
  get(units: Exclude<UnitType, 'week' | 'w'>): number;
31
60
  set(units: UnitType, value: number): EsDay;
32
61
  toDate(): Date;
@@ -36,32 +65,4 @@ declare class EsDay {
36
65
  private $set;
37
66
  }
38
67
 
39
- type DateType = Date | string | number | EsDay;
40
- type PrettyUnitType = 'date' | 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute' | 'second' | 'millisecond';
41
- type PrettyUnit<T extends UnitType> = T extends 'D' ? 'date' : T extends 'w' ? 'week' : T extends 'M' ? 'month' : T extends 'y' ? 'year' : T extends 'H' ? 'hour' : T extends 'm' ? 'minute' : T extends 's' ? 'second' : T extends 'ms' ? 'millisecond' : T;
42
- type UnitType = 'y' | 'M' | 'D' | 'w' | 'H' | 'm' | 's' | 'ms' | PrettyUnitType;
43
- interface AllDateFields {
44
- year: number;
45
- month: number;
46
- /**
47
- * date in month
48
- * 1 ~ 31
49
- */
50
- date: number;
51
- /**
52
- * day in week
53
- * 0 ~ 6
54
- */
55
- day: number;
56
- hour: number;
57
- minute: number;
58
- second: number;
59
- millisecond: number;
60
- }
61
- interface EsDayFactory {
62
- (d?: DateType): EsDay;
63
- extend: (plugin: EsDayPlugin, option?: any) => EsDayFactory;
64
- }
65
- type EsDayPlugin = (option: any, dayTsClass: typeof EsDay, esday: EsDayFactory) => void;
66
-
67
- export { type AllDateFields as A, type DateType as D, type EsDayFactory as E, type PrettyUnitType as P, type UnitType as U, EsDay as a, type PrettyUnit as b, type EsDayPlugin as c };
68
+ export { type AllDateFields as A, type DateType as D, type EsDayFactory as E, type PrettyUnitType as P, type UnitType as U, EsDay as a, type PrettyUnit as b, type EsDayFactoryParserFn as c, type EsDayPlugin as d };
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { E as EsDayFactory } from './index-vJ2vX1HG.js';
2
- export { A as AllDateFields, D as DateType, a as EsDay, c as EsDayPlugin, b as PrettyUnit, P as PrettyUnitType, U as UnitType } from './index-vJ2vX1HG.js';
1
+ import { E as EsDayFactory } from './EsDay-CKy0jEj2.js';
2
+ export { A as AllDateFields, D as DateType, a as EsDay, c as EsDayFactoryParserFn, d as EsDayPlugin, b as PrettyUnit, P as PrettyUnitType, U as UnitType } from './EsDay-CKy0jEj2.js';
3
3
 
4
4
  declare const esday: EsDayFactory;
5
5
 
package/index.js CHANGED
@@ -58,50 +58,10 @@ function padStart(str, length, pad) {
58
58
  return str;
59
59
  return `${Array.from({ length: length + 1 - str.length }).join(pad)}${str}`;
60
60
  }
61
-
62
- // src/core/funcs/startOf.ts
63
- function startOf(date, unit, utc, reverse = false) {
64
- const result = date;
65
- const instanceFactorySet = (method, slice) => {
66
- const argumentStart = [0, 0, 0, 0];
67
- const argumentEnd = [23, 59, 59, 999];
68
- const argument = reverse ? argumentEnd.slice(slice) : argumentStart.slice(slice);
69
- callDateGetOrSet(result, method, utc, argument);
70
- };
71
- const instanceFactory = (d, m) => {
72
- callDateGetOrSet(result, "Month", utc, [m, d]);
73
- };
74
- const $M = callDateGetOrSet(result, "Month", utc);
75
- switch (prettyUnit(unit)) {
76
- case Y:
77
- reverse ? instanceFactory(31, 11) : instanceFactory(1, 0);
78
- instanceFactorySet("Hours", 0);
79
- break;
80
- case M:
81
- reverse ? instanceFactory(0, $M + 1) : instanceFactory(1, $M);
82
- instanceFactorySet("Hours", 0);
83
- break;
84
- case W: {
85
- const dayOfWeek = callDateGetOrSet(result, "Day", utc);
86
- const diff = (dayOfWeek < 1 ? 7 : 0) + dayOfWeek - (reverse ? 6 : 0);
87
- callDateGetOrSet(result, "Date", utc, callDateGetOrSet(result, "Date", utc) - diff);
88
- instanceFactorySet("Hours", 0);
89
- break;
90
- }
91
- case D:
92
- case DATE:
93
- instanceFactorySet("Hours", 0);
94
- break;
95
- case H:
96
- instanceFactorySet("Minutes", 1);
97
- break;
98
- case MIN:
99
- instanceFactorySet("Seconds", 2);
100
- break;
101
- case S:
102
- instanceFactorySet("Milliseconds", 3);
103
- break;
104
- }
61
+ function wrapperInst(date, instance) {
62
+ const newInst = instance.clone();
63
+ newInst["$d"] = new Date(date);
64
+ return newInst;
105
65
  }
106
66
 
107
67
  // src/core/Impl/add.ts
@@ -131,7 +91,7 @@ function addImpl(that, number, units) {
131
91
  second: 1e3
132
92
  }[unit] || 1;
133
93
  const nextTimeStamp = $d.getTime() + number * step;
134
- return new EsDay({ d: new Date(nextTimeStamp), utc: that["$u"] });
94
+ return wrapperInst(new Date(nextTimeStamp), that);
135
95
  }
136
96
 
137
97
  // src/core/Impl/format.ts
@@ -151,15 +111,15 @@ function formatImpl(that, formatStr) {
151
111
  case "YY":
152
112
  return String(dates.year).slice(-2);
153
113
  case "YYYY":
154
- return padStart(`${dates.year}`, 4, "0");
114
+ return padStart(dates.year, 4, "0");
155
115
  case "M":
156
116
  return dates.month + 1;
157
117
  case "MM":
158
- return padStart(`${dates.month + 1}`, 2, "0");
118
+ return padStart(dates.month + 1, 2, "0");
159
119
  case "D":
160
120
  return dates.date;
161
121
  case "DD":
162
- return padStart(`${dates.date}`, 2, "0");
122
+ return padStart(dates.date, 2, "0");
163
123
  case "d":
164
124
  return String(dates.day);
165
125
  case "H":
@@ -190,21 +150,93 @@ function formatImpl(that, formatStr) {
190
150
  return str.replace(REGEX_FORMAT, (match, $1) => $1 || matches(match));
191
151
  }
192
152
 
153
+ // src/core/Impl/startOf.ts
154
+ function startOfImpl(that, unit, reverse = false) {
155
+ const result = that.toDate();
156
+ const utc = that["$u"];
157
+ const instanceFactorySet = (method, slice) => {
158
+ const argumentStart = [0, 0, 0, 0];
159
+ const argumentEnd = [23, 59, 59, 999];
160
+ const argument = reverse ? argumentEnd.slice(slice) : argumentStart.slice(slice);
161
+ callDateGetOrSet(result, method, utc, argument);
162
+ };
163
+ const instanceFactory = (d, m) => {
164
+ callDateGetOrSet(result, "Month", utc, [m, d]);
165
+ };
166
+ const $M = callDateGetOrSet(result, "Month", utc);
167
+ switch (prettyUnit(unit)) {
168
+ case Y:
169
+ reverse ? instanceFactory(31, 11) : instanceFactory(1, 0);
170
+ instanceFactorySet("Hours", 0);
171
+ break;
172
+ case M:
173
+ reverse ? instanceFactory(0, $M + 1) : instanceFactory(1, $M);
174
+ instanceFactorySet("Hours", 0);
175
+ break;
176
+ case W: {
177
+ const dayOfWeek = callDateGetOrSet(result, "Day", utc);
178
+ const diff = (dayOfWeek < 1 ? 7 : 0) + dayOfWeek - (reverse ? 6 : 0);
179
+ callDateGetOrSet(result, "Date", utc, callDateGetOrSet(result, "Date", utc) - diff);
180
+ instanceFactorySet("Hours", 0);
181
+ break;
182
+ }
183
+ case D:
184
+ case DATE:
185
+ instanceFactorySet("Hours", 0);
186
+ break;
187
+ case H:
188
+ instanceFactorySet("Minutes", 1);
189
+ break;
190
+ case MIN:
191
+ instanceFactorySet("Seconds", 2);
192
+ break;
193
+ case S:
194
+ instanceFactorySet("Milliseconds", 3);
195
+ break;
196
+ }
197
+ return wrapperInst(result, that);
198
+ }
199
+
200
+ // src/core/parseDate.ts
201
+ function parseDate(date, utc = false) {
202
+ if (isUndefined(date))
203
+ return /* @__PURE__ */ new Date();
204
+ if (date instanceof Date)
205
+ return new Date(date);
206
+ if (typeof date === "string" && !/Z$/i.test(date)) {
207
+ const d = date.match(REGEX_PARSE);
208
+ if (d) {
209
+ const Y2 = Number(d[1]);
210
+ const M2 = Number(d[2]) - 1 || 0;
211
+ const D2 = Number(d[3] || 1);
212
+ const h = Number(d[4] || 0);
213
+ const m = Number(d[5] || 0);
214
+ const s = Number(d[6] || 0);
215
+ const ms = Number((d[7] || "0").substring(0, 3));
216
+ if (utc) {
217
+ return new Date(Date.UTC(Y2, M2, D2, h, m, s, ms));
218
+ }
219
+ return new Date(Y2, M2, D2, h, m, s, ms);
220
+ }
221
+ }
222
+ return new Date(date);
223
+ }
224
+
193
225
  // src/core/EsDay.ts
194
226
  var EsDay = class _EsDay {
195
227
  $d;
196
228
  // utc mode
197
229
  $u = false;
198
- constructor(cfg) {
199
- this.$d = cfg.d;
200
- this.$u = cfg.utc;
230
+ constructor(d, utc, ...others) {
231
+ this.parse(d, utc, ...others);
232
+ }
233
+ parse(d, utc, ..._others) {
234
+ this.$d = parseDate(d);
235
+ this.$u = utc;
201
236
  }
202
237
  // return utc instance
203
238
  utc() {
204
- return new _EsDay({
205
- d: this.toDate(),
206
- utc: true
207
- });
239
+ return new _EsDay(this.toDate(), true);
208
240
  }
209
241
  isSame(that, units) {
210
242
  const other = esday(that);
@@ -227,27 +259,23 @@ var EsDay = class _EsDay {
227
259
  return !(this.$d.toString() === INVALID_DATE_STRING);
228
260
  }
229
261
  clone() {
230
- return new _EsDay({
231
- d: new Date(this.$d),
232
- utc: this.$u
233
- });
262
+ return new _EsDay(this.toDate(), this.$u);
234
263
  }
235
264
  format(formatStr) {
236
265
  return formatImpl(this, formatStr);
237
266
  }
238
267
  startOf(units) {
239
- const newInst = this.clone();
240
- startOf(newInst.$d, units, newInst.$u);
241
- return newInst;
268
+ return startOfImpl(this, units);
242
269
  }
243
270
  endOf(units) {
244
- const newInst = this.clone();
245
- startOf(newInst.$d, units, newInst.$u, true);
246
- return newInst;
271
+ return startOfImpl(this, units, true);
247
272
  }
248
273
  add(number, units) {
249
274
  return addImpl(this, number, units);
250
275
  }
276
+ subtract(number, units) {
277
+ return this.add(-number, units);
278
+ }
251
279
  get(units) {
252
280
  return getAllFieldsInDate(this.$d, this.$u)[prettyUnit(units)];
253
281
  }
@@ -293,40 +321,19 @@ var EsDay = class _EsDay {
293
321
  };
294
322
  });
295
323
 
296
- // src/core/parseDate.ts
297
- function parseDate(date, utc = false) {
298
- if (isUndefined(date))
299
- return /* @__PURE__ */ new Date();
300
- if (date instanceof Date)
301
- return new Date(date);
302
- if (typeof date === "string" && !/Z$/i.test(date)) {
303
- const d = date.match(REGEX_PARSE);
304
- if (d) {
305
- const Y2 = Number(d[1]);
306
- const M2 = Number(d[2]) - 1 || 0;
307
- const D2 = Number(d[3] || 1);
308
- const h = Number(d[4] || 0);
309
- const m = Number(d[5] || 0);
310
- const s = Number(d[6] || 0);
311
- const ms = Number((d[7] || "0").substring(0, 3));
312
- if (utc) {
313
- return new Date(Date.UTC(Y2, M2, D2, h, m, s, ms));
314
- }
315
- return new Date(Y2, M2, D2, h, m, s, ms);
316
- }
324
+ // src/core/factory.ts
325
+ var esday = (d, ...others) => {
326
+ if (d instanceof EsDay) {
327
+ return d.clone();
328
+ } else {
329
+ return new EsDay(d, false, ...others);
317
330
  }
318
- return new Date(date);
319
- }
320
-
321
- // src/core/index.ts
322
- var esday = (d) => {
331
+ };
332
+ esday.utc = (d, ...others) => {
323
333
  if (d instanceof EsDay) {
324
- return d;
334
+ return d.clone().utc();
325
335
  } else {
326
- return new EsDay({
327
- d: parseDate(d),
328
- utc: false
329
- });
336
+ return new EsDay(d, true, ...others);
330
337
  }
331
338
  };
332
339
  esday.extend = (plugin, option) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "esday",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.3.0",
5
5
  "description": "A simple date library fully written in TypeScript",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
@@ -0,0 +1,5 @@
1
+ import { d as EsDayPlugin } from '../../EsDay-CKy0jEj2.js';
2
+
3
+ declare const customParseFormatPlugin: EsDayPlugin<{}>;
4
+
5
+ export { customParseFormatPlugin };
@@ -0,0 +1,159 @@
1
+ // src/plugins/customParseFormat/index.ts
2
+ var formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|([AaQz]|YYYY|YY?|ww?|MM?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|ZZ?)/g;
3
+ var match1 = /\d/;
4
+ var match2 = /\d{2}/;
5
+ var match3 = /\d{3}/;
6
+ var match4 = /\d{4}/;
7
+ var match1to2 = /\d\d?/;
8
+ var matchSigned = /[+-]?\d+/;
9
+ var matchOffset = /[+-]\d\d:?(\d\d)?|Z/;
10
+ function parseTwoDigitYear(input) {
11
+ const year = +input;
12
+ return year + (year > 68 ? 1900 : 2e3);
13
+ }
14
+ function offsetFromString(offset) {
15
+ if (!offset)
16
+ return 0;
17
+ if (offset === "Z")
18
+ return 0;
19
+ const parts = offset.match(/([+-]|\d\d)/g);
20
+ if (!parts)
21
+ return 0;
22
+ const minutes = +parts[1] * 60 + (+parts[2] || 0);
23
+ return parts[0] === "+" ? -minutes : minutes;
24
+ }
25
+ function addInput(property) {
26
+ return function(input) {
27
+ this[property] = +input;
28
+ };
29
+ }
30
+ var expressions = {
31
+ A: [match1, function() {
32
+ this.afternoon = true;
33
+ }],
34
+ a: [match1, function() {
35
+ this.afternoon = true;
36
+ }],
37
+ Q: [match1, function(input) {
38
+ this.month = (Number.parseInt(input, 10) - 1) * 3 + 1;
39
+ }],
40
+ S: [match1, function(input) {
41
+ this.milliseconds = +input * 100;
42
+ }],
43
+ SS: [match2, function(input) {
44
+ this.milliseconds = +input * 10;
45
+ }],
46
+ SSS: [match3, function(input) {
47
+ this.milliseconds = +input;
48
+ }],
49
+ s: [match1to2, addInput("seconds")],
50
+ ss: [match1to2, addInput("seconds")],
51
+ m: [match1to2, addInput("minutes")],
52
+ mm: [match1to2, addInput("minutes")],
53
+ H: [match1to2, addInput("hours")],
54
+ h: [match1to2, addInput("hours")],
55
+ HH: [match1to2, addInput("hours")],
56
+ hh: [match1to2, addInput("hours")],
57
+ D: [match1to2, addInput("day")],
58
+ DD: [match2, addInput("day")],
59
+ Do: [match1to2, function(input) {
60
+ this.day = Number.parseInt(input.match(/\d+/)?.[0] || "0", 10);
61
+ }],
62
+ w: [match1to2, addInput("week")],
63
+ ww: [match2, addInput("week")],
64
+ M: [match1to2, addInput("month")],
65
+ MM: [match2, addInput("month")],
66
+ Y: [matchSigned, addInput("year")],
67
+ YY: [match2, function(input) {
68
+ this.year = parseTwoDigitYear(input);
69
+ }],
70
+ YYYY: [match4, addInput("year")],
71
+ Z: [matchOffset, function(input) {
72
+ this.zone = { offset: offsetFromString(input) };
73
+ }],
74
+ ZZ: [matchOffset, function(input) {
75
+ this.zone = { offset: offsetFromString(input) };
76
+ }]
77
+ };
78
+ function correctHours(time) {
79
+ if (time.afternoon !== void 0) {
80
+ if (time.hours !== void 0) {
81
+ if (time.afternoon) {
82
+ if (time.hours < 12)
83
+ time.hours += 12;
84
+ } else if (time.hours === 12) {
85
+ time.hours = 0;
86
+ }
87
+ }
88
+ delete time.afternoon;
89
+ }
90
+ }
91
+ function makeParser(format) {
92
+ const array = format.match(formattingTokens) || [];
93
+ const { length } = array;
94
+ for (let i = 0; i < length; i += 1) {
95
+ const token = array[i];
96
+ const parseTo = expressions[token];
97
+ const regex = parseTo && parseTo[0];
98
+ const parser = parseTo && parseTo[1];
99
+ if (parser) {
100
+ array[i] = { regex, parser };
101
+ } else {
102
+ array[i] = token.replace(/^\[|\]$/g, "");
103
+ }
104
+ }
105
+ return function(input) {
106
+ const time = {};
107
+ for (let i = 0, start = 0; i < length; i += 1) {
108
+ const token = array[i];
109
+ if (typeof token === "string") {
110
+ start += token.length;
111
+ } else {
112
+ const { regex, parser } = token;
113
+ const part = input.slice(start);
114
+ const match = regex.exec(part);
115
+ const value = match[0];
116
+ parser.call(time, value);
117
+ input = input.replace(value, "");
118
+ }
119
+ }
120
+ correctHours(time);
121
+ return time;
122
+ };
123
+ }
124
+ function parseFormattedInput(input, format, utc) {
125
+ try {
126
+ const parser = makeParser(format);
127
+ const { year, month, day, hours, minutes, seconds, milliseconds, zone } = parser(input);
128
+ if (utc) {
129
+ return new Date(Date.UTC(year || 0, (month || 1) - 1, day || 1, hours || 0, minutes || 0, seconds || 0, milliseconds || 0));
130
+ }
131
+ if (zone) {
132
+ const offsetMs = (zone.offset || 0) * 6e4;
133
+ return new Date(Date.UTC(year || 0, (month || 1) - 1, day || 1, hours || 0, minutes || 0, seconds || 0, milliseconds || 0) - offsetMs);
134
+ }
135
+ return new Date(year || 0, (month || 1) - 1, day || 1, hours || 0, minutes || 0, seconds || 0, milliseconds || 0);
136
+ } catch {
137
+ return new Date(Number.NaN);
138
+ }
139
+ }
140
+ var customParseFormatPlugin = (_, dayTsClass) => {
141
+ const oldParse = dayTsClass.prototype["parse"];
142
+ dayTsClass.prototype["parse"] = function(d, ...others) {
143
+ const utc = others[0];
144
+ const format = others[1];
145
+ if (typeof d === "string" && typeof format === "string") {
146
+ const date = parseFormattedInput(d, format, utc);
147
+ if (Number.isNaN(date.getTime())) {
148
+ this["$d"] = /* @__PURE__ */ new Date("");
149
+ } else {
150
+ this["$d"] = date;
151
+ this["$u"] = utc;
152
+ }
153
+ } else {
154
+ oldParse.call(this, d, utc);
155
+ }
156
+ };
157
+ };
158
+
159
+ export { customParseFormatPlugin };
@@ -1,10 +1,10 @@
1
- import { c as EsDayPlugin } from '../../index-vJ2vX1HG.js';
1
+ import { d as EsDayPlugin } from '../../EsDay-CKy0jEj2.js';
2
2
 
3
3
  declare module 'esday' {
4
4
  interface EsDay {
5
5
  isToday: () => boolean;
6
6
  }
7
7
  }
8
- declare const isTodayPlugin: EsDayPlugin;
8
+ declare const isTodayPlugin: EsDayPlugin<{}>;
9
9
 
10
10
  export { isTodayPlugin };