esday 0.1.1 → 0.2.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;
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
+ interface EsDayFactory {
24
+ (d?: DateType, conf?: {
25
+ utc?: boolean;
26
+ }): EsDay;
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);
@@ -12,9 +42,10 @@ declare class EsDay {
12
42
  private $d;
13
43
  private $u;
14
44
  constructor(cfg: {
15
- d: Date;
45
+ d?: DateType;
16
46
  utc: boolean;
17
47
  });
48
+ private parse;
18
49
  utc(): EsDay;
19
50
  isSame(that: DateType, units: UnitType): boolean;
20
51
  isAfter(that: DateType, units: UnitType): boolean;
@@ -27,6 +58,7 @@ declare class EsDay {
27
58
  startOf(units: UnitType): EsDay;
28
59
  endOf(units: UnitType): EsDay;
29
60
  add(number: number, units: UnitType): EsDay;
61
+ subtract(number: number, units: UnitType): EsDay;
30
62
  get(units: Exclude<UnitType, 'week' | 'w'>): number;
31
63
  set(units: UnitType, value: number): EsDay;
32
64
  toDate(): Date;
@@ -36,32 +68,4 @@ declare class EsDay {
36
68
  private $set;
37
69
  }
38
70
 
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
71
  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 };
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-SOGuLl47.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 './EsDay-SOGuLl47.js';
3
3
 
4
4
  declare const esday: EsDayFactory;
5
5
 
package/index.js CHANGED
@@ -15,6 +15,28 @@ var FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ssZ";
15
15
  var INVALID_DATE_STRING = "Invalid Date";
16
16
  var REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[T\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/i;
17
17
  var REGEX_FORMAT = /\[([^\]]+)\]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
18
+
19
+ // src/core/factory.ts
20
+ var esday = (d, config) => {
21
+ if (d instanceof EsDay) {
22
+ return d;
23
+ } else {
24
+ return new EsDay({
25
+ d,
26
+ utc: false,
27
+ ...config
28
+ });
29
+ }
30
+ };
31
+ esday.extend = (plugin, option) => {
32
+ if (!plugin.$i) {
33
+ plugin(option, EsDay, esday);
34
+ plugin.$i = true;
35
+ }
36
+ return esday;
37
+ };
38
+
39
+ // src/core/utils.ts
18
40
  function prettyUnit(u) {
19
41
  const special = {
20
42
  M,
@@ -58,50 +80,10 @@ function padStart(str, length, pad) {
58
80
  return str;
59
81
  return `${Array.from({ length: length + 1 - str.length }).join(pad)}${str}`;
60
82
  }
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
- }
83
+ function wrapperInst(date, instance) {
84
+ return esday(date, {
85
+ utc: instance["$u"]
86
+ });
105
87
  }
106
88
 
107
89
  // src/core/Impl/add.ts
@@ -151,15 +133,15 @@ function formatImpl(that, formatStr) {
151
133
  case "YY":
152
134
  return String(dates.year).slice(-2);
153
135
  case "YYYY":
154
- return padStart(`${dates.year}`, 4, "0");
136
+ return padStart(dates.year, 4, "0");
155
137
  case "M":
156
138
  return dates.month + 1;
157
139
  case "MM":
158
- return padStart(`${dates.month + 1}`, 2, "0");
140
+ return padStart(dates.month + 1, 2, "0");
159
141
  case "D":
160
142
  return dates.date;
161
143
  case "DD":
162
- return padStart(`${dates.date}`, 2, "0");
144
+ return padStart(dates.date, 2, "0");
163
145
  case "d":
164
146
  return String(dates.day);
165
147
  case "H":
@@ -190,13 +172,88 @@ function formatImpl(that, formatStr) {
190
172
  return str.replace(REGEX_FORMAT, (match, $1) => $1 || matches(match));
191
173
  }
192
174
 
175
+ // src/core/Impl/startOf.ts
176
+ function startOfImpl(that, unit, reverse = false) {
177
+ const result = that.toDate();
178
+ const utc = that["$u"];
179
+ const instanceFactorySet = (method, slice) => {
180
+ const argumentStart = [0, 0, 0, 0];
181
+ const argumentEnd = [23, 59, 59, 999];
182
+ const argument = reverse ? argumentEnd.slice(slice) : argumentStart.slice(slice);
183
+ callDateGetOrSet(result, method, utc, argument);
184
+ };
185
+ const instanceFactory = (d, m) => {
186
+ callDateGetOrSet(result, "Month", utc, [m, d]);
187
+ };
188
+ const $M = callDateGetOrSet(result, "Month", utc);
189
+ switch (prettyUnit(unit)) {
190
+ case Y:
191
+ reverse ? instanceFactory(31, 11) : instanceFactory(1, 0);
192
+ instanceFactorySet("Hours", 0);
193
+ break;
194
+ case M:
195
+ reverse ? instanceFactory(0, $M + 1) : instanceFactory(1, $M);
196
+ instanceFactorySet("Hours", 0);
197
+ break;
198
+ case W: {
199
+ const dayOfWeek = callDateGetOrSet(result, "Day", utc);
200
+ const diff = (dayOfWeek < 1 ? 7 : 0) + dayOfWeek - (reverse ? 6 : 0);
201
+ callDateGetOrSet(result, "Date", utc, callDateGetOrSet(result, "Date", utc) - diff);
202
+ instanceFactorySet("Hours", 0);
203
+ break;
204
+ }
205
+ case D:
206
+ case DATE:
207
+ instanceFactorySet("Hours", 0);
208
+ break;
209
+ case H:
210
+ instanceFactorySet("Minutes", 1);
211
+ break;
212
+ case MIN:
213
+ instanceFactorySet("Seconds", 2);
214
+ break;
215
+ case S:
216
+ instanceFactorySet("Milliseconds", 3);
217
+ break;
218
+ }
219
+ return wrapperInst(result, that);
220
+ }
221
+
222
+ // src/core/parseDate.ts
223
+ function parseDate(date, utc = false) {
224
+ if (isUndefined(date))
225
+ return /* @__PURE__ */ new Date();
226
+ if (date instanceof Date)
227
+ return new Date(date);
228
+ if (typeof date === "string" && !/Z$/i.test(date)) {
229
+ const d = date.match(REGEX_PARSE);
230
+ if (d) {
231
+ const Y2 = Number(d[1]);
232
+ const M2 = Number(d[2]) - 1 || 0;
233
+ const D2 = Number(d[3] || 1);
234
+ const h = Number(d[4] || 0);
235
+ const m = Number(d[5] || 0);
236
+ const s = Number(d[6] || 0);
237
+ const ms = Number((d[7] || "0").substring(0, 3));
238
+ if (utc) {
239
+ return new Date(Date.UTC(Y2, M2, D2, h, m, s, ms));
240
+ }
241
+ return new Date(Y2, M2, D2, h, m, s, ms);
242
+ }
243
+ }
244
+ return new Date(date);
245
+ }
246
+
193
247
  // src/core/EsDay.ts
194
248
  var EsDay = class _EsDay {
195
249
  $d;
196
250
  // utc mode
197
251
  $u = false;
198
252
  constructor(cfg) {
199
- this.$d = cfg.d;
253
+ this.parse(cfg);
254
+ }
255
+ parse(cfg) {
256
+ this.$d = parseDate(cfg.d);
200
257
  this.$u = cfg.utc;
201
258
  }
202
259
  // return utc instance
@@ -236,18 +293,17 @@ var EsDay = class _EsDay {
236
293
  return formatImpl(this, formatStr);
237
294
  }
238
295
  startOf(units) {
239
- const newInst = this.clone();
240
- startOf(newInst.$d, units, newInst.$u);
241
- return newInst;
296
+ return startOfImpl(this, units);
242
297
  }
243
298
  endOf(units) {
244
- const newInst = this.clone();
245
- startOf(newInst.$d, units, newInst.$u, true);
246
- return newInst;
299
+ return startOfImpl(this, units, true);
247
300
  }
248
301
  add(number, units) {
249
302
  return addImpl(this, number, units);
250
303
  }
304
+ subtract(number, units) {
305
+ return this.add(-number, units);
306
+ }
251
307
  get(units) {
252
308
  return getAllFieldsInDate(this.$d, this.$u)[prettyUnit(units)];
253
309
  }
@@ -293,48 +349,4 @@ var EsDay = class _EsDay {
293
349
  };
294
350
  });
295
351
 
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
- }
317
- }
318
- return new Date(date);
319
- }
320
-
321
- // src/core/index.ts
322
- var esday = (d) => {
323
- if (d instanceof EsDay) {
324
- return d;
325
- } else {
326
- return new EsDay({
327
- d: parseDate(d),
328
- utc: false
329
- });
330
- }
331
- };
332
- esday.extend = (plugin, option) => {
333
- if (!plugin.$i) {
334
- plugin(option, EsDay, esday);
335
- plugin.$i = true;
336
- }
337
- return esday;
338
- };
339
-
340
352
  export { EsDay, esday };
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.2.0",
5
5
  "description": "A simple date library fully written in TypeScript",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
