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/dist/cjs/n4s.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";var n=require("vest-utils");const t=require("context").createCascade(((t,i)=>{const s={value:t.value,meta:t.meta||{}};return i?t.set?n.assign(s,{parent:()=>function(n){return{value:n.value,meta:n.meta,parent:n.parent}}(i)}):i:n.assign(s,{parent:e})}));function e(){return null}function i(t,e){return n.isStringValue(t)&&n.isStringValue(e)&&t.endsWith(e)}const s=n.bindNot(i);function r(n,t){return n===t}const u=n.bindNot(r);function o(t,e){return n.numberEquals(t,e)||n.greaterThan(t,e)}function a(t,e){return(n.isArray(e)||!(!n.isStringValue(e)||!n.isStringValue(t)))&&-1!==e.indexOf(t)}const l=n.bindNot(a);function c(t,e){return n.isNumeric(t)&&n.isNumeric(e)&&Number(t)<Number(e)}function N(t,e){return n.numberEquals(t,e)||c(t,e)}function f(n,t,e){return o(n,t)&&N(n,e)}const g=n.bindNot(f);function h(t){return n.isNullish(t)||n.isStringValue(t)&&!t.trim()}const d=n.bindNot(h),m=n.bindNot(n.isBoolean),b=t=>!!n.isNumeric(t)&&t%2==0;function p(n,t){return n in t}const E=n.bindNot(p);function y(n){return Number.isNaN(n)}const q=n.bindNot(y);function v(n){return c(n,0)}function S(n){return Boolean("number"==typeof n)}const O=n.bindNot(S),T=t=>!!n.isNumeric(t)&&t%2!=0,V=n.bindNot(n.isStringValue);function x(n){return!!n}const B=n.bindNot(x);function w(t,e){if(n.isNullish(e))return!1;for(const n in e)if(e[n]===t)return!0;return!1}const W=n.bindNot(w);function A(n,t){return o(n.length,t)}function P(t,e){return e instanceof RegExp?e.test(t):!!n.isStringValue(e)&&new RegExp(e).test(t)}const F=n.bindNot(P);function U(n,t){try{return t(n)}catch(n){return!1}}function j(n,t){return c(n.length,t)}function k(n,t){return N(n.length,t)}function I(t,e){return n.isStringValue(t)&&n.isStringValue(e)&&t.startsWith(e)}const J=n.bindNot(I);const K={condition:U,doesNotEndWith:s,doesNotStartWith:J,endsWith:i,equals:r,greaterThan:n.greaterThan,greaterThanOrEquals:o,gt:n.greaterThan,gte:o,inside:a,isArray:n.isArray,isBetween:f,isBlank:h,isBoolean:n.isBoolean,isEmpty:n.isEmpty,isEven:b,isFalsy:B,isKeyOf:p,isNaN:y,isNegative:v,isNotArray:n.isNotArray,isNotBetween:g,isNotBlank:d,isNotBoolean:m,isNotEmpty:n.isNotEmpty,isNotKeyOf:E,isNotNaN:q,isNotNull:n.isNotNull,isNotNullish:n.isNotNullish,isNotNumber:O,isNotNumeric:n.isNotNumeric,isNotString:V,isNotUndefined:n.isNotUndefined,isNotValueOf:W,isNull:n.isNull,isNullish:n.isNullish,isNumber:S,isNumeric:n.isNumeric,isOdd:T,isPositive:n.isPositive,isString:n.isStringValue,isTruthy:x,isUndefined:n.isUndefined,isValueOf:w,lengthEquals:n.lengthEquals,lengthNotEquals:n.lengthNotEquals,lessThan:c,lessThanOrEquals:N,longerThan:n.longerThan,longerThanOrEquals:A,lt:c,lte:N,matches:P,notEquals:u,notInside:l,notMatches:F,numberEquals:n.numberEquals,numberNotEquals:n.numberNotEquals,shorterThan:j,shorterThanOrEquals:k,startsWith:I};function R(n){return K[n]}function $(n,t){const e={pass:n};return t&&(e.message=t),e}function C(t){return n.defaultTo(t,$(!0))}function M(t,e,i,...s){return function(t){n.invariant(n.isBoolean(t)||t&&n.isBoolean(t.pass),"Incorrect return value for rule: "+JSON.stringify(t))}(t),n.isBoolean(t)?$(t):$(t.pass,n.optionalFunctionValue(t.message,e,i,...s))}function X(e){const i={message:function(n){return s=n,r}};let s;const r=new Proxy(i,{get:(u,o)=>{const a=R(o);return a?function(i,r,u){return function(...o){const a=t.run({value:e},(()=>M(r(e,...o),u,e,...o)));function l(){return n.isNullish(s)?n.isNullish(a.message)?`enforce/${u} failed with ${JSON.stringify(e)}`:n.StringObject(a.message):n.StringObject(s)}return n.invariant(a.pass,l()),i}}(r,a,o):i[o]}});return r}const z=function(){const e={context:()=>t.useX(),extend:t=>{n.assign(K,t)}};return new Proxy(n.assign(X,e),{get:(e,i)=>i in e?e[i]:R(i)?function(e){const i=[];let s;return function e(r){return(...u)=>{const o=R(r);i.push((n=>M(o(n,...u),r,n,...u)));let a={run:e=>C(n.mapFirst(i,((i,r)=>{var u;const o=t.run({value:e},(()=>i(e)));r(!o.pass,$(!!o.pass,null!==(u=n.optionalFunctionValue(s,e,o.message))&&void 0!==u?u:o.message))}))),test:n=>a.run(n).pass,message:n=>(n&&(s=n),a)};return a=new Proxy(a,{get:(n,t)=>R(t)?e(t):n[t]}),a}}(e)}(i):void 0})}();exports.ctx=t,exports.enforce=z;
|
|
@@ -1,43 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var n4s = require('n4s');
|
|
6
|
-
|
|
7
|
-
function mapFirst(array, callback) {
|
|
8
|
-
var broke = false;
|
|
9
|
-
var breakoutValue = null;
|
|
10
|
-
for (var i = 0; i < array.length; i++) {
|
|
11
|
-
callback(array[i], breakout, i);
|
|
12
|
-
if (broke) {
|
|
13
|
-
return breakoutValue;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function breakout(value) {
|
|
17
|
-
broke = true;
|
|
18
|
-
breakoutValue = value;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function isFunction(value) {
|
|
23
|
-
return typeof value === 'function';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function optionalFunctionValue(value) {
|
|
27
|
-
var args = [];
|
|
28
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
29
|
-
args[_i - 1] = arguments[_i];
|
|
30
|
-
}
|
|
31
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function defaultTo(callback, defaultValue) {
|
|
35
|
-
var _a;
|
|
36
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
37
|
-
}
|
|
4
|
+
var vestUtils = require('vest-utils');
|
|
38
5
|
|
|
39
6
|
function ruleReturn(pass, message) {
|
|
40
|
-
|
|
7
|
+
const output = { pass };
|
|
41
8
|
if (message) {
|
|
42
9
|
output.message = message;
|
|
43
10
|
}
|
|
@@ -50,7 +17,7 @@ function passing() {
|
|
|
50
17
|
return ruleReturn(true);
|
|
51
18
|
}
|
|
52
19
|
function defaultToPassing(callback) {
|
|
53
|
-
return defaultTo(callback, passing());
|
|
20
|
+
return vestUtils.defaultTo(callback, passing());
|
|
54
21
|
}
|
|
55
22
|
|
|
56
23
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -63,66 +30,38 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
63
30
|
}
|
|
64
31
|
|
|
65
32
|
function isArrayOf(inputArray, currentRule) {
|
|
66
|
-
return defaultToPassing(mapFirst(inputArray,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
breakout(res);
|
|
70
|
-
}
|
|
33
|
+
return defaultToPassing(vestUtils.mapFirst(inputArray, (currentValue, breakout, index) => {
|
|
34
|
+
const res = n4s.ctx.run({ value: currentValue, set: true, meta: { index } }, () => runLazyRule(currentRule, currentValue));
|
|
35
|
+
breakout(!res.pass, res);
|
|
71
36
|
}));
|
|
72
37
|
}
|
|
73
38
|
|
|
74
39
|
function loose(inputObject, shapeObject) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return runLazyRule(currentRule, currentValue);
|
|
80
|
-
});
|
|
40
|
+
for (const key in shapeObject) {
|
|
41
|
+
const currentValue = inputObject[key];
|
|
42
|
+
const currentRule = shapeObject[key];
|
|
43
|
+
const res = n4s.ctx.run({ value: currentValue, set: true, meta: { key } }, () => runLazyRule(currentRule, currentValue));
|
|
81
44
|
if (!res.pass) {
|
|
82
|
-
return
|
|
45
|
+
return res;
|
|
83
46
|
}
|
|
84
|
-
};
|
|
85
|
-
for (var key in shapeObject) {
|
|
86
|
-
var state_1 = _loop_1(key);
|
|
87
|
-
if (typeof state_1 === "object")
|
|
88
|
-
return state_1.value;
|
|
89
47
|
}
|
|
90
48
|
return passing();
|
|
91
49
|
}
|
|
92
50
|
|
|
93
|
-
function isNull(value) {
|
|
94
|
-
return value === null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function isUndefined(value) {
|
|
98
|
-
return value === undefined;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function isNullish(value) {
|
|
102
|
-
return isNull(value) || isUndefined(value);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
51
|
function optional(value, ruleChain) {
|
|
106
|
-
if (isNullish(value)) {
|
|
52
|
+
if (vestUtils.isNullish(value)) {
|
|
107
53
|
return passing();
|
|
108
54
|
}
|
|
109
55
|
return runLazyRule(ruleChain, value);
|
|
110
56
|
}
|
|
111
57
|
|
|
112
|
-
/**
|
|
113
|
-
* A safe hasOwnProperty access
|
|
114
|
-
*/
|
|
115
|
-
function hasOwnProperty(obj, key) {
|
|
116
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
58
|
function shape(inputObject, shapeObject) {
|
|
120
|
-
|
|
59
|
+
const baseRes = loose(inputObject, shapeObject);
|
|
121
60
|
if (!baseRes.pass) {
|
|
122
61
|
return baseRes;
|
|
123
62
|
}
|
|
124
|
-
for (
|
|
125
|
-
if (!hasOwnProperty(shapeObject, key)) {
|
|
63
|
+
for (const key in inputObject) {
|
|
64
|
+
if (!vestUtils.hasOwnProperty(shapeObject, key)) {
|
|
126
65
|
return failing();
|
|
127
66
|
}
|
|
128
67
|
}
|
|
@@ -130,15 +69,15 @@ function shape(inputObject, shapeObject) {
|
|
|
130
69
|
}
|
|
131
70
|
|
|
132
71
|
// Help needed improving the typings of this file.
|
|
133
|
-
// Ideally, we'd be able to extend
|
|
72
|
+
// Ideally, we'd be able to extend ShapeObject, but that's not possible.
|
|
134
73
|
function partial(shapeObject) {
|
|
135
|
-
|
|
136
|
-
for (
|
|
74
|
+
const output = {};
|
|
75
|
+
for (const key in shapeObject) {
|
|
137
76
|
output[key] = n4s.enforce.optional(shapeObject[key]);
|
|
138
77
|
}
|
|
139
78
|
return output;
|
|
140
79
|
}
|
|
141
80
|
|
|
142
|
-
n4s.enforce.extend({ isArrayOf
|
|
81
|
+
n4s.enforce.extend({ isArrayOf, loose, optional, shape });
|
|
143
82
|
|
|
144
83
|
exports.partial = partial;
|
package/dist/cjs/schema.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
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 s(){return r(!0)}function u(n,t){try{return n.run(t)}catch(n){return e()}}function o(t,r){for(const e in r){const s=t[e],o=r[e],i=n.ctx.run({value:s,set:!0,meta:{key:e}},(()=>u(o,s)));if(!i.pass)return i}return s()}n.enforce.extend({isArrayOf:function(r,e){return o=t.mapFirst(r,((t,r,s)=>{const o=n.ctx.run({value:t,set:!0,meta:{index:s}},(()=>u(e,t)));r(!o.pass,o)})),t.defaultTo(o,s());var o},loose:o,optional:function(n,r){return t.isNullish(n)?s():u(r,n)},shape:function(n,r){const u=o(n,r);if(!u.pass)return u;for(const s in n)if(!t.hasOwnProperty(r,s))return e();return s()}}),exports.partial=function(t){const r={};for(const e in t)r[e]=n.enforce.optional(t[e]);return r};
|
|
@@ -1,77 +1,8 @@
|
|
|
1
1
|
import { ctx } from 'n4s';
|
|
2
|
-
|
|
3
|
-
function mapFirst(array, callback) {
|
|
4
|
-
var broke = false;
|
|
5
|
-
var breakoutValue = null;
|
|
6
|
-
for (var i = 0; i < array.length; i++) {
|
|
7
|
-
callback(array[i], breakout, i);
|
|
8
|
-
if (broke) {
|
|
9
|
-
return breakoutValue;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function breakout(value) {
|
|
13
|
-
broke = true;
|
|
14
|
-
breakoutValue = value;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isFunction(value) {
|
|
19
|
-
return typeof value === 'function';
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function optionalFunctionValue(value) {
|
|
23
|
-
var args = [];
|
|
24
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
25
|
-
args[_i - 1] = arguments[_i];
|
|
26
|
-
}
|
|
27
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function defaultTo(callback, defaultValue) {
|
|
31
|
-
var _a;
|
|
32
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Throws a timed out error.
|
|
37
|
-
*/
|
|
38
|
-
function throwError(devMessage, productionMessage) {
|
|
39
|
-
throw new Error(devMessage );
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* A safe hasOwnProperty access
|
|
44
|
-
*/
|
|
45
|
-
function hasOwnProperty(obj, key) {
|
|
46
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function isNumber(value) {
|
|
50
|
-
return Boolean(typeof value === 'number');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function lengthEquals(value, arg1) {
|
|
54
|
-
return value.length === Number(arg1);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function isEmpty(value) {
|
|
58
|
-
if (!value) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
else if (isNumber(value)) {
|
|
62
|
-
return value === 0;
|
|
63
|
-
}
|
|
64
|
-
else if (hasOwnProperty(value, 'length')) {
|
|
65
|
-
return lengthEquals(value, 0);
|
|
66
|
-
}
|
|
67
|
-
else if (typeof value === 'object') {
|
|
68
|
-
return lengthEquals(Object.keys(value), 0);
|
|
69
|
-
}
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
2
|
+
import { defaultTo, assign, invariant, StringObject, mapFirst } from 'vest-utils';
|
|
72
3
|
|
|
73
4
|
function ruleReturn(pass, message) {
|
|
74
|
-
|
|
5
|
+
const output = { pass };
|
|
75
6
|
if (message) {
|
|
76
7
|
output.message = message;
|
|
77
8
|
}
|
|
@@ -97,41 +28,27 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
97
28
|
}
|
|
98
29
|
|
|
99
30
|
/* eslint-disable max-lines-per-function */
|
|
100
|
-
function compose() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
return Object.assign(function (value) {
|
|
106
|
-
var res = run(value);
|
|
107
|
-
if (!res.pass) {
|
|
108
|
-
if (isEmpty(res.message)) {
|
|
109
|
-
throwError();
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
113
|
-
throw res.message;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
31
|
+
function compose(...composites) {
|
|
32
|
+
return assign((value) => {
|
|
33
|
+
const res = run(value);
|
|
34
|
+
invariant(res.pass, StringObject(res.message));
|
|
116
35
|
}, {
|
|
117
|
-
run
|
|
118
|
-
test:
|
|
36
|
+
run,
|
|
37
|
+
test: (value) => run(value).pass,
|
|
119
38
|
});
|
|
120
39
|
function run(value) {
|
|
121
|
-
return ctx.run({ value
|
|
122
|
-
return defaultToPassing(mapFirst(composites,
|
|
40
|
+
return ctx.run({ value }, () => {
|
|
41
|
+
return defaultToPassing(mapFirst(composites, (composite, breakout) => {
|
|
123
42
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
124
|
-
The ideal is that instead of `
|
|
43
|
+
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
|
|
125
44
|
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
126
45
|
so we're limiting it to a small set of functions.
|
|
127
46
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
breakout(res);
|
|
131
|
-
}
|
|
47
|
+
const res = runLazyRule(composite, value);
|
|
48
|
+
breakout(!res.pass, res);
|
|
132
49
|
}));
|
|
133
50
|
});
|
|
134
51
|
}
|
|
135
52
|
}
|
|
136
53
|
|
|
137
|
-
export default
|
|
54
|
+
export { compose as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ctx as
|
|
1
|
+
import{ctx as t}from"n4s";import{defaultTo as n,assign as r,invariant as s,StringObject as u,mapFirst as e}from"vest-utils";function o(t,n){const r={pass:t};return n&&(r.message=n),r}function a(t){return n(t,o(!0))}function c(t,n){try{return t.run(n)}catch(t){return o(!1)}}function f(...n){return r((t=>{const n=o(t);s(n.pass,u(n.message))}),{run:o,test:t=>o(t).pass});function o(r){return t.run({value:r},(()=>a(e(n,((t,n)=>{const s=c(t,r);n(!s.pass,s)})))))}}export{f as default};
|
|
@@ -1,39 +1,8 @@
|
|
|
1
1
|
import { enforce } from 'n4s';
|
|
2
|
-
|
|
3
|
-
function mapFirst(array, callback) {
|
|
4
|
-
var broke = false;
|
|
5
|
-
var breakoutValue = null;
|
|
6
|
-
for (var i = 0; i < array.length; i++) {
|
|
7
|
-
callback(array[i], breakout, i);
|
|
8
|
-
if (broke) {
|
|
9
|
-
return breakoutValue;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function breakout(value) {
|
|
13
|
-
broke = true;
|
|
14
|
-
breakoutValue = value;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isFunction(value) {
|
|
19
|
-
return typeof value === 'function';
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function optionalFunctionValue(value) {
|
|
23
|
-
var args = [];
|
|
24
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
25
|
-
args[_i - 1] = arguments[_i];
|
|
26
|
-
}
|
|
27
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function defaultTo(callback, defaultValue) {
|
|
31
|
-
var _a;
|
|
32
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
33
|
-
}
|
|
2
|
+
import { defaultTo, mapFirst, bindNot, greaterThan } from 'vest-utils';
|
|
34
3
|
|
|
35
4
|
function ruleReturn(pass, message) {
|
|
36
|
-
|
|
5
|
+
const output = { pass };
|
|
37
6
|
if (message) {
|
|
38
7
|
output.message = message;
|
|
39
8
|
}
|
|
@@ -61,70 +30,45 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
61
30
|
}
|
|
62
31
|
}
|
|
63
32
|
|
|
64
|
-
function allOf(value) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
70
|
-
var res = runLazyRule(rule, value);
|
|
71
|
-
if (!res.pass) {
|
|
72
|
-
breakout(res);
|
|
73
|
-
}
|
|
33
|
+
function allOf(value, ...rules) {
|
|
34
|
+
return defaultToPassing(mapFirst(rules, (rule, breakout) => {
|
|
35
|
+
const res = runLazyRule(rule, value);
|
|
36
|
+
breakout(!res.pass, res);
|
|
74
37
|
}));
|
|
75
38
|
}
|
|
76
39
|
|
|
77
|
-
function anyOf(value) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
return defaultToFailing(mapFirst(rules, function (rule, breakout) {
|
|
83
|
-
var res = runLazyRule(rule, value);
|
|
84
|
-
if (res.pass) {
|
|
85
|
-
breakout(res);
|
|
86
|
-
}
|
|
40
|
+
function anyOf(value, ...rules) {
|
|
41
|
+
return defaultToFailing(mapFirst(rules, (rule, breakout) => {
|
|
42
|
+
const res = runLazyRule(rule, value);
|
|
43
|
+
breakout(res.pass, res);
|
|
87
44
|
}));
|
|
88
45
|
}
|
|
89
46
|
|
|
90
|
-
function noneOf(value) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
96
|
-
var res = runLazyRule(rule, value);
|
|
97
|
-
if (res.pass) {
|
|
98
|
-
breakout(failing());
|
|
99
|
-
}
|
|
47
|
+
function noneOf(value, ...rules) {
|
|
48
|
+
return defaultToPassing(mapFirst(rules, (rule, breakout) => {
|
|
49
|
+
const res = runLazyRule(rule, value);
|
|
50
|
+
breakout(res.pass, failing());
|
|
100
51
|
}));
|
|
101
52
|
}
|
|
102
53
|
|
|
103
|
-
function
|
|
104
|
-
return value
|
|
54
|
+
function equals(value, arg1) {
|
|
55
|
+
return value === arg1;
|
|
105
56
|
}
|
|
57
|
+
bindNot(equals);
|
|
106
58
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
function oneOf(value) {
|
|
113
|
-
var rules = [];
|
|
114
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
115
|
-
rules[_i - 1] = arguments[_i];
|
|
116
|
-
}
|
|
117
|
-
var passing = [];
|
|
118
|
-
rules.some(function (rule) {
|
|
119
|
-
if (longerThan(passing, REQUIRED_COUNT)) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
var res = runLazyRule(rule, value);
|
|
59
|
+
const REQUIRED_COUNT = 1;
|
|
60
|
+
function oneOf(value, ...rules) {
|
|
61
|
+
let passingCount = 0;
|
|
62
|
+
rules.some(rule => {
|
|
63
|
+
const res = runLazyRule(rule, value);
|
|
123
64
|
if (res.pass) {
|
|
124
|
-
|
|
65
|
+
passingCount++;
|
|
66
|
+
}
|
|
67
|
+
if (greaterThan(passingCount, REQUIRED_COUNT)) {
|
|
68
|
+
return false;
|
|
125
69
|
}
|
|
126
70
|
});
|
|
127
|
-
return ruleReturn(
|
|
71
|
+
return ruleReturn(equals(passingCount, REQUIRED_COUNT));
|
|
128
72
|
}
|
|
129
73
|
|
|
130
|
-
enforce.extend({ allOf
|
|
74
|
+
enforce.extend({ allOf, anyOf, noneOf, oneOf });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{enforce as n}from"n4s";
|
|
1
|
+
import{enforce as n}from"n4s";import{defaultTo as t,mapFirst as r,bindNot as u,greaterThan as e}from"vest-utils";function o(n,t){const r={pass:n};return t&&(r.message=t),r}function s(){return o(!1)}function f(n){return t(n,o(!0))}function c(n,t){try{return n.run(t)}catch(n){return s()}}function i(n,t){return n===t}u(i);n.extend({allOf:function(n,...t){return f(r(t,((t,r)=>{const u=c(t,n);r(!u.pass,u)})))},anyOf:function(n,...u){return e=r(u,((t,r)=>{const u=c(t,n);r(u.pass,u)})),t(e,s());var e},noneOf:function(n,...t){return f(r(t,((t,r)=>{r(c(t,n).pass,s())})))},oneOf:function(n,...t){let r=0;return t.some((t=>{if(c(t,n).pass&&r++,e(r,1))return!1})),o(i(r,1))}});
|