n4s 3.1.0 → 4.0.0-next-03ba92

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 (45) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/LICENSE +1 -2
  3. package/README.md +2 -5
  4. package/compose/package.json +7 -0
  5. package/dist/cjs/compose.development.js +222 -0
  6. package/dist/cjs/compose.js +7 -0
  7. package/dist/cjs/compose.production.js +1 -0
  8. package/dist/cjs/n4s.development.js +793 -0
  9. package/dist/cjs/n4s.js +7 -0
  10. package/dist/cjs/n4s.production.js +1 -0
  11. package/dist/cjs/package.json +1 -0
  12. package/dist/es/compose.development.js +220 -0
  13. package/dist/es/compose.production.js +1 -0
  14. package/dist/es/n4s.development.js +789 -0
  15. package/dist/es/n4s.production.js +1 -0
  16. package/dist/es/package.json +1 -0
  17. package/dist/umd/compose.development.js +228 -0
  18. package/dist/umd/compose.production.js +1 -0
  19. package/dist/umd/n4s.development.js +799 -0
  20. package/dist/umd/n4s.production.js +1 -0
  21. package/docs/README.md +2 -5
  22. package/docs/_sidebar.md +0 -1
  23. package/docs/external.md +1 -28
  24. package/package.json +67 -53
  25. package/tsconfig.json +8 -0
  26. package/types/compose.d.ts +9 -0
  27. package/types/n4s.d.ts +179 -0
  28. package/config/jest/jest.setup.js +0 -14
  29. package/config/rollup/enforce.js +0 -8
  30. package/config/rollup/rollup.config.js +0 -3
  31. package/docs/compound.md +0 -187
  32. package/docs/custom.md +0 -52
  33. package/docs/template.md +0 -53
  34. package/esm/n4s.es.development.js +0 -1142
  35. package/esm/n4s.es.production.js +0 -1142
  36. package/esm/n4s.es.production.min.js +0 -1
  37. package/esm/package.json +0 -1
  38. package/jest.config.js +0 -3
  39. package/n4s.cjs.development.js +0 -1144
  40. package/n4s.cjs.production.js +0 -1144
  41. package/n4s.cjs.production.min.js +0 -1
  42. package/n4s.js +0 -7
  43. package/n4s.umd.development.js +0 -1231
  44. package/n4s.umd.production.js +0 -1231
  45. package/n4s.umd.production.min.js +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,120 @@
