valgen 5.17.3 → 5.18.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  ## Changelog
2
2
 
3
- ### [v5.17.3](https://github.com/panates/valgen/compare/v5.17.2...v5.17.3) -
3
+ ### [v5.18.0](https://github.com/panates/valgen/compare/v5.17.3...v5.18.0) -
4
+
5
+ #### 🚀 New Features
6
+
7
+ - feat: isTime accept Date instance @Eray Hanoğlu
8
+
9
+ #### 🪲 Fixes
10
+
11
+ - fix: Minor fix in isDate rule @Eray Hanoğlu
12
+
13
+ ### [v5.17.3](https://github.com/panates/valgen/compare/v5.17.2...v5.17.3) - 30 July 2025
4
14
 
5
15
  #### 🚀 New Features
6
16
 
@@ -1,19 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isTime = isTime;
4
- const tslib_1 = require("tslib");
5
- const validator_1 = tslib_1.__importDefault(require("@browsery/validator"));
6
4
  const index_js_1 = require("../../core/index.js");
5
+ const TIME_PATTERN = /^(\d{2}):?(\d{2})(?::?(\d{2})?(?:\.(\d{1,3}))?)?$/;
7
6
  /**
8
7
  * Validates if value is a time formatted string
9
8
  * @validator isTime
10
9
  */
