valgen 5.0.0 → 5.1.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,5 +1,20 @@
1
+ # v5.1.0
2
+ [2023-12-23]
3
+
4
+ ### Changes
5
+
6
+ * Moved from "dayjs" to "date-fns" ([`fd240af`](https://github.com/panates/valgen/commit/fd240afe3cdbb7485695d204aa25b9bf62ad315a))
7
+
8
+ # v5.0.1
9
+ [2023-12-23]
10
+
11
+ ### Changes
12
+
13
+ * Added new rules ([`ddcab7c`](https://github.com/panates/valgen/commit/ddcab7c54c287a9ec839c04302faa6b3bf5e554d))
14
+ * Moved all validator factories into "factories" namespace, ([`2ac5abd`](https://github.com/panates/valgen/commit/2ac5abd7f2097de4ec769bcc55034636142ee598))
15
+
1
16
  # v5.0.0
2
- [2023-12-20]
17
+ [2023-12-21]
3
18
 
4
19
  ### Changes
5
20
 
@@ -1,13 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.isDateString = exports.isDate = void 0;
7
- const dayjs_1 = __importDefault(require("dayjs"));
8
- const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
4
+ const date_fns_1 = require("date-fns");
9
5
  const index_js_1 = require("../core/index.js");
10
- dayjs_1.default.extend(customParseFormat_1.default);
11
6
  /* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
12
7
  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)?)?$/;
13
8
  /**
@@ -18,27 +13,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
18
13
  function isDate(options) {
19
14
  const precision = options?.precision;
20
15
  return (0, index_js_1.validator)('isDate', function (input, context, _this) {
21
- if (input != null && !(input instanceof Date) && context.coerce) {
16
+ let d;
17
+ if (input instanceof Date)
18
+ d = input;
19
+ else if (input != null && context.coerce) {
22
20
  if (typeof input === 'string' && context.coerce) {
23
- const d = (0, dayjs_1.default)(input);
24
- if (d.isValid())
25
- input = d.toDate();
21
+ d = (0, date_fns_1.parseISO)(input);
26
22
  }
27
23
  else if (typeof input === 'number')
28
- input = new Date(input);
24
+ d = new Date(input);
29
25
  }
30
- if (input && input instanceof Date && !isNaN(input.getTime())) {
26
+ if (d && !isNaN(d.getTime())) {
31
27
  if (precision === 'year') {
32
- input.setHours(0, 0, 0, 0);
33
- input.setMonth(0, 1);
28
+ d.setHours(0, 0, 0, 0);
29
+ d.setMonth(0, 1);
34
30
  }
35
31
  if (precision === 'month') {
36
- input.setHours(0, 0, 0, 0);
37
- input.setDate(1);
32
+ d.setHours(0, 0, 0, 0);
33
+ d.setDate(1);
38
34
  }
39
35
  if (precision === 'date')
40
- input.setHours(0, 0, 0, 0);
41
- return input;
36
+ d.setHours(0, 0, 0, 0);
37
+ return d;
42
38
  }
43
39
  context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
44
40
  }, options);
@@ -54,10 +50,10 @@ function isDateString(options) {
54
50
  const trim = options?.trim;
55
51
  return (0, index_js_1.validator)('isDateString', function (input, context, _this) {
56
52
  if (typeof input === 'string') {
57
- const d = (0, dayjs_1.default)(input);
58
- if (d.isValid()) {
59
- const m = DATE_PATTERN.exec(input);
60
- if (m) {
53
+ const m = DATE_PATTERN.exec(input);
54
+ if (m) {
55
+ const d = (0, date_fns_1.parseISO)(input);
56
+ if (d && !isNaN(d.getTime())) {
61
57
  if (!precision ||
62
58
  precision === 'year' ||
63
59
  (precision === 'month' && m[2]) ||
@@ -76,7 +72,7 @@ function isDateString(options) {
76
72
  return s;
77
73
  if (trim === 'date' || !m[4])
78
74
  return s;
79
- s += 'T' + m[4];
75
+ s += 'T' + m[4].substring(0, 8);
80
76
  if (trim === 'time')
81
77
  return s;
82
78
  if (m[9])
@@ -86,13 +82,10 @@ function isDateString(options) {
86
82
  }
87
83
  }
88
84
  }
89
- else if (input != null) {
90
- const d = (0, dayjs_1.default)(input);
91
- if (d.isValid()) {
92
- if (trim === 'date')
93
- return d.format('YYYY-MM-DD');
94
- return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
95
- }
85
+ else if (input instanceof Date) {
86
+ return trim === 'date'
87
+ ? (0, date_fns_1.formatISO)(input, { representation: 'date' })
88
+ : (0, date_fns_1.formatISO)(input).substring(0, 19);
96
89
  }
97
90
  context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
98
91
  }, options);
@@ -1,7 +1,5 @@
1
- import dayjs from 'dayjs';
2
- import customParseFormat from 'dayjs/plugin/customParseFormat';
1
+ import { formatISO, parseISO } from 'date-fns';
3
2
  import { validator } from '../core/index.js';
4
- dayjs.extend(customParseFormat);
5
3
  /* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
6
4
  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)?)?$/;
7
5
  /**
@@ -12,27 +10,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
12
10
  export function isDate(options) {
13
11
  const precision = options?.precision;
14
12
  return validator('isDate', function (input, context, _this) {
15
- if (input != null && !(input instanceof Date) && context.coerce) {
13
+ let d;
14
+ if (input instanceof Date)
15
+ d = input;
16
+ else if (input != null && context.coerce) {
16
17
  if (typeof input === 'string' && context.coerce) {
17
- const d = dayjs(input);
18
- if (d.isValid())
19
- input = d.toDate();
18
+ d = parseISO(input);
20
19
  }
21
20
  else if (typeof input === 'number')
22
- input = new Date(input);
21
+ d = new Date(input);
23
22
  }
24
- if (input && input instanceof Date && !isNaN(input.getTime())) {
23
+ if (d && !isNaN(d.getTime())) {
25
24
  if (precision === 'year') {
26
- input.setHours(0, 0, 0, 0);
27
- input.setMonth(0, 1);
25
+ d.setHours(0, 0, 0, 0);
26
+ d.setMonth(0, 1);
28
27
  }
29
28
  if (precision === 'month') {
30
- input.setHours(0, 0, 0, 0);
31
- input.setDate(1);
29
+ d.setHours(0, 0, 0, 0);
30
+ d.setDate(1);
32
31
  }
33
32
  if (precision === 'date')
34
- input.setHours(0, 0, 0, 0);
35
- return input;
33
+ d.setHours(0, 0, 0, 0);
34
+ return d;
36
35
  }
37
36
  context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
38
37
  }, options);
@@ -47,10 +46,10 @@ export function isDateString(options) {
47
46
  const trim = options?.trim;
48
47
  return validator('isDateString', function (input, context, _this) {
49
48
  if (typeof input === 'string') {
50
- const d = dayjs(input);
51
- if (d.isValid()) {
52
- const m = DATE_PATTERN.exec(input);
53
- if (m) {
49
+ const m = DATE_PATTERN.exec(input);
50
+ if (m) {
51
+ const d = parseISO(input);
52
+ if (d && !isNaN(d.getTime())) {
54
53
  if (!precision ||
55
54
  precision === 'year' ||
56
55
  (precision === 'month' && m[2]) ||
@@ -69,7 +68,7 @@ export function isDateString(options) {
69
68
  return s;
70
69
  if (trim === 'date' || !m[4])
71
70
  return s;
72
- s += 'T' + m[4];
71
+ s += 'T' + m[4].substring(0, 8);
73
72
  if (trim === 'time')
74
73
  return s;
75
74
  if (m[9])
@@ -79,13 +78,10 @@ export function isDateString(options) {
79
78
  }
80
79
  }
81
80
  }
82
- else if (input != null) {
83
- const d = dayjs(input);
84
- if (d.isValid()) {
85
- if (trim === 'date')
86
- return d.format('YYYY-MM-DD');
87
- return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
88
- }
81
+ else if (input instanceof Date) {
82
+ return trim === 'date'
83
+ ? formatISO(input, { representation: 'date' })
84
+ : formatISO(input).substring(0, 19);
89
85
  }
90
86
  context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
91
87
  }, options);
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.0.0",
4
+ "version": "5.1.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@types/validator": "^13.11.7",
16
- "dayjs": "^2.0.0-alpha.4",
16
+ "date-fns": "^3.0.6",
17
17
  "ts-gems": "^3.1.0",
18
18
  "validator": "^13.11.0"
19
19
  },