1
+ # n4s - Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
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
+
7
+ ## 4.0.0 - 2021-11-20
8
+ ### Changed or removed
9
+ - 7c43eab major(context): used named export in context (ealush)
10
+ - e8652bc breaking(vest, enforce): prepare next major (ealush)
11
+ - dab8e00 breaking(vest, enforce): prepare next major (ealush)
12
+
13
+ ### Added
14
+ - 49e601a feat(n4s): enforce.compose (ealush)
15
+ - 2b902cb added(n4s): add custom message support to lazy enforcements (undefined)
16
+ - 220127b added(n4s): partial rule modifier (undefined)
17
+ - b5ce72d feat(n4s): context propagation within enforce (undefined)
18
+ - 651de44 feat(n4s): optional (undefined)
19
+ - 516ff54 feat(n4s): isArrayOf (undefined)
20
+ - 32fe8a5 feat(n4s): add shape and loose validations (undefined)
21
+
22
+ ### Fixed and improved
23
+ - 03ba92c vx: support namespaced exports (ealush)
24
+ - 38a54ae vx: support installing of local packages (ealush)
25
+ - 7baedf2 n4s: use named export in entry (ealush)
26
+ - 366001b patch(vest): better handling of skipped async tests (ealush)
27
+ - 5e9194f vx: add next release process (ealush)
28
+ - 7473483 hack(n4s): use 'any' as the input for compose (ealush)
29
+ - 8b30b66 patch(vest): make compose an external export (ealush)
30
+ - fe83e88 patch: Some cleanups (ealush)
31
+ - 780c5b7 types(n4s): Allow adding types for custom matchers (ealush)
32
+ - 94e00a8 patch(vest): Add isolate module for containing re-orders (ealush)
33
+ - bed7040 vx: add back to workspace (ealush)
34
+ - aae250a patch(vest): Throw error when tests are called in the wrong order (ealush)
35
+ - f2d458d update build artifacts (ealush)
36
+ - dc99d94 patch: shorten isBlank (ealush)
37
+ - package.json
38
+ - packages/anyone/package.json
39
+ - dbb836e patch: simplify fieldname comparison (ealush)
40
+ - 3fe2f21 chore: cleanups (ealush)
41
+ - .eslintrc.js
42
+ - 149aab3 add(n4s): enforce.condition (ealush)
43
+ - packages/anyone/README.md
44
+ - package.json
45
+ - d786b34 fix(n4s): make sure oneOf correctly validates single case (ealush)
46
+ - 13830b9 types(n4s): simplify the types for (ealush)
47
+ - packages/anyone/package.json
48
+ - 40ef071 chore: cleanup residue (undefined)
49
+ - eeac20e chore: remove duplicate types (undefined)
50
+ - e4e2a80 tests(n4s): add paritla tests (undefined)
51
+ - packages/anyone/package.json
52
+ - 4d88c04 patch: add nodejs exports (undefined)
53
+ - 6e57aa1 patch: remove unused exports (undefined)
54
+ - 1dddaf4 patch(n4s): improve types for rule chaining (undefined)
55
+ - packages/anyone/package.json
56
+ - 97175f1 patch: simplify proxy test/run assignment (undefined)
57
+ - 39b92f1 patch: enable enforce in es5 environments (undefined)
58
+ - 26af06b chore: reduce complexity, remove all lint errors (undefined)
59
+ - packages/anyone/.npmignore
60
+ - .github/PULL_REQUEST_TEMPLATE.md
61
+ - 5be986d chore: cleanup unused code (ealush)
62
+ - ba68539 lint: handling lint of all packages (ealush)
63
+ - .gitignore
64
+ - cca5130 patch(n4s): add ruleReturn default values (ealush)
65
+ - 75306ff fix(n4s): make enforce compound runners fall back to correct response (ealush)
66
+ - 4751584 fix(n4s): make enforce chaining work (ealush)
67
+ - 73b28a1 chore: some lint fixes (ealush)
68
+
69
+ ## 3.1.0 - 2021-08-06
70
+
71
+ ### Added
72
+
73
+ - bf432a1 added(n4s): add custom message support to lazy enforcements (undefined)
74
+ - c093a7f added(n4s): partial rule modifier (undefined)
75
+ - 045bc72 feat(n4s): context propagation within enforce (undefined)
76
+ - 443df9d feat(n4s): optional (undefined)
77
+ - 86172ca feat(n4s): isArrayOf (undefined)
78
+ - f48f38b feat(n4s): add shape and loose validations (undefined)
79
+
80
+ ### Fixed and improved
81
+
82
+ - fdcf20b genscript (undefined)
83
+ - b12614f chore: cleanup residue (undefined)
84
+ - 4da20b4 chore: remove duplicate types (undefined)
85
+ - c66298d tests(n4s): add paritla tests (undefined)
86
+ - packages/anyone/package.json
87
+ - a8c1e67 patch: add nodejs exports (undefined)
88
+ - 83efb86 patch: remove unused exports (undefined)
89
+ - 9546b9e patch(n4s): improve types for rule chaining (undefined)
90
+ - packages/anyone/package.json
91
+ - 7bab926 patch: simplify proxy test/run assignment (undefined)
92
+ - ed5c903 patch: enable enforce in es5 environments (undefined)
93
+ - 3494de6 chore: reduce complexity, remove all lint errors (undefined)
94
+ - packages/anyone/.npmignore
95
+ - .github/PULL_REQUEST_TEMPLATE.md
96
+ - aec6cd6 chore: cleanup unused code (ealush)
97
+ - 0103b38 lint: handling lint of all packages (ealush)
98
+ - .gitignore
99
+ - 03cf487 patch(n4s): add ruleReturn default values (ealush)
100
+ - 76e8c98 fix(n4s): make enforce compound runners fall back to correct response (ealush)
101
+ - ff91bd2 fix(n4s): make enforce chaining work (ealush)
102
+ - c3fd912 chore: some lint fixes (ealush)
103
+ - 49b6b84 Vest 4 Infra Setup (ealush)
104
+
105
+ ## 1.1.14 - 2021-07-02
106
+
107
+ ### Fixed and improved
108
+
109
+ - 34e0414 improved conditions (undefined)
110
+ - 26c28c6 all tests pass (undefined)
111
+ - 33f4e46 release (undefined)
112
+ - 6fe40c7 better bundle (undefined)
113
+ - c2cfb65 better typing (undefined)
114
+ - c6387ab before ts settings (undefined)
115
+ - c0e9708 generate correct d.ts file (undefined)
116
+ - 8e01b8e x (undefined)
117
+ - afb3960 x (undefined)
118
+ - e0a8463 add changelog support (undefined)
119
+ - cc46c38 current (undefined)
120
+ - b6db1c6 transform any to unknowns (ealush)
package/LICENSE CHANGED
@@ -1,7 +1,6 @@
1
1
  MIT License
2
2
 
3
3
  Copyright (c) 2020 ealush
