esday 0.0.2 → 0.1.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.
@@ -0,0 +1,67 @@
1
+ declare interface EsDay {
2
+ year: (() => number) & ((value: number) => EsDay);
3
+ month: (() => number) & ((value: number) => EsDay);
4
+ date: (() => number) & ((value: number) => EsDay);
5
+ day: (() => number) & ((value: number) => EsDay);
6
+ hour: (() => number) & ((value: number) => EsDay);
7
+ minute: (() => number) & ((value: number) => EsDay);
8
+ second: (() => number) & ((value: number) => EsDay);
9
+ millisecond: (() => number) & ((value: number) => EsDay);
10
+ }
11
+ declare class EsDay {
12
+ private $d;
13
+ private $u;
14
+ constructor(cfg: {
15
+ d: Date;
16
+ utc: boolean;
17
+ });
18
+ utc(): EsDay;
19
+ isSame(that: DateType, units: UnitType): boolean;
20
+ isAfter(that: DateType, units: UnitType): boolean;
21
+ isBefore(that: DateType, units: UnitType): boolean;
22
+ valueOf(): number;
23
+ unix(): number;
24
+ isValid(): boolean;
25
+ clone(): EsDay;
26
+ format(formatStr: string): string;
27
+ startOf(units: UnitType): EsDay;
28
+ endOf(units: UnitType): EsDay;
29
+ add(number: number, units: UnitType): EsDay;
30
+ get(units: Exclude<UnitType, 'week' | 'w'>): number;
31
+ set(units: UnitType, value: number): EsDay;
32
+ toDate(): Date;
33
+ toJSON(): string | null;
34
+ toISOString(): string;
35
+ toString(): string;
36
+ private $set;
37
+ }
38
+
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 };
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
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';
3
+
4
+ declare const esday: EsDayFactory;
5
+
6
+ export { EsDayFactory, esday };
package/index.js CHANGED
@@ -11,18 +11,10 @@ var M = "month";
11
11
  var Q = "quarter";
12
12
  var Y = "year";
13
13
  var DATE = "date";
