@wisemen/wise-date 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.
- package/README.md +302 -0
- package/dist/future-infinity-date.d.ts +31 -0
- package/dist/future-infinity-date.js +87 -0
- package/dist/future-infinity-date.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/init-dayjs.d.ts +1 -0
- package/dist/init-dayjs.js +24 -0
- package/dist/init-dayjs.js.map +1 -0
- package/dist/invalid-date.d.ts +5 -0
- package/dist/invalid-date.js +8 -0
- package/dist/invalid-date.js.map +1 -0
- package/dist/month.d.ts +15 -0
- package/dist/month.js +21 -0
- package/dist/month.js.map +1 -0
- package/dist/past-infinity-date.d.ts +31 -0
- package/dist/past-infinity-date.js +87 -0
- package/dist/past-infinity-date.js.map +1 -0
- package/dist/plain-date-object.d.ts +6 -0
- package/dist/plain-date-object.js +2 -0
- package/dist/plain-date-object.js.map +1 -0
- package/dist/tests/wise-date.constructor.unit.test.d.ts +1 -0
- package/dist/tests/wise-date.constructor.unit.test.js +31 -0
- package/dist/tests/wise-date.constructor.unit.test.js.map +1 -0
- package/dist/tests/wise-date.manipulation.unit.test.d.ts +1 -0
- package/dist/tests/wise-date.manipulation.unit.test.js +92 -0
- package/dist/tests/wise-date.manipulation.unit.test.js.map +1 -0
- package/dist/tests/wise-date.ordering.unit.test.d.ts +1 -0
- package/dist/tests/wise-date.ordering.unit.test.js +174 -0
- package/dist/tests/wise-date.ordering.unit.test.js.map +1 -0
- package/dist/units.d.ts +4 -0
- package/dist/units.js +2 -0
- package/dist/units.js.map +1 -0
- package/dist/wise-date.column.d.ts +3 -0
- package/dist/wise-date.column.js +31 -0
- package/dist/wise-date.column.js.map +1 -0
- package/dist/wise-date.d.ts +52 -0
- package/dist/wise-date.factory.d.ts +4 -0
- package/dist/wise-date.factory.js +14 -0
- package/dist/wise-date.factory.js.map +1 -0
- package/dist/wise-date.js +156 -0
- package/dist/wise-date.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
3
|
+
import { InvalidDate } from './invalid-date.js';
|
|
4
|
+
dayjs.extend(customParseFormat);
|
|
5
|
+
export class WiseDate {
|
|
6
|
+
static today() {
|
|
7
|
+
return new WiseDate();
|
|
8
|
+
}
|
|
9
|
+
static tomorrow() {
|
|
10
|
+
return new WiseDate(dayjs().add(1, 'day'));
|
|
11
|
+
}
|
|
12
|
+
static yesterday() {
|
|
13
|
+
return new WiseDate(dayjs().subtract(1, 'day'));
|
|
14
|
+
}
|
|
15
|
+
static max(date, otherDate) {
|
|
16
|
+
return date.isAfter(otherDate) ? date : otherDate;
|
|
17
|
+
}
|
|
18
|
+
static min(date, otherDate) {
|
|
19
|
+
return date.isBefore(otherDate) ? date : otherDate;
|
|
20
|
+
}
|
|
21
|
+
date;
|
|
22
|
+
constructor(input, format = 'YYYY-MM-DD') {
|
|
23
|
+
if (input === undefined) {
|
|
24
|
+
this.date = dayjs();
|
|
25
|
+
}
|
|
26
|
+
else if (input instanceof WiseDate) {
|
|
27
|
+
this.date = dayjs(input.date);
|
|
28
|
+
}
|
|
29
|
+
else if (input instanceof Date) {
|
|
30
|
+
this.date = dayjs(input);
|
|
31
|
+
}
|
|
32
|
+
else if (dayjs.isDayjs(input)) {
|
|
33
|
+
this.date = input.clone();
|
|
34
|
+
}
|
|
35
|
+
else if (typeof input === 'string') {
|
|
36
|
+
this.date = dayjs(input, format, true);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.date = dayjs(`${input.year}-${input.month}-${input.day}`, 'YYYY-M-D', true);
|
|
40
|
+
}
|
|
41
|
+
if (!this.date.isValid()) {
|
|
42
|
+
throw new InvalidDate(this);
|
|
43
|
+
}
|
|
44
|
+
this.date = this.date.startOf('day');
|
|
45
|
+
}
|
|
46
|
+
isSame(otherDate, unit = 'day') {
|
|
47
|
+
return this.date.isSame(otherDate.date, unit);
|
|
48
|
+
}
|
|
49
|
+
isAfter(otherDate, unit = 'day') {
|
|
50
|
+
if (otherDate.isFutureInfinity()) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
else if (otherDate.isPastInfinity()) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return this.date.isAfter(otherDate.date, unit);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
isSameOrAfter(otherDate, unit = 'day') {
|
|
61
|
+
return this.isAfter(otherDate, unit) || this.isSame(otherDate, unit);
|
|
62
|
+
}
|
|
63
|
+
isBefore(otherDate, unit) {
|
|
64
|
+
if (otherDate.isPastInfinity()) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
else if (otherDate.isFutureInfinity()) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return this.date.isBefore(otherDate.date, unit);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
isSameOrBefore(otherDate, unit = 'day') {
|
|
75
|
+
return this.isBefore(otherDate, unit) || this.isSame(otherDate, unit);
|
|
76
|
+
}
|
|
77
|
+
startOf(unit) {
|
|
78
|
+
return new WiseDate(this.date.startOf(unit));
|
|
79
|
+
}
|
|
80
|
+
endOf(unit) {
|
|
81
|
+
return new WiseDate(this.date.endOf(unit));
|
|
82
|
+
}
|
|
83
|
+
get year() {
|
|
84
|
+
return this.date.get('year');
|
|
85
|
+
}
|
|
86
|
+
get month() {
|
|
87
|
+
return this.date.get('month') + 1;
|
|
88
|
+
}
|
|
89
|
+
get dayOfMonth() {
|
|
90
|
+
return this.date.get('day');
|
|
91
|
+
}
|
|
92
|
+
get weekOfYear() {
|
|
93
|
+
return this.date.week();
|
|
94
|
+
}
|
|
95
|
+
get dayOfYear() {
|
|
96
|
+
return this.date.dayOfYear();
|
|
97
|
+
}
|
|
98
|
+
isToday() {
|
|
99
|
+
return this.date.isToday();
|
|
100
|
+
}
|
|
101
|
+
isTomorrow() {
|
|
102
|
+
return this.date.isTomorrow();
|
|
103
|
+
}
|
|
104
|
+
isYesterday() {
|
|
105
|
+
return this.date.isYesterday();
|
|
106
|
+
}
|
|
107
|
+
add(amount, unit) {
|
|
108
|
+
return new WiseDate(this.date.add(amount, unit));
|
|
109
|
+
}
|
|
110
|
+
subtract(amount, unit) {
|
|
111
|
+
return new WiseDate(this.date.subtract(amount, unit));
|
|
112
|
+
}
|
|
113
|
+
diff(withOther, unit, precise = false) {
|
|
114
|
+
if (withOther.isFutureInfinity() || withOther.isPastInfinity()) {
|
|
115
|
+
return Infinity;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return this.date.diff(withOther.date, unit, precise);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @see https://day.js.org/docs/en/display/format
|
|
123
|
+
*/
|
|
124
|
+
format(template) {
|
|
125
|
+
return this.date.format(template);
|
|
126
|
+
}
|
|
127
|
+
clone() {
|
|
128
|
+
return new WiseDate(this.date);
|
|
129
|
+
}
|
|
130
|
+
toPlainObject() {
|
|
131
|
+
return {
|
|
132
|
+
year: this.year,
|
|
133
|
+
month: this.month,
|
|
134
|
+
day: this.dayOfMonth
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
toDate() {
|
|
138
|
+
return this.date.toDate();
|
|
139
|
+
}
|
|
140
|
+
toString() {
|
|
141
|
+
return this.format('YYYY-MM-DD');
|
|
142
|
+
}
|
|
143
|
+
isFutureInfinity() {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
isPastInfinity() {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
isInfinity() {
|
|
150
|
+
return this.isFutureInfinity() || this.isPastInfinity();
|
|
151
|
+
}
|
|
152
|
+
valueOf() {
|
|
153
|
+
return this.toString();
|
|
154
|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IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IACnC,CAAC;IAEM,KAAK;QACV,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAEM,aAAa;QAClB,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,GAAG,EAAE,IAAI,CAAC,UAAU;SACrB,CAAA;IACH,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAA;IAC3B,CAAC;IAEM,QAAQ;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;IAClC,CAAC;IAEM,gBAAgB;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAEM,cAAc;QACnB,OAAO,KAAK,CAAA;IACd,CAAC;IAEM,UAAU;QACf,OAAO,IAAI,CAAC,gBAAgB,EAAE,IAAI,IAAI,CAAC,cAAc,EAAE,CAAA;IACzD,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAA;IACxB,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wisemen/wise-date",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prebuild": "rm -rf ./dist",
|
|
12
|
+
"clean": "rm -rf ./dist",
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"pretest": "npm run clean && npm run build",
|
|
15
|
+
"test": "node --test",
|
|
16
|
+
"lint": "eslint --cache",
|
|
17
|
+
"prerelease": "npm run build",
|
|
18
|
+
"release": "pnpx release-it"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"dayjs": "^1.11.13",
|
|
22
|
+
"typeorm": "^0.3.22"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.14.0",
|
|
26
|
+
"expect": "^29.7.0",
|
|
27
|
+
"typescript": "^5.8.3",
|
|
28
|
+
"@stylistic/eslint-plugin": "^4.2.0",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^8.29.1",
|
|
30
|
+
"@wisemen/eslint-config-nestjs": "^0.1.7",
|
|
31
|
+
"eslint": "^9.24.0"
|
|
32
|
+
},
|
|
33
|
+
"author": "Kobe Kwanten",
|
|
34
|
+
"contributors": [],
|
|
35
|
+
"license": "GPL",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git@github.com:wisemen-digital/node-core.git"
|
|
39
|
+
}
|
|
40
|
+
}
|