valgen 6.2.0 → 6.2.2
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/constants.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +3 -3
- package/rules/type-rules/is-date.d.ts +1 -1
- package/rules/type-rules/is-date.js +12 -7
package/constants.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -65,6 +65,7 @@ declare const toBoolean: Validator<boolean | undefined, unknown, ExecutionOption
|
|
|
65
65
|
declare const toDate: Validator<Date, string | number | Date, ExecutionOptions>;
|
|
66
66
|
declare const toDateString: (input: Date | string | number, options?: ExecutionOptions & {
|
|
67
67
|
trim?: vg.isDateString.Precision;
|
|
68
|
+
separators?: boolean;
|
|
68
69
|
}, ctx?: Context) => any;
|
|
69
70
|
declare const toInteger: Validator<number, unknown, ExecutionOptions>;
|
|
70
71
|
declare const toNumber: Validator<number, unknown, ExecutionOptions>;
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valgen",
|
|
3
3
|
"description": "Fast runtime type validator, converter and io (encoding/decoding) library",
|
|
4
|
-
"version": "6.2.
|
|
4
|
+
"version": "6.2.2",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@browsery/validator": "^13.15.35",
|
|
9
|
-
"@jsopen/objects": "^2.2.
|
|
10
|
-
"date-fns": "^4.1
|
|
9
|
+
"@jsopen/objects": "^2.2.2",
|
|
10
|
+
"date-fns": "^4.2.1",
|
|
11
11
|
"reflect-metadata": "^0.2.2",
|
|
12
12
|
"ts-gems": "^3.12.0",
|
|
13
13
|
"tslib": "^2.8.1"
|
|
@@ -46,7 +46,10 @@ export function isDateString(options) {
|
|
|
46
46
|
precisionMin = (PRECISION_INDEX_KEYS[PRECISION_INDEX_VALUES.indexOf(precisionMinIdx)] || precisionMin);
|
|
47
47
|
return validator(isDateString.name, (input, context, _this) => {
|
|
48
48
|
const coerce = options?.coerce ?? context.coerce;
|
|
49
|
-
const parsed = coerceDateString(input,
|
|
49
|
+
const parsed = coerceDateString(input, {
|
|
50
|
+
trimPrecision: trim ? precisionMax : undefined,
|
|
51
|
+
separators: options?.separators,
|
|
52
|
+
});
|
|
50
53
|
if (parsed) {
|
|
51
54
|
if (parsed.precision < precisionMinIdx) {
|
|
52
55
|
context.fail(_this, `Minimum date precision should be ${precisionMin}`, input, options);
|
|
@@ -62,7 +65,9 @@ export function isDateString(options) {
|
|
|
62
65
|
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
63
66
|
const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](?:([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,6}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
64
67
|
const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,6}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
|
|
65
|
-
function coerceDateString(input,
|
|
68
|
+
function coerceDateString(input, options = {}) {
|
|
69
|
+
const { trimPrecision } = options;
|
|
70
|
+
const separators = options.separators ?? true;
|
|
66
71
|
const precisionIndex = (trimPrecision ? PRECISION_INDEX[trimPrecision] : 9) || 9;
|
|
67
72
|
let dateParts;
|
|
68
73
|
let detectedPrecision;
|
|
@@ -122,15 +127,15 @@ function coerceDateString(input, trimPrecision) {
|
|
|
122
127
|
detectedPrecision = Math.min(dateParts.length, 8);
|
|
123
128
|
let value = dateParts[0] || '0000';
|
|
124
129
|
if (precisionIndex > 1)
|
|
125
|
-
value += '-' + (dateParts[1] || '01');
|
|
130
|
+
value += (separators ? '-' : '') + (dateParts[1] || '01');
|
|
126
131
|
if (precisionIndex > 2)
|
|
127
|
-
value += '-' + (dateParts[2] || '01');
|
|
132
|
+
value += (separators ? '-' : '') + (dateParts[2] || '01');
|
|
128
133
|
if (precisionIndex > 3)
|
|
129
|
-
value += 'T' + (dateParts[3] || '00');
|
|
134
|
+
value += (separators ? 'T' : '') + (dateParts[3] || '00');
|
|
130
135
|
if (precisionIndex > 4)
|
|
131
|
-
value += ':' + (dateParts[4] || '00');
|
|
136
|
+
value += (separators ? ':' : '') + (dateParts[4] || '00');
|
|
132
137
|
if (precisionIndex > 5)
|
|
133
|
-
value += ':' + (dateParts[5] || '00');
|
|
138
|
+
value += (separators ? ':' : '') + (dateParts[5] || '00');
|
|
134
139
|
if (precisionIndex > 6)
|
|
135
140
|
value += dateParts[6] ? '.' + dateParts[6] : '';
|
|
136
141
|
if (precisionIndex > 7)
|