n4s 5.0.1 → 5.0.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/CHANGELOG.md +0 -4
- package/README.md +2 -0
- package/compose/package.json +2 -0
- package/compounds/package.json +2 -0
- package/date/package.json +9 -0
- package/dist/cjs/compose.development.js +14 -97
- package/dist/cjs/compose.js +0 -1
- package/dist/cjs/compose.production.js +1 -1
- package/dist/cjs/compounds.development.js +30 -86
- package/dist/cjs/compounds.js +0 -1
- package/dist/cjs/compounds.production.js +1 -1
- package/dist/cjs/date.development.js +186 -0
- package/dist/cjs/date.js +6 -0
- package/dist/cjs/date.production.js +1 -0
- package/dist/cjs/email.development.js +363 -0
- package/dist/cjs/email.js +6 -0
- package/dist/cjs/email.production.js +1 -0
- package/dist/cjs/isURL.development.js +353 -0
- package/dist/cjs/isURL.js +6 -0
- package/dist/cjs/isURL.production.js +1 -0
- package/dist/cjs/n4s.development.js +205 -387
- package/dist/cjs/n4s.js +0 -1
- package/dist/cjs/n4s.production.js +1 -1
- package/dist/cjs/schema.development.js +19 -80
- package/dist/cjs/schema.js +0 -1
- package/dist/cjs/schema.production.js +1 -1
- package/dist/es/compose.development.js +14 -97
- package/dist/es/compose.production.js +1 -1
- package/dist/es/compounds.development.js +28 -84
- package/dist/es/compounds.production.js +1 -1
- package/dist/es/date.development.js +184 -0
- package/dist/es/date.production.js +1 -0
- package/dist/es/email.development.js +361 -0
- package/dist/es/email.production.js +1 -0
- package/dist/es/isURL.development.js +351 -0
- package/dist/es/isURL.production.js +1 -0
- package/dist/es/n4s.development.js +192 -372
- package/dist/es/n4s.production.js +1 -1
- package/dist/es/schema.development.js +16 -75
- package/dist/es/schema.production.js +1 -1
- package/dist/umd/compose.development.js +18 -102
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +34 -91
- package/dist/umd/compounds.production.js +1 -1
- package/dist/umd/date.development.js +190 -0
- package/dist/umd/date.production.js +1 -0
- package/dist/umd/email.development.js +367 -0
- package/dist/umd/email.production.js +1 -0
- package/dist/umd/isURL.development.js +357 -0
- package/dist/umd/isURL.production.js +1 -0
- package/dist/umd/n4s.development.js +209 -392
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +23 -85
- package/dist/umd/schema.production.js +1 -1
- package/email/package.json +9 -0
- package/isURL/package.json +9 -0
- package/package.json +162 -55
- package/schema/package.json +2 -0
- package/testUtils/TEnforceMock.ts +3 -0
- package/types/compose.d.ts +46 -55
- package/types/compose.d.ts.map +1 -0
- package/types/compounds.d.ts +50 -62
- package/types/compounds.d.ts.map +1 -0
- package/types/date.d.ts +18 -0
- package/types/date.d.ts.map +1 -0
- package/types/email.d.ts +12 -0
- package/types/email.d.ts.map +1 -0
- package/types/isURL.d.ts +12 -0
- package/types/isURL.d.ts.map +1 -0
- package/types/n4s.d.ts +62 -73
- package/types/n4s.d.ts.map +1 -0
- package/types/schema.d.ts +55 -63
- package/types/schema.d.ts.map +1 -0
- package/docs/.nojekyll +0 -0
- package/docs/README.md +0 -44
- package/docs/_sidebar.md +0 -5
- package/docs/external.md +0 -27
- package/docs/index.html +0 -32
- package/docs/rules.md +0 -1282
- package/tsconfig.json +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -4,10 +4,6 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
-
## 5.0.1 - 2021-12-24
|
|
8
|
-
### Fixed and improved
|
|
9
|
-
- 7e8d48d Update changelog (ealush)
|
|
10
|
-
|
|
11
7
|
## 4.0.0 2021-12-24
|
|
12
8
|
|
|
13
9
|
- 7daf6d2 rule(n4s): add isNullish rule, improve isBlank (ealush)
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Enforce - n4s
|
|
2
2
|
|
|
3
|
+
[](https://discord.gg/WmADZpJnSe) [](https://www.npmjs.com/package/n4s) [](https://www.npmjs.com/package/n4s) [](https://bundlephobia.com/package/n4s) [](https://github.com/ealush/vest/actions)
|
|
4
|
+
|
|
3
5
|
Enforce is a validations assertions library. It provides rules that you can test your data against.
|
|
4
6
|
|
|
5
7
|
By default, enforce throws an error when your validations fail. These errors should be caught by a validation testing framework such as [Vest](https://github.com/ealush/vest).
|
package/compose/package.json
CHANGED
package/compounds/package.json
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"main": "../dist/cjs/compounds.js",
|
|
3
3
|
"module": "../dist/es/compounds.production.js",
|
|
4
|
+
"unpkg": "../dist/umd/compounds.production.js",
|
|
5
|
+
"jsdelivr": "../dist/umd/compounds.production.js",
|
|
4
6
|
"name": "compounds",
|
|
5
7
|
"types": "../types/compounds.d.ts",
|
|
6
8
|
"private": true
|
|
@@ -1,79 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var n4s = require('n4s');
|
|
4
|
-
|
|
5
|
-
function mapFirst(array, callback) {
|
|
6
|
-
var broke = false;
|
|
7
|
-
var breakoutValue = null;
|
|
8
|
-
for (var i = 0; i < array.length; i++) {
|
|
9
|
-
callback(array[i], breakout, i);
|
|
10
|
-
if (broke) {
|
|
11
|
-
return breakoutValue;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function breakout(value) {
|
|
15
|
-
broke = true;
|
|
16
|
-
breakoutValue = value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function isFunction(value) {
|
|
21
|
-
return typeof value === 'function';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function optionalFunctionValue(value) {
|
|
25
|
-
var args = [];
|
|
26
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
27
|
-
args[_i - 1] = arguments[_i];
|
|
28
|
-
}
|
|
29
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function defaultTo(callback, defaultValue) {
|
|
33
|
-
var _a;
|
|
34
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Throws a timed out error.
|
|
39
|
-
*/
|
|
40
|
-
function throwError(devMessage, productionMessage) {
|
|
41
|
-
throw new Error(devMessage );
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A safe hasOwnProperty access
|
|
46
|
-
*/
|
|
47
|
-
function hasOwnProperty(obj, key) {
|
|
48
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function isNumber(value) {
|
|
52
|
-
return Boolean(typeof value === 'number');
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function lengthEquals(value, arg1) {
|
|
56
|
-
return value.length === Number(arg1);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function isEmpty(value) {
|
|
60
|
-
if (!value) {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
else if (isNumber(value)) {
|
|
64
|
-
return value === 0;
|
|
65
|
-
}
|
|
66
|
-
else if (hasOwnProperty(value, 'length')) {
|
|
67
|
-
return lengthEquals(value, 0);
|
|
68
|
-
}
|
|
69
|
-
else if (typeof value === 'object') {
|
|
70
|
-
return lengthEquals(Object.keys(value), 0);
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
4
|
+
var vestUtils = require('vest-utils');
|
|
74
5
|
|
|
75
6
|
function ruleReturn(pass, message) {
|
|
76
|
-
|
|
7
|
+
const output = { pass };
|
|
77
8
|
if (message) {
|
|
78
9
|
output.message = message;
|
|
79
10
|
}
|
|
@@ -86,7 +17,7 @@ function passing() {
|
|
|
86
17
|
return ruleReturn(true);
|
|
87
18
|
}
|
|
88
19
|
function defaultToPassing(callback) {
|
|
89
|
-
return defaultTo(callback, passing());
|
|
20
|
+
return vestUtils.defaultTo(callback, passing());
|
|
90
21
|
}
|
|
91
22
|
|
|
92
23
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -99,38 +30,24 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
99
30
|
}
|
|
100
31
|
|
|
101
32
|
/* eslint-disable max-lines-per-function */
|
|
102
|
-
function compose() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
return Object.assign(function (value) {
|
|
108
|
-
var res = run(value);
|
|
109
|
-
if (!res.pass) {
|
|
110
|
-
if (isEmpty(res.message)) {
|
|
111
|
-
throwError();
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
115
|
-
throw res.message;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
33
|
+
function compose(...composites) {
|
|
34
|
+
return vestUtils.assign((value) => {
|
|
35
|
+
const res = run(value);
|
|
36
|
+
vestUtils.invariant(res.pass, vestUtils.StringObject(res.message));
|
|
118
37
|
}, {
|
|
119
|
-
run
|
|
120
|
-
test:
|
|
38
|
+
run,
|
|
39
|
+
test: (value) => run(value).pass,
|
|
121
40
|
});
|
|
122
41
|
function run(value) {
|
|
123
|
-
return n4s.ctx.run({ value
|
|
124
|
-
return defaultToPassing(mapFirst(composites,
|
|
42
|
+
return n4s.ctx.run({ value }, () => {
|
|
43
|
+
return defaultToPassing(vestUtils.mapFirst(composites, (composite, breakout) => {
|
|
125
44
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
126
|
-
The ideal is that instead of `
|
|
45
|
+
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
|
|
127
46
|
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
128
47
|
so we're limiting it to a small set of functions.
|
|
129
48
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
breakout(res);
|
|
133
|
-
}
|
|
49
|
+
const res = runLazyRule(composite, value);
|
|
50
|
+
breakout(!res.pass, res);
|
|
134
51
|
}));
|
|
135
52
|
});
|
|
136
53
|
}
|
package/dist/cjs/compose.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var t=require("n4s"),n=require("vest-utils");function r(t,n){const r={pass:t};return n&&(r.message=n),r}function s(t){return n.defaultTo(t,r(!0))}function e(t,n){try{return t.run(n)}catch(t){return r(!1)}}module.exports=function(...r){return n.assign((t=>{const r=u(t);n.invariant(r.pass,n.StringObject(r.message))}),{run:u,test:t=>u(t).pass});function u(u){return t.ctx.run({value:u},(()=>s(n.mapFirst(r,((t,n)=>{const r=e(t,u);n(!r.pass,r)})))))}};
|
|
@@ -1,41 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var n4s = require('n4s');
|
|
4
|
-
|
|
5
|
-
function mapFirst(array, callback) {
|
|
6
|
-
var broke = false;
|
|
7
|
-
var breakoutValue = null;
|
|
8
|
-
for (var i = 0; i < array.length; i++) {
|
|
9
|
-
callback(array[i], breakout, i);
|
|
10
|
-
if (broke) {
|
|
11
|
-
return breakoutValue;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function breakout(value) {
|
|
15
|
-
broke = true;
|
|
16
|
-
breakoutValue = value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function isFunction(value) {
|
|
21
|
-
return typeof value === 'function';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function optionalFunctionValue(value) {
|
|
25
|
-
var args = [];
|
|
26
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
27
|
-
args[_i - 1] = arguments[_i];
|
|
28
|
-
}
|
|
29
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function defaultTo(callback, defaultValue) {
|
|
33
|
-
var _a;
|
|
34
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
35
|
-
}
|
|
4
|
+
var vestUtils = require('vest-utils');
|
|
36
5
|
|
|
37
6
|
function ruleReturn(pass, message) {
|
|
38
|
-
|
|
7
|
+
const output = { pass };
|
|
39
8
|
if (message) {
|
|
40
9
|
output.message = message;
|
|
41
10
|
}
|
|
@@ -48,10 +17,10 @@ function passing() {
|
|
|
48
17
|
return ruleReturn(true);
|
|
49
18
|
}
|
|
50
19
|
function defaultToFailing(callback) {
|
|
51
|
-
return defaultTo(callback, failing());
|
|
20
|
+
return vestUtils.defaultTo(callback, failing());
|
|
52
21
|
}
|
|
53
22
|
function defaultToPassing(callback) {
|
|
54
|
-
return defaultTo(callback, passing());
|
|
23
|
+
return vestUtils.defaultTo(callback, passing());
|
|
55
24
|
}
|
|
56
25
|
|
|
57
26
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -63,70 +32,45 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
63
32
|
}
|
|
64
33
|
}
|
|
65
34
|
|
|
66
|
-
function allOf(value) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
72
|
-
var res = runLazyRule(rule, value);
|
|
73
|
-
if (!res.pass) {
|
|
74
|
-
breakout(res);
|
|
75
|
-
}
|
|
35
|
+
function allOf(value, ...rules) {
|
|
36
|
+
return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
37
|
+
const res = runLazyRule(rule, value);
|
|
38
|
+
breakout(!res.pass, res);
|
|
76
39
|
}));
|
|
77
40
|
}
|
|
78
41
|
|
|
79
|
-
function anyOf(value) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
return defaultToFailing(mapFirst(rules, function (rule, breakout) {
|
|
85
|
-
var res = runLazyRule(rule, value);
|
|
86
|
-
if (res.pass) {
|
|
87
|
-
breakout(res);
|
|
88
|
-
}
|
|
42
|
+
function anyOf(value, ...rules) {
|
|
43
|
+
return defaultToFailing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
44
|
+
const res = runLazyRule(rule, value);
|
|
45
|
+
breakout(res.pass, res);
|
|
89
46
|
}));
|
|
90
47
|
}
|
|
91
48
|
|
|
92
|
-
function noneOf(value) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
98
|
-
var res = runLazyRule(rule, value);
|
|
99
|
-
if (res.pass) {
|
|
100
|
-
breakout(failing());
|
|
101
|
-
}
|
|
49
|
+
function noneOf(value, ...rules) {
|
|
50
|
+
return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
51
|
+
const res = runLazyRule(rule, value);
|
|
52
|
+
breakout(res.pass, failing());
|
|
102
53
|
}));
|
|
103
54
|
}
|
|
104
55
|
|
|
105
|
-
function
|
|
106
|
-
return value
|
|
56
|
+
function equals(value, arg1) {
|
|
57
|
+
return value === arg1;
|
|
107
58
|
}
|
|
59
|
+
vestUtils.bindNot(equals);
|
|
108
60
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
function oneOf(value) {
|
|
115
|
-
var rules = [];
|
|
116
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
117
|
-
rules[_i - 1] = arguments[_i];
|
|
118
|
-
}
|
|
119
|
-
var passing = [];
|
|
120
|
-
rules.some(function (rule) {
|
|
121
|
-
if (longerThan(passing, REQUIRED_COUNT)) {
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
var res = runLazyRule(rule, value);
|
|
61
|
+
const REQUIRED_COUNT = 1;
|
|
62
|
+
function oneOf(value, ...rules) {
|
|
63
|
+
let passingCount = 0;
|
|
64
|
+
rules.some(rule => {
|
|
65
|
+
const res = runLazyRule(rule, value);
|
|
125
66
|
if (res.pass) {
|
|
126
|
-
|
|
67
|
+
passingCount++;
|
|
68
|
+
}
|
|
69
|
+
if (vestUtils.greaterThan(passingCount, REQUIRED_COUNT)) {
|
|
70
|
+
return false;
|
|
127
71
|
}
|
|
128
72
|
});
|
|
129
|
-
return ruleReturn(
|
|
73
|
+
return ruleReturn(equals(passingCount, REQUIRED_COUNT));
|
|
130
74
|
}
|
|
131
75
|
|
|
132
|
-
n4s.enforce.extend({ allOf
|
|
76
|
+
n4s.enforce.extend({ allOf, anyOf, noneOf, oneOf });
|
package/dist/cjs/compounds.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n=require("n4s")
|
|
1
|
+
"use strict";var n=require("n4s"),t=require("vest-utils");function r(n,t){const r={pass:n};return t&&(r.message=t),r}function e(){return r(!1)}function u(n){return t.defaultTo(n,r(!0))}function s(n,t){try{return n.run(t)}catch(n){return e()}}function o(n,t){return n===t}t.bindNot(o);n.enforce.extend({allOf:function(n,...r){return u(t.mapFirst(r,((t,r)=>{const e=s(t,n);r(!e.pass,e)})))},anyOf:function(n,...r){return u=t.mapFirst(r,((t,r)=>{const e=s(t,n);r(e.pass,e)})),t.defaultTo(u,e());var u},noneOf:function(n,...r){return u(t.mapFirst(r,((t,r)=>{r(s(t,n).pass,e())})))},oneOf:function(n,...e){let u=0;return e.some((r=>{if(s(r,n).pass&&u++,t.greaterThan(u,1))return!1})),r(o(u,1))}});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var n4s = require('n4s');
|
|
4
|
+
|
|
5
|
+
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
6
|
+
|
|
7
|
+
function assertString(input) {
|
|
8
|
+
var isString = typeof input === 'string' || input instanceof String;
|
|
9
|
+
|
|
10
|
+
if (!isString) {
|
|
11
|
+
var invalidType = _typeof(input);
|
|
12
|
+
|
|
13
|
+
if (input === null) invalidType = 'null';else if (invalidType === 'object') invalidType = input.constructor.name;
|
|
14
|
+
throw new TypeError("Expected a string but received a ".concat(invalidType));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function toDate(date) {
|
|
19
|
+
assertString(date);
|
|
20
|
+
date = Date.parse(date);
|
|
21
|
+
return !isNaN(date) ? new Date(date) : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isAfter(date, options) {
|
|
25
|
+
// For backwards compatibility:
|
|
26
|
+
// isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date`
|
|
27
|
+
var comparisonDate = (options === null || options === void 0 ? void 0 : options.comparisonDate) || options || Date().toString();
|
|
28
|
+
var comparison = toDate(comparisonDate);
|
|
29
|
+
var original = toDate(date);
|
|
30
|
+
return !!(original && comparison && original > comparison);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isBefore(str) {
|
|
34
|
+
var date = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String(new Date());
|
|
35
|
+
assertString(str);
|
|
36
|
+
var comparison = toDate(date);
|
|
37
|
+
var original = toDate(str);
|
|
38
|
+
return !!(original && comparison && original < comparison);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function merge() {
|
|
42
|
+
var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
43
|
+
var defaults = arguments.length > 1 ? arguments[1] : undefined;
|
|
44
|
+
|
|
45
|
+
for (var key in defaults) {
|
|
46
|
+
if (typeof obj[key] === 'undefined') {
|
|
47
|
+
obj[key] = defaults[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
55
|
+
|
|
56
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
57
|
+
|
|
58
|
+
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
59
|
+
|
|
60
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
61
|
+
|
|
62
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
63
|
+
|
|
64
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
65
|
+
|
|
66
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
67
|
+
var default_date_options = {
|
|
68
|
+
format: 'YYYY/MM/DD',
|
|
69
|
+
delimiters: ['/', '-'],
|
|
70
|
+
strictMode: false
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function isValidFormat(format) {
|
|
74
|
+
return /(^(y{4}|y{2})[.\/-](m{1,2})[.\/-](d{1,2})$)|(^(m{1,2})[.\/-](d{1,2})[.\/-]((y{4}|y{2})$))|(^(d{1,2})[.\/-](m{1,2})[.\/-]((y{4}|y{2})$))/gi.test(format);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function zip(date, format) {
|
|
78
|
+
var zippedArr = [],
|
|
79
|
+
len = Math.min(date.length, format.length);
|
|
80
|
+
|
|
81
|
+
for (var i = 0; i < len; i++) {
|
|
82
|
+
zippedArr.push([date[i], format[i]]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return zippedArr;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isDate(input, options) {
|
|
89
|
+
if (typeof options === 'string') {
|
|
90
|
+
// Allow backward compatbility for old format isDate(input [, format])
|
|
91
|
+
options = merge({
|
|
92
|
+
format: options
|
|
93
|
+
}, default_date_options);
|
|
94
|
+
} else {
|
|
95
|
+
options = merge(options, default_date_options);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (typeof input === 'string' && isValidFormat(options.format)) {
|
|
99
|
+
var formatDelimiter = options.delimiters.find(function (delimiter) {
|
|
100
|
+
return options.format.indexOf(delimiter) !== -1;
|
|
101
|
+
});
|
|
102
|
+
var dateDelimiter = options.strictMode ? formatDelimiter : options.delimiters.find(function (delimiter) {
|
|
103
|
+
return input.indexOf(delimiter) !== -1;
|
|
104
|
+
});
|
|
105
|
+
var dateAndFormat = zip(input.split(dateDelimiter), options.format.toLowerCase().split(formatDelimiter));
|
|
106
|
+
var dateObj = {};
|
|
107
|
+
|
|
108
|
+
var _iterator = _createForOfIteratorHelper(dateAndFormat),
|
|
109
|
+
_step;
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
113
|
+
var _step$value = _slicedToArray(_step.value, 2),
|
|
114
|
+
dateWord = _step$value[0],
|
|
115
|
+
formatWord = _step$value[1];
|
|
116
|
+
|
|
117
|
+
if (dateWord.length !== formatWord.length) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
dateObj[formatWord.charAt(0)] = dateWord;
|
|
122
|
+
}
|
|
123
|
+
} catch (err) {
|
|
124
|
+
_iterator.e(err);
|
|
125
|
+
} finally {
|
|
126
|
+
_iterator.f();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return new Date("".concat(dateObj.m, "/").concat(dateObj.d, "/").concat(dateObj.y)).getDate() === +dateObj.d;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!options.strictMode) {
|
|
133
|
+
return Object.prototype.toString.call(input) === '[object Date]' && isFinite(input);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/* eslint-disable max-len */
|
|
140
|
+
// from http://goo.gl/0ejHHW
|
|
141
|
+
|
|
142
|
+
var iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; // same as above, except with a strict 'T' separator between date and time
|
|
143
|
+
|
|
144
|
+
var iso8601StrictSeparator = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
145
|
+
/* eslint-enable max-len */
|
|
146
|
+
|
|
147
|
+
var isValidDate = function isValidDate(str) {
|
|
148
|
+
// str must have passed the ISO8601 check
|
|
149
|
+
// this check is meant to catch invalid dates
|
|
150
|
+
// like 2009-02-31
|
|
151
|
+
// first check for ordinal dates
|
|
152
|
+
var ordinalMatch = str.match(/^(\d{4})-?(\d{3})([ T]{1}\.*|$)/);
|
|
153
|
+
|
|
154
|
+
if (ordinalMatch) {
|
|
155
|
+
var oYear = Number(ordinalMatch[1]);
|
|
156
|
+
var oDay = Number(ordinalMatch[2]); // if is leap year
|
|
157
|
+
|
|
158
|
+
if (oYear % 4 === 0 && oYear % 100 !== 0 || oYear % 400 === 0) return oDay <= 366;
|
|
159
|
+
return oDay <= 365;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
|
|
163
|
+
var year = match[1];
|
|
164
|
+
var month = match[2];
|
|
165
|
+
var day = match[3];
|
|
166
|
+
var monthString = month ? "0".concat(month).slice(-2) : month;
|
|
167
|
+
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare
|
|
168
|
+
|
|
169
|
+
var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
|
|
170
|
+
|
|
171
|
+
if (month && day) {
|
|
172
|
+
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return true;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
function isISO8601(str) {
|
|
179
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
180
|
+
assertString(str);
|
|
181
|
+
var check = options.strictSeparator ? iso8601StrictSeparator.test(str) : iso8601.test(str);
|
|
182
|
+
if (check && options.strict) return isValidDate(str);
|
|
183
|
+
return check;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
n4s.enforce.extend({ isAfter, isBefore, isDate, isISO8601 });
|
package/dist/cjs/date.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("n4s");function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}function n(t){if(!("string"==typeof t||t instanceof String)){var n=r(t);throw null===t?n="null":"object"===n&&(n=t.constructor.name),new TypeError("Expected a string but received a ".concat(n))}}function e(t){return n(t),t=Date.parse(t),isNaN(t)?null:new Date(t)}function o(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments.length>1?arguments[1]:void 0;for(var n in r)void 0===t[n]&&(t[n]=r[n]);return t}function i(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],e=!0,o=!1,i=void 0;try{for(var a,u=t[Symbol.iterator]();!(e=(a=u.next()).done)&&(n.push(a.value),!r||n.length!==r);e=!0);}catch(t){o=!0,i=t}finally{try{e||null==u.return||u.return()}finally{if(o)throw i}}return n}(t,r)||a(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,r){if(t){if("string"==typeof t)return u(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(t,r):void 0}}function u(t,r){(null==r||r>t.length)&&(r=t.length);for(var n=0,e=new Array(r);n<r;n++)e[n]=t[n];return e}var c={format:"YYYY/MM/DD",delimiters:["/","-"],strictMode:!1};var f=/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/,d=/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/,l=function(t){var r=t.match(/^(\d{4})-?(\d{3})([ T]{1}\.*|$)/);if(r){var n=Number(r[1]),e=Number(r[2]);return n%4==0&&n%100!=0||n%400==0?e<=366:e<=365}var o=t.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number),i=o[1],a=o[2],u=o[3],c=a?"0".concat(a).slice(-2):a,f=u?"0".concat(u).slice(-2):u,d=new Date("".concat(i,"-").concat(c||"01","-").concat(f||"01"));return!a||!u||d.getUTCFullYear()===i&&d.getUTCMonth()+1===a&&d.getUTCDate()===u};t.enforce.extend({isAfter:function(t,r){var n=e((null==r?void 0:r.comparisonDate)||r||Date().toString()),o=e(t);return!!(o&&n&&o>n)},isBefore:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);n(t);var o=e(r),i=e(t);return!!(i&&o&&i<o)},isDate:function(t,r){if(r=o("string"==typeof r?{format:r}:r,c),"string"==typeof t&&(v=r.format,/(^(y{4}|y{2})[.\/-](m{1,2})[.\/-](d{1,2})$)|(^(m{1,2})[.\/-](d{1,2})[.\/-]((y{4}|y{2})$))|(^(d{1,2})[.\/-](m{1,2})[.\/-]((y{4}|y{2})$))/gi.test(v))){var n,e=r.delimiters.find((function(t){return-1!==r.format.indexOf(t)})),u=r.strictMode?e:r.delimiters.find((function(r){return-1!==t.indexOf(r)})),f=function(t,r){for(var n=[],e=Math.min(t.length,r.length),o=0;o<e;o++)n.push([t[o],r[o]]);return n}(t.split(u),r.format.toLowerCase().split(e)),d={},l=function(t,r){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=a(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var e=0,o=function(){};return{s:o,n:function(){return e>=t.length?{done:!0}:{done:!1,value:t[e++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=!0,c=!1;return{s:function(){n=t[Symbol.iterator]()},n:function(){var t=n.next();return u=t.done,t},e:function(t){c=!0,i=t},f:function(){try{u||null==n.return||n.return()}finally{if(c)throw i}}}}(f);try{for(l.s();!(n=l.n()).done;){var s=i(n.value,2),y=s[0],m=s[1];if(y.length!==m.length)return!1;d[m.charAt(0)]=y}}catch(t){l.e(t)}finally{l.f()}return new Date("".concat(d.m,"/").concat(d.d,"/").concat(d.y)).getDate()===+d.d}var v;return!r.strictMode&&("[object Date]"===Object.prototype.toString.call(t)&&isFinite(t))},isISO8601:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};n(t);var e=r.strictSeparator?d.test(t):f.test(t);return e&&r.strict?l(t):e}});
|