14
-
15
- // src/core/funcs/formatDate.ts
16
- function formatDate(d, formatStr) {
17
- const year = d.$y;
18
- const month = d.$M + 1;
19
- const date = d.$D;
20
- const hours = d.$H;
21
- const minutes = d.$m;
22
- const seconds = d.$s;
23
- const milliseconds = d.$ms;
24
- return formatStr.replace(/YYYY/g, year.toString()).replace(/MM/g, month.toString().padStart(2, "0")).replace(/DD/g, date.toString().padStart(2, "0")).replace(/HH/g, hours.toString().padStart(2, "0")).replace(/mm/g, minutes.toString().padStart(2, "0")).replace(/ss/g, seconds.toString().padStart(2, "0")).replace(/SSS/g, milliseconds.toString().padStart(3, "0"));
25
- }
14
+ var FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ssZ";
15
+ var INVALID_DATE_STRING = "Invalid Date";
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
+ 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;
26
18
  function prettyUnit(u) {
27
19
  const special = {
28
20
  M,
@@ -48,6 +40,24 @@ function callDateGetOrSet(date, field, utc, value) {
48
40
  }
49
41
  return date[`get${utc ? "UTC" : ""}${field}`]();
50
42
  }
43
+ function getAllFieldsInDate(date, utc) {
44
+ return {
45
+ year: callDateGetOrSet(date, "FullYear", utc),
46
+ month: callDateGetOrSet(date, "Month", utc),
47
+ date: callDateGetOrSet(date, "Date", utc),
48
+ day: callDateGetOrSet(date, "Day", utc),
49
+ hour: callDateGetOrSet(date, "Hours", utc),
50
+ minute: callDateGetOrSet(date, "Minutes", utc),
51
+ second: callDateGetOrSet(date, "Seconds", utc),
52
+ millisecond: callDateGetOrSet(date, "Milliseconds", utc)
53
+ };
54
+ }
55
+ function padStart(str, length, pad) {
56
+ str = String(str);
57
+ if (!str || str.length >= length)
58
+ return str;
59
+ return `${Array.from({ length: length + 1 - str.length }).join(pad)}${str}`;
60
+ }
51
61
 
52
62
  // src/core/funcs/startOf.ts
53
63
  function startOf(date, unit, utc, reverse = false) {
@@ -94,42 +104,118 @@ function startOf(date, unit, utc, reverse = false) {
94
104
  }
95
105
  }
96
106
 
107
+ // src/core/Impl/add.ts
108
+ function addImpl(that, number, units) {
109
+ const $d = that["$d"];
110
+ const unit = prettyUnit(units);
111
+ const instanceFactorySet = (multiplier) => {
112
+ const newInstance = that.clone();
113
+ newInstance["$d"].setDate($d.getDate() + Math.round(multiplier * number));
114
+ return newInstance;
115
+ };
116
+ if (unit === M) {
117
+ return that.set("month", that.get("month") + number);
118
+ }
119
+ if (unit === Y) {
120
+ return that.set("year", that.get("year") + number);
121
+ }
122
+ if (unit === DATE || unit === D) {
123
+ return instanceFactorySet(1);
124
+ }
125
+ if (unit === W) {
126
+ return instanceFactorySet(7);
127
+ }
128
+ const step = {
129
+ minute: 60 * 1e3,
130
+ hour: 60 * 60 * 1e3,
131
+ second: 1e3
132
+ }[unit] || 1;
133
+ const nextTimeStamp = $d.getTime() + number * step;
134
+ return new EsDay({ d: new Date(nextTimeStamp), utc: that["$u"] });
135
+ }
136
+
137
+ // src/core/Impl/format.ts
138
+ function formatImpl(that, formatStr) {
139
+ if (!that.isValid())
140
+ return INVALID_DATE_STRING;
141
+ const $d = that["$d"];
142
+ const dates = getAllFieldsInDate($d, that["$u"]);
143
+ const str = formatStr || FORMAT_DEFAULT;
144
+ const get$H = (num) => padStart(dates.hour % 12 || 12, num, "0");
145
+ const meridiemFunc = (hour, _minute, isLowercase) => {
146
+ const m = hour < 12 ? "AM" : "PM";
147
+ return isLowercase ? m.toLowerCase() : m;
148
+ };
149
+ const matches = (match) => {
150
+ switch (match) {
151
+ case "YY":
152
+ return String(dates.year).slice(-2);
153
+ case "YYYY":
154
+ return padStart(`${dates.year}`, 4, "0");
155
+ case "M":
156
+ return dates.month + 1;
157
+ case "MM":
158
+ return padStart(`${dates.month + 1}`, 2, "0");
159
+ case "D":
160
+ return dates.date;
161
+ case "DD":
162
+ return padStart(`${dates.date}`, 2, "0");
163
+ case "d":
164
+ return String(dates.day);
165
+ case "H":
166
+ return String(dates.hour);
167
+ case "HH":
168
+ return padStart(dates.hour, 2, "0");
169
+ case "h":
170
+ return get$H(1);
171
+ case "hh":
172
+ return get$H(2);
173
+ case "a":
174
+ return meridiemFunc(dates.hour, dates.minute, true);
175
+ case "A":
176
+ return meridiemFunc(dates.hour, dates.minute, false);
177
+ case "m":
178
+ return String(dates.minute);
179
+ case "mm":
180
+ return padStart(dates.minute, 2, "0");
181
+ case "s":
182
+ return String(dates.second);
183
+ case "ss":
184
+ return padStart(dates.second, 2, "0");
185
+ case "SSS":
186
+ return padStart(dates.millisecond, 3, "0");
187
+ }
188
+ return null;
189
+ };
190
+ return str.replace(REGEX_FORMAT, (match, $1) => $1 || matches(match));
191
+ }
192
+
97
193
  // src/core/EsDay.ts
98
194
  var EsDay = class _EsDay {
99
195
  $d;
100
- $y;
101
- $M;
102
- $D;
103
- $W;
104
- $H;
105
- $m;
106
- $s;
107
- $ms;
108
196
  // utc mode
109
197
  $u = false;
110
198
  constructor(cfg) {
111
199
  this.$d = cfg.d;
112
200
  this.$u = cfg.utc;
113
- this.init();
114
- }
115
- init() {
116
- const $d = this.$d;
117
- this.$y = callDateGetOrSet($d, "FullYear", this.$u);
118
- this.$M = callDateGetOrSet($d, "Month", this.$u);
119
- this.$D = callDateGetOrSet($d, "Date", this.$u);
120
- this.$W = callDateGetOrSet($d, "Day", this.$u);
121
- this.$H = callDateGetOrSet($d, "Hours", this.$u);
122
- this.$m = callDateGetOrSet($d, "Minutes", this.$u);
123
- this.$s = callDateGetOrSet($d, "Seconds", this.$u);
124
- this.$ms = callDateGetOrSet($d, "Milliseconds", this.$u);
125
201
  }
126
202
  // return utc instance
127
203
  utc() {
128
204
  return new _EsDay({
129
- d: this.$d,
205
+ d: this.toDate(),
130
206
  utc: true
131
207
  });
132
208
  }
209
+ isSame(that, units) {
210
+ const other = esday(that);
211
+ return this.startOf(units) <= other && other <= this.endOf(units);
212
+ }
213
+ isAfter(that, units) {
214
+ return esday(that) < this.startOf(units);
215
+ }
216
+ isBefore(that, units) {
217
+ return this.endOf(units) < esday(that);
218
+ }
133
219
  // return this milliseconds
134
220
  valueOf() {
135
221
  return this.$d.getTime();
@@ -137,6 +223,9 @@ var EsDay = class _EsDay {
137
223
  unix() {
138
224
  return Math.floor(this.valueOf() / 1e3);
139
225
  }
226
+ isValid() {
227
+ return !(this.$d.toString() === INVALID_DATE_STRING);
228
+ }
140
229
  clone() {
141
230
  return new _EsDay({
142
231
  d: new Date(this.$d),
@@ -144,31 +233,39 @@ var EsDay = class _EsDay {
144
233
  });
145
234
  }
146
235
  format(formatStr) {
147
- return formatDate({
148
- $y: this.$y,
149
- $M: this.$M,
150
- $D: this.$D,
151
- $H: this.$H,
152
- $m: this.$m,
153
- $s: this.$s,
154
- $ms: this.$ms
155
- }, formatStr);
236
+ return formatImpl(this, formatStr);
156
237
  }
157
238
  startOf(units) {
158
239
  const newInst = this.clone();
159
240
  startOf(newInst.$d, units, newInst.$u);
160
- newInst.init();
161
241
  return newInst;
162
242
  }
163
243
  endOf(units) {
164
244
  const newInst = this.clone();
165
245
  startOf(newInst.$d, units, newInst.$u, true);
166
- newInst.init();
167
246
  return newInst;
168
247
  }
248
+ add(number, units) {
249
+ return addImpl(this, number, units);
250
+ }
251
+ get(units) {
252
+ return getAllFieldsInDate(this.$d, this.$u)[prettyUnit(units)];
253
+ }
169
254
  set(units, value) {
170
255
  return this.clone().$set(units, value);
171
256
  }
257
+ toDate() {
258
+ return new Date(this.valueOf());
259
+ }
260
+ toJSON() {
261
+ return this.isValid() ? this.toISOString() : null;
262
+ }
263
+ toISOString() {
264
+ return this.$d.toISOString();
265
+ }
266
+ toString() {
267
+ return this.$d.toUTCString();
268
+ }
172
269
  $set(units, value) {
173
270
  const unit = prettyUnit(units);
174
271
  const name = {
@@ -181,14 +278,43 @@ var EsDay = class _EsDay {
181
278
  [S]: `Seconds`,
182
279
  [MS]: `Milliseconds`
183
280
  }[unit];
184
- const arg = unit === D ? this.$D + (value - this.$W) : value;
281
+ const arg = unit === D ? this.get("date") + (value - this.get("day")) : value;
185
282
  callDateGetOrSet(this.$d, name, this.$u, arg);
186
283
  return this;
187
284
  }
188
285
  };
286
+ ["year", "month", "date", "day", "hour", "minute", "second", "millisecond"].forEach((key) => {
287
+ EsDay.prototype[key] = function(value) {
288
+ if (value !== void 0) {
289
+ return this.set(key, value);
290
+ } else {
291
+ return this.get(key);
292
+ }
293
+ };
294
+ });
189
295
 
190
296
  // src/core/parseDate.ts
191
- function parseDate(date) {
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
+ }
192
318
  return new Date(date);
193
319
  }
194
320
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "esday",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.1.0",
5
5
  "description": "A simple date library fully written in TypeScript",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
@@ -19,7 +19,9 @@
19
19
  "module": "index.js",
20
20
  "types": "index.d.ts",
21
21
  "typesVersions": {},
22
- "files": [],
22
+ "files": [
23
+ "*"
24
+ ],
23
25
  "dependencies": {
24
26
  "radash": "^12.1.0"
25
27
  },
@@ -0,0 +1,10 @@
1
+ import { c as EsDayPlugin } from '../../index-vJ2vX1HG.js';
2
+
3
+ declare module 'esday' {
4
+ interface EsDay {
5
+ isToday: () => boolean;
6
+ }
7
+ }
8
+ declare const isTodayPlugin: EsDayPlugin;
9
+
10
+ export { isTodayPlugin };
@@ -0,0 +1,8 @@
1
+ // src/plugins/isToday/index.ts
2
+ var isTodayPlugin = (_, dayClass) => {
3
+ dayClass.prototype.isToday = function() {
4
+ return true;
5
+ };
6
+ };
7
+
8
+ export { isTodayPlugin };