@weser/dates 0.0.18 → 1.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 +1 -1
- package/dist/src/format.d.ts +6 -0
- package/dist/src/format.js +89 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/vitest.config.d.ts +2 -0
- package/dist/vitest.config.js +10 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const PATTERN_REGEX = /(M|y|d|D|h|H|m|s|S|G|Z|P|a)+/g;
|
|
2
|
+
const ESCAPE_REGEX = /\\"|"((?:\\"|[^"])*)"|(\+)/g;
|
|
3
|
+
const optionNames = {
|
|
4
|
+
y: 'year',
|
|
5
|
+
M: 'month',
|
|
6
|
+
d: 'day',
|
|
7
|
+
D: 'weekday',
|
|
8
|
+
S: 'fractionalSecondDigits',
|
|
9
|
+
G: 'era',
|
|
10
|
+
Z: 'timeZoneName',
|
|
11
|
+
P: 'dayPeriod',
|
|
12
|
+
a: 'hour12',
|
|
13
|
+
h: 'hour',
|
|
14
|
+
H: 'hour',
|
|
15
|
+
m: 'minute',
|
|
16
|
+
s: 'second',
|
|
17
|
+
};
|
|
18
|
+
const values = {
|
|
19
|
+
y: ['numeric', '2-digit', undefined, 'numeric'],
|
|
20
|
+
M: ['narrow', '2-digit', 'short', 'long'],
|
|
21
|
+
d: ['numeric', '2-digit'],
|
|
22
|
+
D: ['narrow', 'short', 'long'],
|
|
23
|
+
S: [1, 2, 3],
|
|
24
|
+
G: ['narrow', 'short', 'long'],
|
|
25
|
+
Z: ['short', 'long'],
|
|
26
|
+
P: ['narrow', 'short', 'long'],
|
|
27
|
+
a: [true],
|
|
28
|
+
h: ['numeric', '2-digit'],
|
|
29
|
+
H: ['numeric', '2-digit'],
|
|
30
|
+
m: ['numeric', '2-digit'],
|
|
31
|
+
s: ['numeric', '2-digit'],
|
|
32
|
+
};
|
|
33
|
+
export default function format(date, pattern, config = {}) {
|
|
34
|
+
return pattern
|
|
35
|
+
.split(ESCAPE_REGEX)
|
|
36
|
+
.filter((sub) => sub !== undefined)
|
|
37
|
+
.map((sub, index) => {
|
|
38
|
+
// keep escaped strings as is
|
|
39
|
+
if (index % 2 !== 0) {
|
|
40
|
+
return sub;
|
|
41
|
+
}
|
|
42
|
+
return sub.replace(PATTERN_REGEX, (match) => {
|
|
43
|
+
const type = match.charAt(0);
|
|
44
|
+
return String(formatType(date, type, match.length, config) || match);
|
|
45
|
+
});
|
|
46
|
+
})
|
|
47
|
+
.join('');
|
|
48
|
+
}
|
|
49
|
+
function formatType(date, type, length, { locale, timeZone } = {}) {
|
|
50
|
+
const option = optionNames[type];
|
|
51
|
+
const value = values[type][length - 1];
|
|
52
|
+
if (!value) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const options = {
|
|
56
|
+
[option]: value,
|
|
57
|
+
timeZone,
|
|
58
|
+
};
|
|
59
|
+
if (type === 'a') {
|
|
60
|
+
return Intl.DateTimeFormat(locale, {
|
|
61
|
+
...options,
|
|
62
|
+
hour: 'numeric',
|
|
63
|
+
})
|
|
64
|
+
.formatToParts(date)
|
|
65
|
+
.pop()?.value;
|
|
66
|
+
}
|
|
67
|
+
if (type === 'G' || type === 'Z') {
|
|
68
|
+
return Intl.DateTimeFormat(locale, options).formatToParts(date).pop()?.value;
|
|
69
|
+
}
|
|
70
|
+
if (type === 'H' || type === 'h') {
|
|
71
|
+
return Intl.DateTimeFormat('en-GB', {
|
|
72
|
+
...options,
|
|
73
|
+
hourCycle: type === 'H' ? 'h23' : 'h11',
|
|
74
|
+
})
|
|
75
|
+
.format(date)
|
|
76
|
+
.toLocaleLowerCase()
|
|
77
|
+
.replace(' am', '')
|
|
78
|
+
.replace(' pm', '');
|
|
79
|
+
}
|
|
80
|
+
return padIf(['m', 's'].includes(type) && value === '2-digit', Intl.DateTimeFormat(locale, options).format(date), 2);
|
|
81
|
+
}
|
|
82
|
+
function padIf(condition, value, length) {
|
|
83
|
+
if (typeof value === 'string') {
|
|
84
|
+
return condition && length === 2 && parseInt(value) / 10 < 1
|
|
85
|
+
? '0' + value
|
|
86
|
+
: value;
|
|
87
|
+
}
|
|
88
|
+
return condition && length === 2 && value / 10 < 1 ? '0' + value : value;
|
|
89
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as format } from './format.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as format } from './format.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/format.ts","../src/index.ts"],"version":"5.9.
|
|
1
|
+
{"root":["../vitest.config.ts","../src/format.ts","../src/index.ts"],"version":"5.9.3"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/dates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Tiny date formatting library with built-in i18n",
|
|
5
5
|
"author": "Robin Weser <robin@weser.io>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"clean": "rimraf dist",
|
|
34
34
|
"build": "tsc -b",
|
|
35
35
|
"dev": "pnpm build -w",
|
|
36
|
-
"test": "
|
|
36
|
+
"test": "vitest run"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"date",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"format"
|
|
44
44
|
],
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"ava": "^6.1.3",
|
|
47
46
|
"rimraf": "^3.0.2",
|
|
48
|
-
"typescript": "^5.4.5"
|
|
47
|
+
"typescript": "^5.4.5",
|
|
48
|
+
"vitest": "^2.1.8"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "490b5b37e8da7f0cac2880543874914c6f6be5d6"
|
|
51
51
|
}
|