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
@@ -1,419 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.n4s = factory());
5
- }(this, (function () { 'use strict';
6
-
7
- function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function throwError(message) {
26
- throw new Error("[" + "n4s" + "]: " + message);
27
- }
28
-
29
- var isRule = function isRule(rulesObject, name) {
30
- var ruleExists = Object.prototype.hasOwnProperty.call(rulesObject, name) && typeof rulesObject[name] === 'function';
31
-
32
- if (!ruleExists) {
33
- throwError("Rule \"" + name + "\" was not found in rules object. Make sure you typed it correctly.");
34
- }
35
-
36
- return ruleExists;
37
- };
38
-
39
- var GLOBAL_OBJECT = Function('return this')();
40
-
41
- var proxySupported = function proxySupported() {
42
- return typeof GLOBAL_OBJECT.Proxy === 'function';
43
- };
44
-
45
- function bindNot(fn) {
46
- return function () {
47
- return !fn.apply(this, arguments);
48
- };
49
- }
50
-
51
- function endsWith(value, arg1) {
52
- return typeof value === 'string' && typeof arg1 === 'string' && value.endsWith(arg1);
53
- }
54
- var doesNotEndWith = bindNot(endsWith);
55
-
56
- function equals(value, arg1) {
57
- return value === arg1;
58
- }
59
- var notEquals = bindNot(equals);
60
-
61
- function isNumeric(value) {
62
- var result = !isNaN(parseFloat(value)) && !isNaN(Number(value)) && isFinite(value);
63
- return Boolean(result);
64
- }
65
- var isNotNumeric = bindNot(isNumeric);
66
-
67
- function greaterThan(value, arg1) {
68
- return isNumeric(value) && isNumeric(arg1) && Number(value) > Number(arg1);
69
- }
70
-
71
- function greaterThanOrEquals(value, arg1) {
72
- return isNumeric(value) && isNumeric(arg1) && Number(value) >= Number(arg1);
73
- }
74
-
75
- function inside(value, arg1) {
76
- if (Array.isArray(arg1) && ['string', 'number', 'boolean'].indexOf(typeof value) !== -1) {
77
- return arg1.indexOf(value) !== -1;
78
- } // both value and arg1 are strings
79
-
80
-
81
- if (typeof arg1 === 'string' && typeof value === 'string') {
82
- return arg1.indexOf(value) !== -1;
83
- }
84
-
85
- return false;
86
- }
87
- var notInside = bindNot(inside);
88
-
89
- function isArray(value) {
90
- return Boolean(Array.isArray(value));
91
- }
92
- var isNotArray = bindNot(isArray);
93
-
94
- function lessThanOrEquals(value, arg1) {
95
- return isNumeric(value) && isNumeric(arg1) && Number(value) <= Number(arg1);
96
- }
97
-
98
- function isBetween(value, min, max) {
99
- return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
100
- }
101
- var isNotBetween = bindNot(isBetween);
102
-
103
- function isEmpty(value) {
104
- if (!value) {
105
- return true;
106
- } else if (isNumeric(value)) {
107
- return value === 0;
108
- } else if (Object.prototype.hasOwnProperty.call(value, 'length')) {
109
- return value.length === 0;
110
- } else if (typeof value === 'object') {
111
- return Object.keys(value).length === 0;
112
- } else {
113
- return true;
114
- }
115
- }
116
- var isNotEmpty = bindNot(isEmpty);
117
-
118
- /**
119
- * Validates that a given value is an even number
120
- * @param {Number|String} value Value to be validated
121
- * @return {Boolean}
122
- */
123
-
124
- var isEven = function isEven(value) {
125
- if (!isNumeric(value)) {
126
- return false;
127
- }
128
-
129
- return value % 2 === 0;
130
- };
131
-
132
- function isNaN$1(value) {
133
- return Number.isNaN(value);
134
- }
135
- var isNotNaN = bindNot(isNaN$1);
136
-
137
- function isNegative(value) {
138
- if (isNumeric(value)) {
139
- return Number(value) < 0;
140
- }
141
-
142
- return false;
143
- }
144
- var isPositive = bindNot(isNegative);
145
-
146
- function isNull(value) {
147
- return value === null;
148
- }
149
- var isNotNull = bindNot(isNull);
150
-
151
- function isNumber(value) {
152
- return Boolean(typeof value === 'number');
153
- }
154
- var isNotNumber = bindNot(isNumber);
155
-
156
- /**
157
- * Validates that a given value is an odd number
158
- * @param {Number|String} value Value to be validated
159
- * @return {Boolean}
160
- */
161
-
162
- var isOdd = function isOdd(value) {
163
- if (!isNumeric(value)) {
164
- return false;
165
- }
166
-
167
- return value % 2 !== 0;
168
- };
169
-
170
- function isString(value) {
171
- return Boolean(typeof value === 'string');
172
- }
173
- var isNotString = bindNot(isString);
174
-
175
- function isTruthy(value) {
176
- return !!value;
177
- }
178
- var isFalsy = bindNot(isTruthy);
179
-
180
- function isUndefined(value) {
181
- return value === undefined;
182
- }
183
- var isNotUndefined = bindNot(isUndefined);
184
-
185
- function lengthEquals(value, arg1) {
186
- return value.length === arg1;
187
- }
188
- var lengthNotEquals = bindNot(lengthEquals);
189
-
190
- function lessThan(value, arg1) {
191
- return isNumeric(value) && isNumeric(arg1) && Number(value) < Number(arg1);
192
- }
193
-
194
- function longerThan(value, arg1) {
195
- return value.length > arg1;
196
- }
197
-
198
- function longerThanOrEquals(value, arg1) {
199
- return value.length >= arg1;
200
- }
201
-
202
- function matches(value, regex) {
203
- if (regex instanceof RegExp) {
204
- return regex.test(value);
205
- } else if (typeof regex === 'string') {
206
- return new RegExp(regex).test(value);
207
- } else {
208
- return false;
209
- }
210
- }
211
- var notMatches = bindNot(matches);
212
-
213
- function numberEquals(value, arg1) {
214
- return isNumeric(value) && isNumeric(arg1) && Number(value) === Number(arg1);
215
- }
216
- var numberNotEquals = bindNot(numberEquals);
217
-
218
- function shorterThan(value, arg1) {
219
- return value.length < arg1;
220
- }
221
-
222
- function shorterThanOrEquals(value, arg1) {
223
- return value.length <= arg1;
224
- }
225
-
226
- function startsWith(value, arg1) {
227
- return typeof value === 'string' && typeof arg1 === 'string' && value.startsWith(arg1);
228
- }
229
- var doesNotStartWith = bindNot(startsWith);
230
-
231
- var rules = {
232
- doesNotEndWith: doesNotEndWith,
233
- doesNotStartWith: doesNotStartWith,
234
- endsWith: endsWith,
235
- equals: equals,
236
- greaterThan: greaterThan,
237
- greaterThanOrEquals: greaterThanOrEquals,
238
- gt: greaterThan,
239
- gte: greaterThanOrEquals,
240
- inside: inside,
241
- isArray: isArray,
242
- isBetween: isBetween,
243
- isEmpty: isEmpty,
244
- isEven: isEven,
245
- isFalsy: isFalsy,
246
- isNaN: isNaN$1,
247
- isNegative: isNegative,
248
- isNotArray: isNotArray,
249
- isNotBetween: isNotBetween,
250
- isNotEmpty: isNotEmpty,
251
- isNotNaN: isNotNaN,
252
- isNotNull: isNotNull,
253
- isNotNumber: isNotNumber,
254
- isNotNumeric: isNotNumeric,
255
- isNotString: isNotString,
256
- isNotUndefined: isNotUndefined,
257
- isNull: isNull,
258
- isNumber: isNumber,
259
- isNumeric: isNumeric,
260
- isOdd: isOdd,
261
- isPositive: isPositive,
262
- isString: isString,
263
- isTruthy: isTruthy,
264
- isUndefined: isUndefined,
265
- lengthEquals: lengthEquals,
266
- lengthNotEquals: lengthNotEquals,
267
- lessThan: lessThan,
268
- lessThanOrEquals: lessThanOrEquals,
269
- longerThan: longerThan,
270
- longerThanOrEquals: longerThanOrEquals,
271
- lt: lessThan,
272
- lte: lessThanOrEquals,
273
- matches: matches,
274
- notEquals: notEquals,
275
- notInside: notInside,
276
- notMatches: notMatches,
277
- numberEquals: numberEquals,
278
- numberNotEquals: numberNotEquals,
279
- shorterThan: shorterThan,
280
- shorterThanOrEquals: shorterThanOrEquals,
281
- startsWith: startsWith
282
- };
283
-
284
- function validateResult(result, rule) {
285
- if (typeof result !== 'boolean' && (!result || typeof result.pass !== 'boolean')) {
286
- throwError(rule.name + " wrong return value for the rule please check that the return is valid");
287
- }
288
- } // for easier testing and mocking
289
-
290
- function getDefaultResult(value, rule) {
291
- return {
292
- message: formatResultMessage(rule, "invalid " + typeof value + " value")
293
- };
294
- }
295
- function formatResultMessage(rule, msg) {
296
- return "[" + "n4s" + "]/" + rule.name + " " + msg;
297
- }
298
- /**
299
- * Transform the result of a rule into a standard format
300
- * @param {string} interfaceName to be used in the messages
301
- * @param {*} result of the rule
302
- * @param {Object} options
303
- * @param {function} options.rule
304
- * @param {*} options.value
305
- * @returns {Object} result
306
- * @returns {string} result.message
307
- * @returns {boolean} result.pass indicates if the test passes or not
308
- */
309
-
310
- function transformResult(result, _ref) {
311
- var rule = _ref.rule,
312
- value = _ref.value;
313
- var defaultResult = getDefaultResult(value, rule);
314
- validateResult(result, rule);
315
-
316
- if (typeof result === 'boolean') {
317
- return _extends({}, defaultResult, {
318
- pass: result
319
- });
320
- } else {
321
- var formattedResult = {
322
- pass: result.pass
323
- };
324
-
325
- if (result.message) {
326
- formattedResult.message = formatResultMessage(rule, typeof result.message === 'function' ? result.message() : result.message);
327
- }
328
-
329
- return _extends({}, defaultResult, formattedResult);
330
- }
331
- }
332
-
333
- /**
334
- * Run a single rule against enforced value (e.g. `isNumber()`)
335
- *
336
- * @param {Function} rule - rule to run
337
- * @param {Any} value
338
- * @param {Array} args list of arguments sent from consumer
339
- * @throws
340
- */
341
-
342
- function runner(rule, value) {
343
- if (typeof rule !== 'function') {
344
- return;
345
- }
346
-
347
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
348
- args[_key - 2] = arguments[_key];
349
- }
350
-
351
- var ruleResult = rule.apply(void 0, [value].concat(args));
352
- var result = transformResult(ruleResult, {
353
- rule: rule,
354
- value: value
355
- });
356
-
357
- if (!result.pass) {
358
- throw new Error(result.message);
359
- }
360
- }
361
-
362
- var rulesObject = _extends({}, rules);
363
-
364
- var enforce, rulesList;
365
-
366
- if (proxySupported()) {
367
- enforce = function enforce(value) {
368
- var proxy = new Proxy(rulesObject, {
369
- get: function get(rules, fnName) {
370
- if (!isRule(rules, fnName)) {
371
- return;
372
- }
373
-
374
- return function () {
375
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
376
- args[_key] = arguments[_key];
377
- }
378
-
379
- runner.apply(void 0, [rules[fnName], value].concat(args));
380
- return proxy;
381
- };
382
- }
383
- });
384
- return proxy;
385
- };
386
- } else {
387
- rulesList = Object.keys(rulesObject);
388
-
389
- enforce = function enforce(value) {
390
- return rulesList.reduce(function (allRules, fnName) {
391
- var _ref;
392
-
393
- return Object.assign(allRules, _extends({}, isRule(rulesObject, fnName) && (_ref = {}, _ref[fnName] = function () {
394
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
395
- args[_key2] = arguments[_key2];
396
- }
397
-
398
- runner.apply(void 0, [rulesObject[fnName], value].concat(args));
399
- return allRules;
400
- }, _ref)));
401
- }, {});
402
- };
403
- }
404
-
405
- enforce.extend = function (customRules) {
406
- Object.assign(rulesObject, customRules);
407
-
408
- if (!proxySupported()) {
409
- rulesList = Object.keys(rulesObject);
410
- }
411
-
412
- return enforce;
413
- };
414
-
415
- var enforce$1 = enforce;
416
-
417
- return enforce$1;
418
-
419
- })));
@@ -1 +0,0 @@
1
- "use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).n4s=t()}(this,(function(){function n(){return(n=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e,r=arguments[t];for(e in r)Object.prototype.hasOwnProperty.call(r,e)&&(n[e]=r[e])}return n}).apply(this,arguments)}function t(n){return function(){return!n.apply(this,arguments)}}function e(n,t){return"string"==typeof n&&"string"==typeof t&&n.endsWith(t)}function r(n,t){return n===t}function o(n){return!(isNaN(parseFloat(n))||isNaN(Number(n))||!isFinite(n))}function i(n,t){return o(n)&&o(t)&&Number(n)>Number(t)}function u(n,t){return o(n)&&o(t)&&Number(n)>=Number(t)}function s(n,t){return!!(Array.isArray(t)&&-1!==["string","number","boolean"].indexOf(typeof n)||"string"==typeof t&&"string"==typeof n)&&-1!==t.indexOf(n)}function a(n){return!!Array.isArray(n)}function f(n,t){return o(n)&&o(t)&&Number(n)<=Number(t)}function c(n,t,e){return u(n,t)&&f(n,e)}function l(n){return!n||(o(n)?0===n:Object.prototype.hasOwnProperty.call(n,"length")?0===n.length:"object"!=typeof n||0===Object.keys(n).length)}function p(n){return Number.isNaN(n)}function y(n){return!!o(n)&&0>Number(n)}function h(n){return null===n}function g(n){return"number"==typeof n}function N(n){return"string"==typeof n}function b(n){return!!n}function m(n){return void 0===n}function d(n,t){return n.length===t}function v(n,t){return o(n)&&o(t)&&Number(n)<Number(t)}function O(n,t){return t instanceof RegExp?t.test(n):"string"==typeof t&&new RegExp(t).test(n)}function E(n,t){return o(n)&&o(t)&&Number(n)===Number(t)}function j(n,t){return"string"==typeof n&&"string"==typeof t&&n.startsWith(t)}function w(t,e){if("function"==typeof t){for(var r=arguments.length,o=Array(2<r?r-2:0),i=2;i<r;i++)o[i-2]=arguments[i];if(i=t.apply(void 0,[e].concat(o)),r={message:"[n4s]/"+t.name+" invalid "+typeof e+" value"},!("boolean"==typeof i||i&&"boolean"==typeof i.pass))throw Error("[n4s]: "+t.name+" wrong return value for the rule please check that the return is valid");if("boolean"==typeof i?r=n({},r,{pass:i}):(o={pass:i.pass},i.message&&(i="function"==typeof i.message?i.message():i.message,o.message="[n4s]/"+t.name+" "+i),r=n({},r,o)),!r.pass)throw Error(r.message)}}var T=function(n,t){if(!(n=Object.prototype.hasOwnProperty.call(n,t)&&"function"==typeof n[t]))throw Error('[n4s]: Rule "'+t+'" was not found in rules object. Make sure you typed it correctly.');return n},q=Function("return this")(),x=t(e),A=t(r),P=t(o),W=t(s),k=t(a),F=t(c),R=t(l),S=t(p),B=t(y),M=t(h),U=t(g),I=t(N),z=t(b),C=t(m),D=t(d),G=t(O),H=t(E),J=n({},{doesNotEndWith:x,doesNotStartWith:t(j),endsWith:e,equals:r,greaterThan:i,greaterThanOrEquals:u,gt:i,gte:u,inside:s,isArray:a,isBetween:c,isEmpty:l,isEven:function(n){return!!o(n)&&0==n%2},isFalsy:z,isNaN:p,isNegative:y,isNotArray:k,isNotBetween:F,isNotEmpty:R,isNotNaN:S,isNotNull:M,isNotNumber:U,isNotNumeric:P,isNotString:I,isNotUndefined:C,isNull:h,isNumber:g,isNumeric:o,isOdd:function(n){return!!o(n)&&0!=n%2},isPositive:B,isString:N,isTruthy:b,isUndefined:m,lengthEquals:d,lengthNotEquals:D,lessThan:v,lessThanOrEquals:f,longerThan:function(n,t){return n.length>t},longerThanOrEquals:function(n,t){return n.length>=t},lt:v,lte:f,matches:O,notEquals:A,notInside:W,notMatches:G,numberEquals:E,numberNotEquals:H,shorterThan:function(n,t){return n.length<t},shorterThanOrEquals:function(n,t){return n.length<=t},startsWith:j});if("function"==typeof q.Proxy)var K=function(n){var t=new Proxy(J,{get:function(e,r){if(T(e,r))return function(){for(var o=arguments.length,i=Array(o),u=0;u<o;u++)i[u]=arguments[u];return w.apply(void 0,[e[r],n].concat(i)),t}}});return t};else{var L=Object.keys(J);K=function(t){return L.reduce((function(e,r){var o;return Object.assign(e,n({},T(J,r)&&((o={})[r]=function(){for(var n=arguments.length,o=Array(n),i=0;i<n;i++)o[i]=arguments[i];return w.apply(void 0,[J[r],t].concat(o)),e},o)))}),{})}}return K.extend=function(n){return Object.assign(J,n),"function"!=typeof q.Proxy&&(L=Object.keys(J)),K},K}));