11
10
  function isTime(options) {
12
- return (0, index_js_1.validator)('isURL', (input, context, _this) => {
13
- if (input != null &&
14
- typeof input === 'string' &&
15
- validator_1.default.isTime(input, options)) {
16
- return input;
11
+ return (0, index_js_1.validator)('isTime', (input, context, _this) => {
12
+ if (input != null) {
13
+ const coerce = options?.coerce ?? context.coerce;
14
+ let str;
15
+ if (input instanceof Date && coerce) {
16
+ str =
17
+ String(input.getHours()).padStart(2, '0') +
18
+ ':' +
19
+ String(input.getMinutes()).padStart(2, '0') +
20
+ ':' +
21
+ String(input.getSeconds()).padStart(2, '0');
22
+ if (input.getMilliseconds()) {
23
+ str += '.' + String(input.getMilliseconds()).padStart(3, '0');
24
+ }
25
+ return str;
26
+ }
27
+ else
28
+ str = input;
29
+ if (str && typeof str === 'string') {
30
+ const m = TIME_PATTERN.exec(str);
31
+ if (m) {
32
+ if (parseInt(m[1]) <= 23 &&
33
+ parseInt(m[2]) <= 59 &&
34
+ (!m[3] || parseInt(m[3]) <= 59)) {
35
+ if (!coerce)
36
+ return str;
37
+ let out = m[1];
38
+ if (m[2])
39
+ out += ':' + m[2];
40
+ if (m[3])
41
+ out += ':' + m[3];
42
+ if (m[4])
43
+ out += '.' + m[4];
44
+ return out;
45
+ }
46
+ }
47
+ }
17
48
  }
18
49
  context.fail(_this, `Value must be a valid Time`, input);
19
50
  }, options);
@@ -65,7 +65,7 @@ function isDateString(options) {
65
65
  }
66
66
  // noinspection RegExpUnnecessaryNonCapturingGroup
67
67
  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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
68
- const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?$/;
68
+ const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
69
69
  function coerceDateString(input, precision) {
70
70
  let dateParts;
71
71
  let outPrecision = 0;
@@ -100,6 +100,7 @@ function coerceDateString(input, precision) {
100
100
  m[1].substring(10, 12),
101
101
  m[1].substring(12, 14),
102
102
  m[2],
103
+ m[3] ? m[3] + m[4] + (m[5] ? ':' + m[5] : '') : '',
103
104
  ];
104
105
  }
105
106
  }
@@ -108,7 +109,7 @@ function coerceDateString(input, precision) {
108
109
  return;
109
110
  outPrecision = dateParts.findIndex(v => !v);
110
111
  if (outPrecision === -1)
111
- outPrecision = dateParts.length + 1;
112
+ outPrecision = dateParts.length;
112
113
  const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
113
114
  let value = dateParts[0] || '0000';
114
115
  if (precisionIndex > 1)
@@ -121,7 +122,7 @@ function coerceDateString(input, precision) {
121
122
  value += ':' + (dateParts[4] || '00');
122
123
  if (precisionIndex > 5)
123
124
  value += ':' + (dateParts[5] || '00');
124
- if (precisionIndex > 6)
125
+ if (precisionIndex > 5)
125
126
  value += dateParts[6] ? '.' + dateParts[6] : '';
126
127
  if (precisionIndex > 7)
127
128
  value += dateParts[7] || '';
@@ -1,15 +1,47 @@
1
- import validatorJS from '@browsery/validator';
2
1
  import { validator, } from '../../core/index.js';
2
+ const TIME_PATTERN = /^(\d{2}):?(\d{2})(?::?(\d{2})?(?:\.(\d{1,3}))?)?$/;
3
3
  /**
4
4
  * Validates if value is a time formatted string
5
5
  * @validator isTime
6
6
  */
7
7
  export function isTime(options) {
8
- return validator('isURL', (input, context, _this) => {
9
- if (input != null &&
10
- typeof input === 'string' &&
11
- validatorJS.isTime(input, options)) {
12
- return input;
8
+ return validator('isTime', (input, context, _this) => {
9
+ if (input != null) {
10
+ const coerce = options?.coerce ?? context.coerce;
11
+ let str;
12
+ if (input instanceof Date && coerce) {
13
+ str =
14
+ String(input.getHours()).padStart(2, '0') +
15
+ ':' +
16
+ String(input.getMinutes()).padStart(2, '0') +
17
+ ':' +
18
+ String(input.getSeconds()).padStart(2, '0');
19
+ if (input.getMilliseconds()) {
20
+ str += '.' + String(input.getMilliseconds()).padStart(3, '0');
21
+ }
22
+ return str;
23
+ }
24
+ else
25
+ str = input;
26
+ if (str && typeof str === 'string') {
27
+ const m = TIME_PATTERN.exec(str);
28
+ if (m) {
29
+ if (parseInt(m[1]) <= 23 &&
30
+ parseInt(m[2]) <= 59 &&
31
+ (!m[3] || parseInt(m[3]) <= 59)) {
32
+ if (!coerce)
33
+ return str;
34
+ let out = m[1];
35
+ if (m[2])
36
+ out += ':' + m[2];
37
+ if (m[3])
38
+ out += ':' + m[3];
39
+ if (m[4])
40
+ out += '.' + m[4];
41
+ return out;
42
+ }
43
+ }
44
+ }
13
45
  }
14
46
  context.fail(_this, `Value must be a valid Time`, input);
15
47
  }, options);
@@ -60,7 +60,7 @@ export function isDateString(options) {
60
60
  }
61
61
  // noinspection RegExpUnnecessaryNonCapturingGroup
62
62
  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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
63
- const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?$/;
63
+ const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
64
64
  function coerceDateString(input, precision) {
65
65
  let dateParts;
66
66
  let outPrecision = 0;
@@ -95,6 +95,7 @@ function coerceDateString(input, precision) {
95
95
  m[1].substring(10, 12),
96
96
  m[1].substring(12, 14),
97
97
  m[2],
98
+ m[3] ? m[3] + m[4] + (m[5] ? ':' + m[5] : '') : '',
98
99
  ];
99
100
  }
100
101
  }
@@ -103,7 +104,7 @@ function coerceDateString(input, precision) {
103
104
  return;
104
105
  outPrecision = dateParts.findIndex(v => !v);
105
106
  if (outPrecision === -1)
106
- outPrecision = dateParts.length + 1;
107
+ outPrecision = dateParts.length;
107
108
  const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
108
109
  let value = dateParts[0] || '0000';
109
110
  if (precisionIndex > 1)
@@ -116,7 +117,7 @@ function coerceDateString(input, precision) {
116
117
  value += ':' + (dateParts[4] || '00');
117
118
  if (precisionIndex > 5)
118
119
  value += ':' + (dateParts[5] || '00');
119
- if (precisionIndex > 6)
120
+ if (precisionIndex > 5)
120
121
  value += dateParts[6] ? '.' + dateParts[6] : '';
121
122
  if (precisionIndex > 7)
122
123
  value += dateParts[7] || '';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valgen",
3
3
  "description": "Fast runtime type validator, converter and io (encoding/decoding) library",
4
- "version": "5.17.3",
4
+ "version": "5.18.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/types/index.d.cts CHANGED
@@ -43,7 +43,7 @@ declare const isObjectId: import("./core/validator.js").Validator<string | vg.Ob
43
43
  declare const isPort: import("./core/validator.js").Validator<number, string | number, import("./core/types.js").ExecutionOptions>;
44
44
  declare const isString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
45
45
  declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
- declare const isTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
+ declare const isTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
47
47
  declare const isUndefined: import("./core/validator.js").Validator<any, unknown, import("./core/types.js").ExecutionOptions>;
48
48
  declare const isUppercase: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
49
49
  declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
@@ -61,5 +61,5 @@ declare const toDateString: import("./core/validator.js").Validator<string, stri
61
61
  declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
62
62
  declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
63
63
  declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
64
- declare const toTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
64
+ declare const toTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
65
65
  export { isAlpha, isAlphanumeric, isAny, isArray, isAscii, isBase64, isBigint, isBoolean, isBtcAddress, isCreditCard, isDate, isDateString, isDecimal, isDefined, isEAN, isEmail, isEmpty, isETHAddress, isFQDN, isHex, isHexColor, isIBAN, isInteger, isIP, isIPRange, isISSN, isJWT, isLowercase, isMACAddress, isMobilePhone, isNotEmpty, isNotNull, isNotNullish, isNull, isNullish, isNumber, isObject, isObjectId, isPort, isString, isSWIFT, isTime, isUndefined, isUppercase, isURL, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, toArray, toBigint, toBoolean, toDate, toDateString, toInteger, toNumber, toString, toTime, vg, };
package/types/index.d.ts CHANGED
@@ -43,7 +43,7 @@ declare const isObjectId: import("./core/validator.js").Validator<string | vg.Ob
43
43
  declare const isPort: import("./core/validator.js").Validator<number, string | number, import("./core/types.js").ExecutionOptions>;
44
44
  declare const isString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
45
45
  declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
- declare const isTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
+ declare const isTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
47
47
  declare const isUndefined: import("./core/validator.js").Validator<any, unknown, import("./core/types.js").ExecutionOptions>;
48
48
  declare const isUppercase: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
49
49
  declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
@@ -61,5 +61,5 @@ declare const toDateString: import("./core/validator.js").Validator<string, stri
61
61
  declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
62
62
  declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
63
63
  declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
64
- declare const toTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
64
+ declare const toTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
65
65
  export { isAlpha, isAlphanumeric, isAny, isArray, isAscii, isBase64, isBigint, isBoolean, isBtcAddress, isCreditCard, isDate, isDateString, isDecimal, isDefined, isEAN, isEmail, isEmpty, isETHAddress, isFQDN, isHex, isHexColor, isIBAN, isInteger, isIP, isIPRange, isISSN, isJWT, isLowercase, isMACAddress, isMobilePhone, isNotEmpty, isNotNull, isNotNullish, isNull, isNullish, isNumber, isObject, isObjectId, isPort, isString, isSWIFT, isTime, isUndefined, isUppercase, isURL, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, toArray, toBigint, toBoolean, toDate, toDateString, toInteger, toNumber, toString, toTime, vg, };
@@ -1,9 +1,8 @@
1
- import validatorJS from '@browsery/validator';
2
1
  import { type ValidationOptions } from '../../core/index.js';
3
- export interface IsTimeOptions extends ValidationOptions, validatorJS.IsTimeOptions {
2
+ export interface IsTimeOptions extends ValidationOptions {
4
3
  }
5
4
  /**
6
5
  * Validates if value is a time formatted string
7
6
  * @validator isTime
8
7
  */
9
- export declare function isTime(options?: IsTimeOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
8
+ export declare function isTime(options?: IsTimeOptions): import("../../core/validator.js").Validator<string, string | Date, import("../../core/types.js").ExecutionOptions>;