esday 0.0.1

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,39 @@
1
+ declare class Dayts {
2
+ private $d;
3
+ private $y;
4
+ private $M;
5
+ private $D;
6
+ private $W;
7
+ private $H;
8
+ private $m;
9
+ private $s;
10
+ private $ms;
11
+ private $u;
12
+ constructor(cfg: {
13
+ d: Date;
14
+ utc: boolean;
15
+ });
16
+ init(): void;
17
+ utc(): Dayts;
18
+ valueOf(): number;
19
+ unix(): number;
20
+ clone(): Dayts;
21
+ format(formatStr: string): string;
22
+ startOf(units: UnitType): Dayts;
23
+ endOf(units: UnitType): Dayts;
24
+ set(units: UnitType, value: number): Dayts;
25
+ private $set;
26
+ }
27
+
28
+ type DateType = Date | string | number | Dayts;
29
+ type PrettyUnitType = 'date' | 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute' | 'second' | 'millisecond';
30
+ type UnitType = 'y' | 'M' | 'D' | 'W' | 'H' | 'm' | 's' | 'ms' | PrettyUnitType;
31
+ interface DaytsFactory {
32
+ (d?: DateType): Dayts;
33
+ extend: (plugin: DaytsPlugin, option?: any) => DaytsFactory;
34
+ }
35
+ type DaytsPlugin = (option: any, dayTsClass: typeof Dayts, dayts: DaytsFactory) => void;
36
+
37
+ declare const dayts: DaytsFactory;
38
+
39
+ export { type DateType, Dayts, type DaytsFactory, type DaytsPlugin, type PrettyUnitType, type UnitType, dayts };
package/dist/index.js ADDED
@@ -0,0 +1,214 @@
1
+ import { isArray } from 'radash';
2
+
3
+ // src/core/constant.ts
4
+ var MS = "millisecond";
5
+ var S = "second";
6
+ var MIN = "minute";
7
+ var H = "hour";
8
+ var D = "day";
9
+ var W = "week";
10
+ var M = "month";
11
+ var Q = "quarter";
12
+ var Y = "year";
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
+ }
26
+ function prettyUnit(u) {
27
+ const special = {
28
+ M,
29
+ y: Y,
30
+ w: W,
31
+ d: D,
32
+ D: DATE,
33
+ h: H,
34
+ m: MIN,
35
+ s: S,
36
+ ms: MS,
37
+ Q
38
+ };
39
+ return special[u] || String(u || "").toLowerCase().replace(/s$/, "");
40
+ }
41
+ function isUndefined(value) {
42
+ return value === void 0;
43
+ }
44
+ function callDateGetOrSet(date, field, utc, value) {
45
+ if (!isUndefined(value) && field !== "Day") {
46
+ date[`set${utc ? "UTC" : ""}${field}`](...isArray(value) ? value : [value]);
47
+ return;
48
+ }
49
+ return date[`get${utc ? "UTC" : ""}${field}`]();
50
+ }
51
+
52
+ // src/core/funcs/startOf.ts
53
+ function startOf(date, unit, utc, reverse = false) {
54
+ const result = date;
55
+ const instanceFactorySet = (method, slice) => {
56
+ const argumentStart = [0, 0, 0, 0];
57
+ const argumentEnd = [23, 59, 59, 999];
58
+ const argument = reverse ? argumentEnd.slice(slice) : argumentStart.slice(slice);
59
+ callDateGetOrSet(result, method, utc, argument);
60
+ };
61
+ const instanceFactory = (d, m) => {
62
+ callDateGetOrSet(result, "Month", utc, [m, d]);
63
+ };
64
+ const $M = callDateGetOrSet(result, "Month", utc);
65
+ switch (prettyUnit(unit)) {
66
+ case Y:
67
+ reverse ? instanceFactory(31, 11) : instanceFactory(1, 0);
68
+ instanceFactorySet("Hours", 0);
69
+ break;
70
+ case M:
71
+ reverse ? instanceFactory(0, $M + 1) : instanceFactory(1, $M);
72
+ instanceFactorySet("Hours", 0);
73
+ break;
74
+ case W: {
75
+ const dayOfWeek = callDateGetOrSet(result, "Day", utc);
76
+ const diff = (dayOfWeek < 1 ? 7 : 0) + dayOfWeek - (reverse ? 6 : 0);
77
+ callDateGetOrSet(result, "Date", utc, callDateGetOrSet(result, "Date", utc) - diff);
78
+ instanceFactorySet("Hours", 0);
79
+ break;
80
+ }
81
+ case D:
82
+ case DATE:
83
+ instanceFactorySet("Hours", 0);
84
+ break;
85
+ case H:
86
+ instanceFactorySet("Minutes", 1);
87
+ break;
88
+ case MIN:
89
+ instanceFactorySet("Seconds", 2);
90
+ break;
91
+ case S:
92
+ instanceFactorySet("Milliseconds", 3);
93
+ break;
94
+ }
95
+ }
96
+
97
+ // src/core/Dayts.ts
98
+ var Dayts = class _Dayts {
99
+ $d;
100
+ $y;
101
+ $M;
102
+ $D;
103
+ $W;
104
+ $H;
105
+ $m;
106
+ $s;
107
+ $ms;
108
+ // utc mode
109
+ $u = false;
110
+ constructor(cfg) {
111
+ this.$d = cfg.d;
112
+ 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
+ }
126
+ // return utc instance
127
+ utc() {
128
+ return new _Dayts({
129
+ d: this.$d,
130
+ utc: true
131
+ });
132
+ }
133
+ // return this milliseconds
134
+ valueOf() {
135
+ return this.$d.getTime();
136
+ }
137
+ unix() {
138
+ return Math.floor(this.valueOf() / 1e3);
139
+ }
140
+ clone() {
141
+ return new _Dayts({
142
+ d: new Date(this.$d),
143
+ utc: this.$u
144
+ });
145
+ }
146
+ 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);
156
+ }
157
+ startOf(units) {
158
+ const newInst = this.clone();
159
+ startOf(newInst.$d, units, newInst.$u);
160
+ newInst.init();
161
+ return newInst;
162
+ }
163
+ endOf(units) {
164
+ const newInst = this.clone();
165
+ startOf(newInst.$d, units, newInst.$u, true);
166
+ newInst.init();
167
+ return newInst;
168
+ }
169
+ set(units, value) {
170
+ return this.clone().$set(units, value);
171
+ }
172
+ $set(units, value) {
173
+ const unit = prettyUnit(units);
174
+ const name = {
175
+ [D]: `Date`,
176
+ [DATE]: `Date`,
177
+ [M]: `Month`,
178
+ [Y]: `FullYear`,
179
+ [H]: `Hours`,
180
+ [MIN]: `Minutes`,
181
+ [S]: `Seconds`,
182
+ [MS]: `Milliseconds`
183
+ }[unit];
184
+ const arg = unit === D ? this.$D + (value - this.$W) : value;
185
+ callDateGetOrSet(this.$d, name, this.$u, arg);
186
+ return this;
187
+ }
188
+ };
189
+
190
+ // src/core/parseDate.ts
191
+ function parseDate(date) {
192
+ return new Date(date);
193
+ }
194
+
195
+ // src/core/index.ts
196
+ var dayts = (d) => {
197
+ if (d instanceof Dayts) {
198
+ return d;
199
+ } else {
200
+ return new Dayts({
201
+ d: parseDate(d),
202
+ utc: false
203
+ });
204
+ }
205
+ };
206
+ dayts.extend = (plugin, option) => {
207
+ if (!plugin.$i) {
208
+ plugin(option, Dayts, dayts);
209
+ plugin.$i = true;
210
+ }
211
+ return dayts;
212
+ };
213
+
214
+ export { Dayts, dayts };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "esday",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "A simple date library fully written in TypeScript",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "typesVersions": {},
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "dependencies": {
16
+ "radash": "^12.1.0"
17
+ },
18
+ "devDependencies": {
19
+ "@antfu/eslint-config": "^3",
20
+ "@types/node": "^22.5.2",
21
+ "@vitest/ui": "latest",
22
+ "eslint": "^9",
23
+ "tsup": "^8.2.4",
24
+ "typescript": "^5.5.4",
25
+ "vite": "latest",
26
+ "vitest": "latest"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup --dts",
30
+ "test": "vitest",
31
+ "test:ui": "vitest --ui --api 9527",
32
+ "test:run": "vitest run",
33
+ "pub": "pnpm build && pnpm publish"
34
+ }
35
+ }