4
- Copyright (c) 2019 Fiverr
5
4
 
6
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
6
  of this software and associated documentation files (the "Software"), to deal
@@ -19,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -7,7 +7,7 @@ By default, enforce throws an error when your validations fail. These errors sho
7
7
  You can extend Enforce per need, and you can add your custom validation rules in your app.
8
8
 
9
9
  ```js
10
- import enforce from 'n4s';
10
+ import { enforce } from 'n4s';
11
11
 
12
12
  enforce(4).isNumber();
13
13
  // passes
@@ -33,10 +33,7 @@ npm i n4s
33
33
  If you wish to use enforce's functionality safely with a boolean return interface, you can use its lazy validation interface:
34
34
 
35
35
  ```js
36
- enforce
37
- .isArray()
38
- .longerThan(3)
39
- .test([1,2,3])
36
+ enforce.isArray().longerThan(3).test([1, 2, 3]);
40
37
  ```
41
38
 
42
39
  [Read the docs](https://ealush.github.io/n4s)
@@ -0,0 +1,7 @@
1
+ {
2
+ "main": "../dist/cjs/compose.js",
3
+ "module": "../dist/es/compose.production.js",
4
+ "name": "compose",
5
+ "types": "../types/compose.d.ts",
6
+ "private": true
7
+ }
@@ -0,0 +1,222 @@
1
+ 'use strict';
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
+ var assign = Object.assign;
43
+
44
+ // eslint-disable-next-line max-lines-per-function
45
+ function createContext(init) {
46
+ var storage = { ancestry: [] };
47
+ return {
48
+ bind: bind,
49
+ run: run,
50
+ use: use,
51
+ useX: useX
52
+ };
53
+ function useX(errorMessage) {
54
+ var _a;
55
+ return ((_a = storage.ctx) !== null && _a !== void 0 ? _a : throwError(defaultTo(errorMessage, 'Context was used after it was closed')));
56
+ }
57
+ function run(ctxRef, fn) {
58
+ var _a;
59
+ var parentContext = use();
60
+ var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
61
+ var ctx = set(Object.freeze(out));
62
+ storage.ancestry.unshift(ctx);
63
+ var res = fn(ctx);
64
+ clear();
65
+ return res;
66
+ }
67
+ function bind(ctxRef, fn) {
68
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69
+ // @ts-ignore - this one's pretty hard to get right
70
+ var returnedFn = function () {
71
+ var runTimeArgs = [];
72
+ for (var _i = 0; _i < arguments.length; _i++) {
73
+ runTimeArgs[_i] = arguments[_i];
74
+ }
75
+ return run(ctxRef, function () {
76
+ return fn.apply(void 0, runTimeArgs);
77
+ });
78
+ };
79
+ return returnedFn;
80
+ }
81
+ function use() {
82
+ return storage.ctx;
83
+ }
84
+ function set(value) {
85
+ return (storage.ctx = value);
86
+ }
87
+ function clear() {
88
+ var _a;
89
+ storage.ancestry.shift();
90
+ set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
91
+ }
92
+ }
93
+
94
+ var ctx = createContext(function (ctxRef, parentContext) {
95
+ var base = {
96
+ value: ctxRef.value,
97
+ meta: ctxRef.meta || {}
98
+ };
99
+ if (!parentContext) {
100
+ return assign(base, {
101
+ parent: emptyParent
102
+ });
103
+ }
104
+ else if (ctxRef.set) {
105
+ return assign(base, {
106
+ parent: function () { return stripContext(parentContext); }
107
+ });
108
+ }
109
+ return parentContext;
110
+ });
111
+ function stripContext(ctx) {
112
+ if (!ctx) {
113
+ return ctx;
114
+ }
115
+ return {
116
+ value: ctx.value,
117
+ meta: ctx.meta,
118
+ parent: ctx.parent
119
+ };
120
+ }
121
+ function emptyParent() {
122
+ return null;
123
+ }
124
+
125
+ /**
126
+ * A safe hasOwnProperty access
127
+ */
128
+ function hasOwnProperty(obj, key) {
129
+ return Object.prototype.hasOwnProperty.call(obj, key);
130
+ }
131
+
132
+ function isNumber(value) {
133
+ return Boolean(typeof value === 'number');
134
+ }
135
+
136
+ function lengthEquals(value, arg1) {
137
+ return value.length === Number(arg1);
138
+ }
139
+
140
+ function isEmpty(value) {
141
+ if (!value) {
142
+ return true;
143
+ }
144
+ else if (isNumber(value)) {
145
+ return value === 0;
146
+ }
147
+ else if (hasOwnProperty(value, 'length')) {
148
+ return lengthEquals(value, 0);
149
+ }
150
+ else if (typeof value === 'object') {
151
+ return lengthEquals(Object.keys(value), 0);
152
+ }
153
+ return true;
154
+ }
155
+
156
+ function ruleReturn(pass, message) {
157
+ var output = { pass: pass };
158
+ if (message) {
159
+ output.message = message;
160
+ }
161
+ return output;
162
+ }
163
+ function failing() {
164
+ return ruleReturn(false);
165
+ }
166
+ function passing() {
167
+ return ruleReturn(true);
168
+ }
169
+ function defaultToPassing(callback) {
170
+ return defaultTo(callback, passing());
171
+ }
172
+
173
+ function runLazyRule(lazyRule, currentValue) {
174
+ try {
175
+ return lazyRule.run(currentValue);
176
+ }
177
+ catch (_a) {
178
+ return failing();
179
+ }
180
+ }
181
+
182
+ /* eslint-disable max-lines-per-function */
183
+ // TODO: This gives me a headache. Instead of `any` we should use `TLazy`
184
+ // but it fails when using compose. The type is very complex. Feel free to help.
185
+ function compose() {
186
+ var composites = [];
187
+ for (var _i = 0; _i < arguments.length; _i++) {
188
+ composites[_i] = arguments[_i];
189
+ }
190
+ return Object.assign(function (value) {
191
+ var res = run(value);
192
+ if (!res.pass) {
193
+ if (isEmpty(res.message)) {
194
+ throwError();
195
+ }
196
+ else {
197
+ // Explicitly throw a string so that vest.test can pick it up as the validation error message
198
+ throw res.message;
199
+ }
200
+ }
201
+ }, {
202
+ run: run,
203
+ test: function (value) { return run(value).pass; }
204
+ });
205
+ function run(value) {
206
+ return ctx.run({ value: value }, function () {
207
+ return defaultToPassing(mapFirst(composites, function (composite, breakout) {
208
+ /* HACK: Just a small white lie. ~~HELP WANTED~~.
209
+ The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
210
+ The problem is that lazy rules can't really be passed to this function due to some generic hell
211
+ so we're limiting it to a small set of functions.
212
+ */
213
+ var res = runLazyRule(composite, value);
214
+ if (!res.pass) {
215
+ breakout(res);
216
+ }
217
+ }));
218
+ });
219
+ }
220
+ }
221
+
222
+ module.exports = compose;
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./compose.production.js');
5
+ } else {
6
+ module.exports = require('./compose.development.js');
7
+ }
@@ -0,0 +1 @@
1
+ "use strict";function n(n,t){function r(n){e=!0,u=n}for(var e=!1,u=null,o=0;o<n.length;o++)if(t(n[o],r,o),e)return u}function t(n){return"function"==typeof n}function r(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return t(n)?n.apply(void 0,r):n}function e(n,t){var e;return null!==(e=r(n))&&void 0!==e?e:t}function u(n,t){throw Error(e(t,n))}var o=Object.assign;var i=function(n){function t(t,e){var u,f,a=i();return t=o({},a||{},null!==(u=r(n,t,a))&&void 0!==u?u:t),u=c.ctx=Object.freeze(t),c.ancestry.unshift(u),e=e(u),c.ancestry.shift(),c.ctx=null!==(f=c.ancestry[0])&&void 0!==f?f:null,e}function i(){return c.ctx}var c={ancestry:[]};return{bind:function(n,r){return function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];return t(n,(function(){return r.apply(void 0,e)}))}},run:t,use:i,useX:function(n){var t;return null!==(t=c.ctx)&&void 0!==t?t:u(e(n,"Context was used after it was closed"))}}}((function(n,t){var r={value:n.value,meta:n.meta||{}};return t?n.set?o(r,{parent:function(){return function(n){return n?{value:n.value,meta:n.meta,parent:n.parent}:n}(t)}}):t:o(r,{parent:c})}));function c(){return null}function f(n,t){return n.length===Number(t)}function a(n){if(n){if(function(n){return"number"==typeof n}(n))return 0===n;if(function(n,t){return Object.prototype.hasOwnProperty.call(n,t)}(n,"length"))return f(n,0);if("object"==typeof n)return f(Object.keys(n),0)}return!0}function s(n,t){return n={pass:n},t&&(n.message=t),n}function l(n){return e(n,s(!0))}function v(n,t){try{return n.run(t)}catch(n){return s(!1)}}module.exports=function(){function t(t){return i.run({value:t},(function(){return l(n(r,(function(n,r){(n=v(n,t)).pass||r(n)})))}))}for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return Object.assign((function(n){if(!(n=t(n)).pass){if(!a(n.message))throw n.message;u()}}),{run:t,test:function(n){return t(n).pass}})};