@muze-nl/assert 0.3.1 → 0.3.3
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/dist/browser.js +1 -2
- package/package.json +4 -2
- package/src/assert.mjs +6 -20
package/dist/browser.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=browser.js.map
|
|
1
|
+
(()=>{var m=Object.defineProperty;var b=(e,o)=>{for(var n in o)m(e,n,{get:o[n],enumerable:!0})};var c={};b(c,{Optional:()=>A,Recommended:()=>g,Required:()=>O,allOf:()=>R,anyOf:()=>v,assert:()=>h,disable:()=>d,enable:()=>x,error:()=>s,fails:()=>l,instanceOf:()=>_,not:()=>a,oneOf:()=>y,validEmail:()=>E,validURL:()=>w});globalThis.assertEnabled=!1;function x(){globalThis.assertEnabled=!0}function d(){globalThis.assertEnabled=!1}function h(e,o){if(globalThis.assertEnabled){let n=l(e,o);if(n)throw console.error("\u{1F170}\uFE0F Assertions failed because of:",n,"in this source:",e),new Error("Assertions failed",{cause:{problems:n,source:e}})}}function A(e){return function(n,r,i){if(typeof n<"u"&&n!=null&&typeof e<"u")return l(n,e,r,i)}}function O(e){return function(n,r,i){return n==null||typeof n>"u"?s("data is required",n,e||"any value",i):typeof e<"u"?l(n,e,r,i):!1}}function g(e){return function(n,r,i){return n==null||typeof n>"u"?(console.warn("data does not contain recommended value",n,e,i),!1):l(n,e,r,i)}}function y(...e){return function(n,r,i){for(let f of e)if(!l(n,f,r,i))return!1;return s("data does not match oneOf patterns",n,e,i)}}function v(...e){return function(n,r,i){if(!Array.isArray(n))return s("data is not an array",n,"anyOf",i);for(let f of n)if(y(...e)(f))return s("data does not match anyOf patterns",f,e,i);return!1}}function R(...e){return function(n,r,i){let f=[];for(let t of e)f=f.concat(l(n,t,r,i));if(f=f.filter(Boolean),f.length)return s("data does not match all given patterns",n,e,i,f)}}function w(e,o,n){try{e instanceof URL&&(e=e.href);let r=new URL(e);if(r.href!=e&&!(r.href+"/"==e||r.href==e+"/"))return s("data is not a valid url",e,"validURL",n)}catch{return s("data is not a valid url",e,"validURL",n)}}function E(e,o,n){if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e))return s("data is not a valid email",e,"validEmail",n)}function _(e){return function(n,r,i){if(!(n instanceof e))return s("data is not an instanceof pattern",n,e,i)}}function a(e){return function(n,r,i){if(!l(n,e,r,i))return s("data matches pattern, when required not to",n,e,i)}}function l(e,o,n,r=""){n||(n=e);let i=[];if(o===Boolean)typeof e!="boolean"&&!(e instanceof Boolean)&&i.push(s("data is not a boolean",e,o,r));else if(o===Number)typeof e!="number"&&!(e instanceof Number)&&i.push(s("data is not a number",e,o,r));else if(o===String)typeof e!="string"&&!(e instanceof String)&&i.push(s("data is not a string",e,o,r)),e==""&&i.push(s("data is an empty string, which is not allowed",e,o,r));else if(o instanceof RegExp)if(Array.isArray(e)){let f=e.findIndex((t,u)=>l(t,o,n,r+"["+u+"]"));f>-1&&i.push(s("data["+f+"] does not match pattern",e[f],o,r+"["+f+"]"))}else typeof e>"u"?i.push(s("data is undefined, should match pattern",e,o,r)):o.test(e)||i.push(s("data does not match pattern",e,o,r));else if(o instanceof Function){let f=o(e,n,r);f&&(Array.isArray(f)?i=i.concat(f):i.push(f))}else if(Array.isArray(o)){Array.isArray(e)||i.push(s("data is not an array",e,[],r));for(let f of o)for(let t of e.keys()){let u=l(e[t],f,n,r+"["+t+"]");Array.isArray(u)?i=i.concat(u):u&&i.push(u)}}else if(o&&typeof o=="object")if(Array.isArray(e)){let f=e.findIndex((t,u)=>l(t,o,n,r+"["+u+"]"));f>-1&&i.push(s("data["+f+"] does not match pattern",e[f],o,r+"["+f+"]"))}else if(!e||typeof e!="object")i.push(s("data is not an object, pattern is",e,o,r));else if(e instanceof URLSearchParams&&(e=Object.fromEntries(e)),o instanceof Function){let f=l(e,o,n,r);f&&(i=i.concat(f))}else for(let[f,t]of Object.entries(o)){let u=l(e[f],t,n,r+"."+f);u&&(i=i.concat(u))}else o!=e&&i.push(s("data and pattern are not equal",e,o,r));return i.length?i:!1}function s(e,o,n,r,i){let f={message:e,found:o,expected:n,path:r};return i&&(f.problems=i),f}globalThis.assert=c;})();
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muze-nl/assert",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "light optional assert library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/assert.mjs",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "tap test/*.mjs",
|
|
9
|
-
"tap": "tap"
|
|
9
|
+
"tap": "tap",
|
|
10
|
+
"build": "npx esbuild --bundle src/browser.mjs --outdir=dist --minify",
|
|
11
|
+
"build-dev": "npx esbuild --bundle src/browser.mjs --outdir=dist"
|
|
10
12
|
},
|
|
11
13
|
"repository": {
|
|
12
14
|
"type": "git",
|
package/src/assert.mjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
/*
|
|
2
|
-
FIXME: Optional() fails if the pattern is not set and the value is
|
|
3
|
-
TODO: scratch: Required() and Optional() and Recommended() should test multiple patterns
|
|
4
|
-
instead: add allOf(...patterns) function
|
|
5
2
|
TODO: add assertExplain global flag, so that if assert() fails, you can call explain() with
|
|
6
3
|
the same pattern and it will return text explanation of why it failed, each assertion function must
|
|
7
4
|
then check assertExplain, and return a text explanation of what fails or succeeds
|
|
@@ -39,7 +36,10 @@ export function assert(source, test) {
|
|
|
39
36
|
if (globalThis.assertEnabled) {
|
|
40
37
|
let problems = fails(source,test)
|
|
41
38
|
if (problems) {
|
|
42
|
-
|
|
39
|
+
console.error('🅰️ Assertions failed because of:', problems, 'in this source:', source)
|
|
40
|
+
throw new Error('Assertions failed', {
|
|
41
|
+
cause: { problems, source }
|
|
42
|
+
})
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -49,9 +49,7 @@ export function assert(source, test) {
|
|
|
49
49
|
*/
|
|
50
50
|
export function Optional(pattern) {
|
|
51
51
|
return function _Optional(data, root, path) {
|
|
52
|
-
if (data
|
|
53
|
-
return false
|
|
54
|
-
} else {
|
|
52
|
+
if (typeof data != 'undefined' && data!=null && typeof pattern != 'undefined' ) {
|
|
55
53
|
return fails(data, pattern, root, path)
|
|
56
54
|
}
|
|
57
55
|
}
|
|
@@ -76,7 +74,7 @@ export function Required(pattern) {
|
|
|
76
74
|
* Tests a given value against a pattern, only if the value is not null or undefined
|
|
77
75
|
* If null or undefined, it does print a warning to the console.
|
|
78
76
|
*/
|
|
79
|
-
export function Recommended(
|
|
77
|
+
export function Recommended(pattern) {
|
|
80
78
|
return function _Recommended(data, root, path) {
|
|
81
79
|
if (data==null || typeof data == 'undefined') {
|
|
82
80
|
console.warn('data does not contain recommended value', data, pattern, path)
|
|
@@ -290,18 +288,6 @@ export function fails(data, pattern, root, path='') {
|
|
|
290
288
|
return false
|
|
291
289
|
}
|
|
292
290
|
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Class used in assert() to add problems found and details to the error object
|
|
296
|
-
*/
|
|
297
|
-
class assertError extends Error {
|
|
298
|
-
constructor(message, problems, ...details) {
|
|
299
|
-
super(message)
|
|
300
|
-
this.problems = problems
|
|
301
|
-
this.details = details
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
291
|
/**
|
|
306
292
|
* Returns an object with message, found and expected properties
|
|
307
293
|
*/
|