@@ -0,0 +1,13 @@
1
+ import { D as DateType, a as EsDay, c as EsDayPlugin } from '../../EsDay-SOGuLl47.js';
2
+
3
+ declare module 'esday' {
4
+ interface EsDayFactory {
5
+ (d?: DateType, conf?: {
6
+ utc?: boolean;
7
+ format?: string;
8
+ }): EsDay;
9
+ }
10
+ }
11
+ declare const customParseFormatPlugin: EsDayPlugin<{}>;
12
+
13
+ export { customParseFormatPlugin };
@@ -0,0 +1,156 @@
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(cfg) {
143
+ const format = cfg.format;
144
+ if (typeof cfg.d === "string" && format) {
145
+ const date = parseFormattedInput(cfg.d, format, cfg.utc);
146
+ if (Number.isNaN(date.getTime()))
147
+ this["$d"] = /* @__PURE__ */ new Date("");
148
+ else
149
+ this["$d"] = date;
150
+ } else {
151
+ oldParse.call(this, cfg);
152
+ }
153
+ };
154
+ };
155
+
156
+ export { customParseFormatPlugin };
@@ -1,10 +1,10 @@
1
- import { c as EsDayPlugin } from '../../index-vJ2vX1HG.js';
1
+ import { c as EsDayPlugin } from '../../EsDay-SOGuLl47.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 };