n4s 2.2.0-rc.1 → 4.0.0-dev-e266d9

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/LICENSE +1 -2
  3. package/README.md +10 -4
  4. package/compose/package.json +7 -0
  5. package/compounds/package.json +7 -0
  6. package/dist/cjs/compose.development.js +139 -0
  7. package/dist/cjs/compose.js +7 -0
  8. package/dist/cjs/compose.production.js +1 -0
  9. package/dist/cjs/compounds.development.js +132 -0
  10. package/dist/cjs/compounds.js +7 -0
  11. package/dist/cjs/compounds.production.js +1 -0
  12. package/dist/cjs/n4s.development.js +602 -0
  13. package/dist/cjs/n4s.js +7 -0
  14. package/dist/cjs/n4s.production.js +1 -0
  15. package/dist/cjs/package.json +1 -0
  16. package/dist/cjs/schema.development.js +144 -0
  17. package/dist/cjs/schema.js +7 -0
  18. package/dist/cjs/schema.production.js +1 -0
  19. package/dist/es/compose.development.js +137 -0
  20. package/dist/es/compose.production.js +1 -0
  21. package/dist/es/compounds.development.js +130 -0
  22. package/dist/es/compounds.production.js +1 -0
  23. package/dist/es/n4s.development.js +597 -0
  24. package/dist/es/n4s.production.js +1 -0
  25. package/dist/es/package.json +1 -0
  26. package/dist/es/schema.development.js +140 -0
  27. package/dist/es/schema.production.js +1 -0
  28. package/dist/umd/compose.development.js +143 -0
  29. package/dist/umd/compose.production.js +1 -0
  30. package/dist/umd/compounds.development.js +136 -0
  31. package/dist/umd/compounds.production.js +1 -0
  32. package/dist/umd/n4s.development.js +606 -0
  33. package/dist/umd/n4s.production.js +1 -0
  34. package/dist/umd/schema.development.js +148 -0
  35. package/dist/umd/schema.production.js +1 -0
  36. package/docs/README.md +10 -4
  37. package/docs/_sidebar.md +2 -2
  38. package/docs/external.md +27 -0
  39. package/docs/rules.md +74 -0
  40. package/package.json +126 -67
  41. package/schema/package.json +7 -0
  42. package/tsconfig.json +8 -0
  43. package/types/compose.d.ts +134 -0
  44. package/types/compounds.d.ts +146 -0
  45. package/types/n4s.d.ts +167 -0
  46. package/types/schema.d.ts +151 -0
  47. package/config/rollup/enforce.js +0 -13
  48. package/config/rollup/enforceExtended.js +0 -10
  49. package/config/rollup/ensure.js +0 -10
  50. package/config/rollup/rollup.config.js +0 -5
  51. package/docs/business_rules.md +0 -80
  52. package/docs/custom.md +0 -54
  53. package/docs/ensure.md +0 -40
  54. package/enforceExtended.cjs.development.js +0 -1949
  55. package/enforceExtended.cjs.production.js +0 -1949
  56. package/enforceExtended.cjs.production.min.js +0 -1
  57. package/enforceExtended.umd.development.js +0 -1955
  58. package/enforceExtended.umd.production.js +0 -1972
  59. package/enforceExtended.umd.production.min.js +0 -1
  60. package/ensure.cjs.development.js +0 -418
  61. package/ensure.cjs.production.js +0 -418
  62. package/ensure.cjs.production.min.js +0 -1
  63. package/ensure.umd.development.js +0 -424
  64. package/ensure.umd.production.js +0 -444
  65. package/ensure.umd.production.min.js +0 -1
  66. package/esm/enforceExtended.mjs.development.js +0 -1947
  67. package/esm/enforceExtended.mjs.production.js +0 -1947
  68. package/esm/enforceExtended.mjs.production.min.js +0 -1
  69. package/esm/ensure.mjs.development.js +0 -416
  70. package/esm/ensure.mjs.production.js +0 -416
  71. package/esm/ensure.mjs.production.min.js +0 -1
  72. package/esm/n4s.mjs.development.js +0 -394
  73. package/esm/n4s.mjs.production.js +0 -394
  74. package/esm/n4s.mjs.production.min.js +0 -1
  75. package/esm/package.json +0 -1
  76. package/jest.config.js +0 -3
  77. package/n4s.cjs.development.js +0 -396
  78. package/n4s.cjs.production.js +0 -396
  79. package/n4s.cjs.production.min.js +0 -1
  80. package/n4s.umd.development.js +0 -402
  81. package/n4s.umd.index.js +0 -7
  82. package/n4s.umd.production.js +0 -419
  83. package/n4s.umd.production.min.js +0 -1
@@ -0,0 +1,140 @@
1
+ import { ctx, 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
+ }
34
+
35
+ function ruleReturn(pass, message) {
36
+ var output = { pass: pass };
37
+ if (message) {
38
+ output.message = message;
39
+ }
40
+ return output;
41
+ }
42
+ function failing() {
43
+ return ruleReturn(false);
44
+ }
45
+ function passing() {
46
+ return ruleReturn(true);
47
+ }
48
+ function defaultToPassing(callback) {
49
+ return defaultTo(callback, passing());
50
+ }
51
+
52
+ function runLazyRule(lazyRule, currentValue) {
53
+ try {
54
+ return lazyRule.run(currentValue);
55
+ }
56
+ catch (_a) {
57
+ return failing();
58
+ }
59
+ }
60
+
61
+ function isArrayOf(inputArray, currentRule) {
62
+ return defaultToPassing(mapFirst(inputArray, function (currentValue, breakout, index) {
63
+ var res = ctx.run({ value: currentValue, set: true, meta: { index: index } }, function () { return runLazyRule(currentRule, currentValue); });
64
+ if (!res.pass) {
65
+ breakout(res);
66
+ }
67
+ }));
68
+ }
69
+
70
+ function loose(inputObject, shapeObject) {
71
+ var _loop_1 = function (key) {
72
+ var currentValue = inputObject[key];
73
+ var currentRule = shapeObject[key];
74
+ var res = ctx.run({ value: currentValue, set: true, meta: { key: key } }, function () {
75
+ return runLazyRule(currentRule, currentValue);
76
+ });
77
+ if (!res.pass) {
78
+ return { value: res };
79
+ }
80
+ };
81
+ for (var key in shapeObject) {
82
+ var state_1 = _loop_1(key);
83
+ if (typeof state_1 === "object")
84
+ return state_1.value;
85
+ }
86
+ return passing();
87
+ }
88
+
89
+ function isNull(value) {
90
+ return value === null;
91
+ }
92
+
93
+ function isUndefined(value) {
94
+ return value === undefined;
95
+ }
96
+
97
+ function isNullish(value) {
98
+ return isNull(value) || isUndefined(value);
99
+ }
100
+
101
+ function optional(value, ruleChain) {
102
+ if (isNullish(value)) {
103
+ return passing();
104
+ }
105
+ return runLazyRule(ruleChain, value);
106
+ }
107
+
108
+ /**
109
+ * A safe hasOwnProperty access
110
+ */
111
+ function hasOwnProperty(obj, key) {
112
+ return Object.prototype.hasOwnProperty.call(obj, key);
113
+ }
114
+
115
+ function shape(inputObject, shapeObject) {
116
+ var baseRes = loose(inputObject, shapeObject);
117
+ if (!baseRes.pass) {
118
+ return baseRes;
119
+ }
120
+ for (var key in inputObject) {
121
+ if (!hasOwnProperty(shapeObject, key)) {
122
+ return failing();
123
+ }
124
+ }
125
+ return passing();
126
+ }
127
+
128
+ // Help needed improving the typings of this file.
129
+ // Ideally, we'd be able to extend IShapeObject, but that's not possible.
130
+ function partial(shapeObject) {
131
+ var output = {};
132
+ for (var key in shapeObject) {
133
+ output[key] = enforce.optional(shapeObject[key]);
134
+ }
135
+ return output;
136
+ }
137
+
138
+ enforce.extend({ isArrayOf: isArrayOf, loose: loose, optional: optional, shape: shape });
139
+
140
+ export { partial };
@@ -0,0 +1 @@
1
+ import{ctx as n,enforce as r}from"n4s";function t(n,r){return n={pass:n},r&&(n.message=r),n}function u(n,r){try{return n.run(r)}catch(n){return t(!1)}}function e(r,e){function o(t){var o=r[t],i=e[t];if(!(t=n.run({value:o,set:!0,meta:{key:t}},(function(){return u(i,o)}))).pass)return{value:t}}for(var i in e){var a=o(i);if("object"==typeof a)return a.value}return t(!0)}r.extend({isArrayOf:function(r,e){return function(n,r){var t;return null!==(t=function(n){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];return"function"==typeof n?n.apply(void 0,r):n}(n))&&void 0!==t?t:r}(function(n,r){function t(n){u=!0,e=n}for(var u=!1,e=null,o=0;o<n.length;o++)if(r(n[o],t,o),u)return e}(r,(function(r,t,o){(o=n.run({value:r,set:!0,meta:{index:o}},(function(){return u(e,r)}))).pass||t(o)})),t(!0))},loose:e,optional:function(n,r){return null==n?t(!0):u(r,n)},shape:function(n,r){var u=e(n,r);if(!u.pass)return u;for(var o in n)if(!Object.prototype.hasOwnProperty.call(r,o))return t(!1);return t(!0)}});export function partial(n){var t,u={};for(t in n)u[t]=r.optional(n[t]);return u}
@@ -0,0 +1,143 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('n4s')) :
3
+ typeof define === 'function' && define.amd ? define(['n4s'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory(global.n4s));
5
+ }(this, (function (n4s) { 'use strict';
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
+ }
38
+
39
+ /**
40
+ * Throws a timed out error.
41
+ */
42
+ function throwError(devMessage, productionMessage) {
43
+ throw new Error(devMessage );
44
+ }
45
+
46
+ /**
47
+ * A safe hasOwnProperty access
48
+ */
49
+ function hasOwnProperty(obj, key) {
50
+ return Object.prototype.hasOwnProperty.call(obj, key);
51
+ }
52
+
53
+ function isNumber(value) {
54
+ return Boolean(typeof value === 'number');
55
+ }
56
+
57
+ function lengthEquals(value, arg1) {
58
+ return value.length === Number(arg1);
59
+ }
60
+
61
+ function isEmpty(value) {
62
+ if (!value) {
63
+ return true;
64
+ }
65
+ else if (isNumber(value)) {
66
+ return value === 0;
67
+ }
68
+ else if (hasOwnProperty(value, 'length')) {
69
+ return lengthEquals(value, 0);
70
+ }
71
+ else if (typeof value === 'object') {
72
+ return lengthEquals(Object.keys(value), 0);
73
+ }
74
+ return true;
75
+ }
76
+
77
+ function ruleReturn(pass, message) {
78
+ var output = { pass: pass };
79
+ if (message) {
80
+ output.message = message;
81
+ }
82
+ return output;
83
+ }
84
+ function failing() {
85
+ return ruleReturn(false);
86
+ }
87
+ function passing() {
88
+ return ruleReturn(true);
89
+ }
90
+ function defaultToPassing(callback) {
91
+ return defaultTo(callback, passing());
92
+ }
93
+
94
+ function runLazyRule(lazyRule, currentValue) {
95
+ try {
96
+ return lazyRule.run(currentValue);
97
+ }
98
+ catch (_a) {
99
+ return failing();
100
+ }
101
+ }
102
+
103
+ /* eslint-disable max-lines-per-function */
104
+ function compose() {
105
+ var composites = [];
106
+ for (var _i = 0; _i < arguments.length; _i++) {
107
+ composites[_i] = arguments[_i];
108
+ }
109
+ return Object.assign(function (value) {
110
+ var res = run(value);
111
+ if (!res.pass) {
112
+ if (isEmpty(res.message)) {
113
+ throwError();
114
+ }
115
+ else {
116
+ // Explicitly throw a string so that vest.test can pick it up as the validation error message
117
+ throw res.message;
118
+ }
119
+ }
120
+ }, {
121
+ run: run,
122
+ test: function (value) { return run(value).pass; }
123
+ });
124
+ function run(value) {
125
+ return n4s.ctx.run({ value: value }, function () {
126
+ return defaultToPassing(mapFirst(composites, function (composite, breakout) {
127
+ /* HACK: Just a small white lie. ~~HELP WANTED~~.
128
+ The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
129
+ The problem is that lazy rules can't really be passed to this function due to some generic hell
130
+ so we're limiting it to a small set of functions.
131
+ */
132
+ var res = runLazyRule(composite, value);
133
+ if (!res.pass) {
134
+ breakout(res);
135
+ }
136
+ }));
137
+ });
138
+ }
139
+ }
140
+
141
+ return compose;
142
+
143
+ })));
@@ -0,0 +1 @@
1
+ "use strict";!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("n4s")):"function"==typeof define&&define.amd?define(["n4s"],e):(n="undefined"!=typeof globalThis?globalThis:n||self).compose=e(n.n4s)}(this,(function(n){function e(n,e){function t(n){r=!0,o=n}for(var r=!1,o=null,u=0;u<n.length;u++)if(e(n[u],t,u),r)return o}function t(n,e){var t;return null!==(t=function(n){for(var e=[],t=1;t<arguments.length;t++)e[t-1]=arguments[t];return"function"==typeof n?n.apply(void 0,e):n}(n))&&void 0!==t?t:e}function r(n){if(n){if("number"==typeof n)return 0===n;if(Object.prototype.hasOwnProperty.call(n,"length"))return n.length===Number(0);if("object"==typeof n)return Object.keys(n).length===Number(0)}return!0}function o(n,e){return n={pass:n},e&&(n.message=e),n}return function(){function u(r){return n.ctx.run({value:r},(function(){return t(e(f,(function(n,e){try{var t=n.run(r)}catch(n){t=o(!1)}t.pass||e(t)})),o(!0))}))}for(var f=[],i=0;i<arguments.length;i++)f[i]=arguments[i];return Object.assign((function(n){if(!(n=u(n)).pass){if(r(n.message))throw Error(t(void 0,void 0));throw n.message}}),{run:u,test:function(n){return u(n).pass}})}}));
@@ -0,0 +1,136 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('n4s')) :
3
+ typeof define === 'function' && define.amd ? define(['n4s'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s));
5
+ }(this, (function (n4s) { 'use strict';
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
+ }
38
+
39
+ function ruleReturn(pass, message) {
40
+ var output = { pass: pass };
41
+ if (message) {
42
+ output.message = message;
43
+ }
44
+ return output;
45
+ }
46
+ function failing() {
47
+ return ruleReturn(false);
48
+ }
49
+ function passing() {
50
+ return ruleReturn(true);
51
+ }
52
+ function defaultToFailing(callback) {
53
+ return defaultTo(callback, failing());
54
+ }
55
+ function defaultToPassing(callback) {
56
+ return defaultTo(callback, passing());
57
+ }
58
+
59
+ function runLazyRule(lazyRule, currentValue) {
60
+ try {
61
+ return lazyRule.run(currentValue);
62
+ }
63
+ catch (_a) {
64
+ return failing();
65
+ }
66
+ }
67
+
68
+ function allOf(value) {
69
+ var rules = [];
70
+ for (var _i = 1; _i < arguments.length; _i++) {
71
+ rules[_i - 1] = arguments[_i];
72
+ }
73
+ return defaultToPassing(mapFirst(rules, function (rule, breakout) {
74
+ var res = runLazyRule(rule, value);
75
+ if (!res.pass) {
76
+ breakout(res);
77
+ }
78
+ }));
79
+ }
80
+
81
+ function anyOf(value) {
82
+ var rules = [];
83
+ for (var _i = 1; _i < arguments.length; _i++) {
84
+ rules[_i - 1] = arguments[_i];
85
+ }
86
+ return defaultToFailing(mapFirst(rules, function (rule, breakout) {
87
+ var res = runLazyRule(rule, value);
88
+ if (res.pass) {
89
+ breakout(res);
90
+ }
91
+ }));
92
+ }
93
+
94
+ function noneOf(value) {
95
+ var rules = [];
96
+ for (var _i = 1; _i < arguments.length; _i++) {
97
+ rules[_i - 1] = arguments[_i];
98
+ }
99
+ return defaultToPassing(mapFirst(rules, function (rule, breakout) {
100
+ var res = runLazyRule(rule, value);
101
+ if (res.pass) {
102
+ breakout(failing());
103
+ }
104
+ }));
105
+ }
106
+
107
+ function lengthEquals(value, arg1) {
108
+ return value.length === Number(arg1);
109
+ }
110
+
111
+ function longerThan(value, arg1) {
112
+ return value.length > Number(arg1);
113
+ }
114
+
115
+ var REQUIRED_COUNT = 1;
116
+ function oneOf(value) {
117
+ var rules = [];
118
+ for (var _i = 1; _i < arguments.length; _i++) {
119
+ rules[_i - 1] = arguments[_i];
120
+ }
121
+ var passing = [];
122
+ rules.some(function (rule) {
123
+ if (longerThan(passing, REQUIRED_COUNT)) {
124
+ return false;
125
+ }
126
+ var res = runLazyRule(rule, value);
127
+ if (res.pass) {
128
+ passing.push(res);
129
+ }
130
+ });
131
+ return ruleReturn(lengthEquals(passing, REQUIRED_COUNT));
132
+ }
133
+
134
+ n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
135
+
136
+ })));
@@ -0,0 +1 @@
1
+ "use strict";!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("n4s")):"function"==typeof define&&define.amd?define(["n4s"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n4s)}(this,(function(n){function e(n,e){function t(n){r=!0,f=n}for(var r=!1,f=null,o=0;o<n.length;o++)if(e(n[o],t,o),r)return f}function t(n,e){var t;return null!==(t=function(n){for(var e=[],t=1;t<arguments.length;t++)e[t-1]=arguments[t];return"function"==typeof n?n.apply(void 0,e):n}(n))&&void 0!==t?t:e}function r(n,e){return n={pass:n},e&&(n.message=e),n}function f(n,e){try{return n.run(e)}catch(n){return r(!1)}}n.enforce.extend({allOf:function(n){for(var o=[],u=1;u<arguments.length;u++)o[u-1]=arguments[u];return t(e(o,(function(e,t){(e=f(e,n)).pass||t(e)})),r(!0))},anyOf:function(n){for(var o=[],u=1;u<arguments.length;u++)o[u-1]=arguments[u];return t(e(o,(function(e,t){(e=f(e,n)).pass&&t(e)})),r(!1))},noneOf:function(n){for(var o=[],u=1;u<arguments.length;u++)o[u-1]=arguments[u];return t(e(o,(function(e,t){f(e,n).pass&&t(r(!1))})),r(!0))},oneOf:function(n){for(var e=[],t=1;t<arguments.length;t++)e[t-1]=arguments[t];var o=[];return e.some((function(e){if(o.length>Number(1))return!1;(e=f(e,n)).pass&&o.push(e)})),r(o.length===Number(1))}})}));