@muze-nl/assert 0.1.0 → 0.1.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/README.md +46 -2
- package/dist/browser.js +2 -0
- package/dist/browser.js.map +1 -0
- package/package.json +11 -2
- package/src/assert.mjs +157 -112
- package/src/browser.mjs +3 -0
package/README.md
CHANGED
|
@@ -4,9 +4,53 @@
|
|
|
4
4
|
|
|
5
5
|
This is a light-weight library to do optional assertion checking. By default any assertions made are not tested. Assertion code is not run. Unless you toggle assertion checking, usually in developer mode, by calling `enable`. Now your assertions are run, and if any assertions fail, an error is thrown with information about the specific failure.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
_Note:_ This library was created as part of the [@muze-nl/metro](https://github.com/muze-nl/metro/) package initially, but has escaped its confines. In the rest of the documentation, when referring to 'middleware', we mean middleware modules for the metro http client in the browser.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
### Using NPM:
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm install @muze-nl/assert
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The include it in your javascript code like this:
|
|
18
|
+
```javascript
|
|
19
|
+
import * as assert from '@muze-nl/assert'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or if you are a fan of shorter assertions:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import { assert, enable, disable, Optional, Required, Recommended, oneOf, anyOf, not, validURL, instanceOf } from '@muze-nl/assert'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Using a CDN like jsdelivr
|
|
29
|
+
```html
|
|
30
|
+
<script src="https://cdn.jsdelivr.net/npm/@muze-nl/assert@0.1.1/dist/browser.js" integrity="sha384-fqO47gvA1/4UGo0iokMu6ZXdBCkRUbNfXejhrmZrWpJaP+7FPaJqJ03Irhzl1ifk" crossorigin="anonymous"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
_Note_: jsdelivr.com doesn't calculate the integrity hash for you, I've used https://www.srihash.org here.
|
|
34
|
+
|
|
35
|
+
Using a CDN like this means that assert is loaded globally as window.assert.
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
8
38
|
|
|
9
|
-
|
|
39
|
+
```javascript
|
|
40
|
+
function myFunction(param1, param2) {
|
|
41
|
+
assert(param1, Required(validURL))
|
|
42
|
+
assert(param2, Optional(not(/foo.*/)))
|
|
43
|
+
// do your own stuff here
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
When calling myFunction above, none of the assertions are actually checked, unless you enable assertion checking first, like this:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
enable()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Asserting preconditions
|
|
10
54
|
|
|
11
55
|
When writing middleware there is usually quite a lot of preconditions to check. When a developer wants to use your middleware, it is nice to have explicit feedback about what he or she is doing wrong. However this is only useful during development. Once in production you should assume that there are no developer mistakes anymore... or at least that the end user has no use for detailed error reports about your middleware.
|
|
12
56
|
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e,t,a,n){Object.defineProperty(e,t,{get:a,set:n,enumerable:!0,configurable:!0})}var t={};function a(){globalThis.assertEnabled=!0}function n(){globalThis.assertEnabled=!1}e(t,"enable",()=>a),e(t,"disable",()=>n),e(t,"assert",()=>r),e(t,"fails",()=>b),e(t,"Optional",()=>s),e(t,"Required",()=>o),e(t,"Recommended",()=>i),e(t,"oneOf",()=>l),e(t,"error",()=>m),e(t,"anyOf",()=>f),e(t,"validURL",()=>d),e(t,"validEmail",()=>u),e(t,"instanceOf",()=>c),e(t,"not",()=>h),globalThis.assertEnabled=!1;const r=(e,t)=>{if(globalThis.assertEnabled){let a=b(e,t);if(a)throw new y("Assertions failed",a,e)}},s=e=>t=>null!=t&&void 0!==t&&b(t,e),o=e=>t=>b(t,e),i=e=>t=>null==t||void 0===t?(console.warning("data does not contain recommended value",t,e),!1):b(t,e),l=(...e)=>t=>{for(let a of e)if(!b(t,a))return!1;return m("data does not match oneOf patterns",t,e)},f=(...e)=>t=>{if(!Array.isArray(t))return m("data is not an array",t,"anyOf");for(let a of t)if(l(...e)(a))return m("data does not match anyOf patterns",a,e);return!1};function d(e){try{if(e instanceof URL&&(e=e.href),new URL(e).href!=e)return m("data is not a valid url",e,"validURL")}catch(t){return m("data is not a valid url",e,"validURL")}return!1}function u(e){return!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)&&m("data is not a valid email",e,"validEmail")}const c=e=>t=>!(t instanceof e)&&m("data is not an instanceof pattern",t,e),h=e=>t=>!b(t,e)&&m("data matches pattern, when required not to",t,e);function b(e,t,a){a||(a=e);let n=[];if(t===Boolean)"boolean"!=typeof e&&n.push(m("data is not a boolean",e,t));else if(t===Number)"number"!=typeof e&&n.push(m("data is not a number",e,t));else if(t instanceof RegExp){if(Array.isArray(e)){let r=e.findIndex(e=>b(e,t,a));r>-1&&n.push(m("data["+r+"] does not match pattern",e[r],t))}else t.test(e)||n.push(m("data does not match pattern",e,t))}else if(t instanceof Function)t(e,a)&&n.push(m("data does not match function",e,t));else if(Array.isArray(t))for(p of(Array.isArray(e)||n.push(m("data is not an array",e,[])),t)){let t=b(e,p,a);Array.isArray(t)?n.concat(t):t&&n.push(t)}else if(t&&"object"==typeof t){if(Array.isArray(e)){let r=e.findIndex(e=>b(e,t,a));r>-1&&n.push(m("data["+r+"] does not match pattern",e[r],t))}else if(e&&"object"==typeof e){e instanceof URLSearchParams&&(e=Object.fromEntries(e));let r=n[n.length-1];for(let[s,o]of Object.entries(t)){let t=b(e[s],o,a);t&&(r&&"string"!=typeof r||(r={},n.push(m(r,e[s],o))),r[s]=t.problems)}}else n.push(m("data is not an object, pattern is",e,t))}else t!=e&&n.push(m("data and pattern are not equal",e,t));return!!n.length&&n}class y extends Error{constructor(e,t,...a){super(e),this.problems=t,this.details=a}}function m(e,t,a){return{message:e,found:t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AM,CAAO,CAAE,CAAK,CAAE,CAAQ,EAC7C,MAAO,CACN,QAAA,EACA,MAAA,EACA,SAAA,CACD,CACD,CD/OA,WAAW,MAAM,CAAG","sources":["<anon>","src/browser.mjs","src/assert.mjs"],"sourcesContent":["\nfunction $parcel$export(e, n, v, s) {\n Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});\n}\nvar $2ddd80d1adc2ea42$exports = {};\n\n$parcel$export($2ddd80d1adc2ea42$exports, \"enable\", () => $2ddd80d1adc2ea42$export$d7c4a0dd6a4567e5);\n$parcel$export($2ddd80d1adc2ea42$exports, \"disable\", () => $2ddd80d1adc2ea42$export$e20fbacbb41798b);\n$parcel$export($2ddd80d1adc2ea42$exports, \"assert\", () => $2ddd80d1adc2ea42$export$a7a9523472993e97);\n$parcel$export($2ddd80d1adc2ea42$exports, \"fails\", () => $2ddd80d1adc2ea42$export$478159de811fd37d);\n$parcel$export($2ddd80d1adc2ea42$exports, \"Optional\", () => $2ddd80d1adc2ea42$export$7acb7b24c478f9c6);\n$parcel$export($2ddd80d1adc2ea42$exports, \"Required\", () => $2ddd80d1adc2ea42$export$1788c381af06add2);\n$parcel$export($2ddd80d1adc2ea42$exports, \"Recommended\", () => $2ddd80d1adc2ea42$export$a83bcf183f49ea9);\n$parcel$export($2ddd80d1adc2ea42$exports, \"oneOf\", () => $2ddd80d1adc2ea42$export$a9a18ae5ba42aeab);\n$parcel$export($2ddd80d1adc2ea42$exports, \"error\", () => $2ddd80d1adc2ea42$export$a3bc9b8ed74fc);\n$parcel$export($2ddd80d1adc2ea42$exports, \"anyOf\", () => $2ddd80d1adc2ea42$export$b26c150f612c10f7);\n$parcel$export($2ddd80d1adc2ea42$exports, \"validURL\", () => $2ddd80d1adc2ea42$export$c6f1a4382426409f);\n$parcel$export($2ddd80d1adc2ea42$exports, \"validEmail\", () => $2ddd80d1adc2ea42$export$9ab921aaffe56820);\n$parcel$export($2ddd80d1adc2ea42$exports, \"instanceOf\", () => $2ddd80d1adc2ea42$export$ca03416d6c9e029e);\n$parcel$export($2ddd80d1adc2ea42$exports, \"not\", () => $2ddd80d1adc2ea42$export$6003a5f097c73977);\n/**\n * assertEnabled (Boolean) used to toggle whether the assert()\n * method should test assertions or not.\n */ globalThis.assertEnabled = false;\nfunction $2ddd80d1adc2ea42$export$d7c4a0dd6a4567e5() {\n globalThis.assertEnabled = true;\n}\nfunction $2ddd80d1adc2ea42$export$e20fbacbb41798b() {\n globalThis.assertEnabled = false;\n}\nconst $2ddd80d1adc2ea42$export$a7a9523472993e97 = (source, test)=>{\n if (globalThis.assertEnabled) {\n let problems = $2ddd80d1adc2ea42$export$478159de811fd37d(source, test);\n if (problems) throw new $2ddd80d1adc2ea42$var$assertError(\"Assertions failed\", problems, source);\n }\n};\nconst $2ddd80d1adc2ea42$export$7acb7b24c478f9c6 = (pattern)=>(data)=>data == null || typeof data == \"undefined\" ? false : $2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern);\nconst $2ddd80d1adc2ea42$export$1788c381af06add2 = (pattern)=>(data)=>$2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern);\nconst $2ddd80d1adc2ea42$export$a83bcf183f49ea9 = (pattern)=>(data)=>data == null || typeof data == \"undefined\" ? (()=>{\n console.warning(\"data does not contain recommended value\", data, pattern);\n return false;\n })() : $2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern);\nconst $2ddd80d1adc2ea42$export$a9a18ae5ba42aeab = (...patterns)=>(data)=>{\n for (let pattern of patterns){\n if (!$2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern)) return false;\n }\n return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data does not match oneOf patterns\", data, patterns);\n };\nconst $2ddd80d1adc2ea42$export$b26c150f612c10f7 = (...patterns)=>(data)=>{\n if (!Array.isArray(data)) return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not an array\", data, \"anyOf\");\n for (let value of data){\n if ($2ddd80d1adc2ea42$export$a9a18ae5ba42aeab(...patterns)(value)) return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data does not match anyOf patterns\", value, patterns);\n }\n return false;\n };\nfunction $2ddd80d1adc2ea42$export$c6f1a4382426409f(data) {\n try {\n if (data instanceof URL) data = data.href;\n let url = new URL(data);\n if (url.href != data) return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not a valid url\", data, \"validURL\");\n } catch (e) {\n return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not a valid url\", data, \"validURL\");\n }\n return false;\n}\nfunction $2ddd80d1adc2ea42$export$9ab921aaffe56820(data) {\n if (/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(data)) return false // data matches email regex\n ;\n return $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not a valid email\", data, \"validEmail\");\n}\nconst $2ddd80d1adc2ea42$export$ca03416d6c9e029e = (constructor)=>(data)=>!(data instanceof constructor) ? $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not an instanceof pattern\", data, constructor) : false;\nconst $2ddd80d1adc2ea42$export$6003a5f097c73977 = (pattern)=>(data)=>$2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern) ? false : $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data matches pattern, when required not to\", data, pattern);\nfunction $2ddd80d1adc2ea42$export$478159de811fd37d(data, pattern, root) {\n if (!root) root = data;\n let problems = [];\n if (pattern === Boolean) {\n if (typeof data != \"boolean\") problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not a boolean\", data, pattern));\n } else if (pattern === Number) {\n if (typeof data != \"number\") problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not a number\", data, pattern));\n } else if (pattern instanceof RegExp) {\n if (Array.isArray(data)) {\n let index = data.findIndex((element)=>$2ddd80d1adc2ea42$export$478159de811fd37d(element, pattern, root));\n if (index > -1) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data[\" + index + \"] does not match pattern\", data[index], pattern));\n } else if (!pattern.test(data)) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data does not match pattern\", data, pattern));\n } else if (pattern instanceof Function) {\n if (pattern(data, root)) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data does not match function\", data, pattern));\n } else if (Array.isArray(pattern)) {\n if (!Array.isArray(data)) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not an array\", data, []));\n for (p of pattern){\n let problem = $2ddd80d1adc2ea42$export$478159de811fd37d(data, p, root);\n if (Array.isArray(problem)) problems.concat(problem);\n else if (problem) problems.push(problem);\n }\n } else if (pattern && typeof pattern == \"object\") {\n if (Array.isArray(data)) {\n let index = data.findIndex((element)=>$2ddd80d1adc2ea42$export$478159de811fd37d(element, pattern, root));\n if (index > -1) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data[\" + index + \"] does not match pattern\", data[index], pattern));\n } else if (!data || typeof data != \"object\") problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data is not an object, pattern is\", data, pattern));\n else {\n if (data instanceof URLSearchParams) data = Object.fromEntries(data);\n let p1 = problems[problems.length - 1];\n for (const [wKey, wVal] of Object.entries(pattern)){\n let result = $2ddd80d1adc2ea42$export$478159de811fd37d(data[wKey], wVal, root);\n if (result) {\n if (!p1 || typeof p1 == \"string\") {\n p1 = {};\n problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(p1, data[wKey], wVal));\n }\n p1[wKey] = result.problems;\n }\n }\n }\n } else if (pattern != data) problems.push($2ddd80d1adc2ea42$export$a3bc9b8ed74fc(\"data and pattern are not equal\", data, pattern));\n if (problems.length) return problems;\n return false;\n}\n/**\n * Class used in assert() to add problems found and details to the error object\n */ class $2ddd80d1adc2ea42$var$assertError extends Error {\n constructor(message, problems, ...details){\n super(message);\n this.problems = problems;\n this.details = details;\n }\n}\nfunction $2ddd80d1adc2ea42$export$a3bc9b8ed74fc(message, found, expected) {\n return {\n message: message,\n found: found,\n expected: expected\n };\n}\n\n\nglobalThis.assert = $2ddd80d1adc2ea42$exports;\n\n\n//# sourceMappingURL=browser.js.map\n","import * as assert from './assert.mjs'\n\nglobalThis.assert = assert\n","/**\n * assertEnabled (Boolean) used to toggle whether the assert()\n * method should test assertions or not.\n */\nglobalThis.assertEnabled = false\n\n/**\n * Enables assertion testing with assert()\n */\nexport function enable() {\n\tglobalThis.assertEnabled = true\n}\n\n/**\n * Disables assertion testing with assert()\n */\nexport function disable() {\n\tglobalThis.assertEnabled = false\n}\n\n/**\n * This function will check the source for the assertions in test, if\n * assertion checking is enabled globally.\n * If it is, and any assertion fails, it will throw an assertError\n * with a list of problems and other details.\n */\nexport const assert = (source, test) => {\n\tif (globalThis.assertEnabled) {\n\t\tlet problems = fails(source,test)\n\t\tif (problems) {\n\t\t\tthrow new assertError('Assertions failed', problems, source)\n\t\t}\n\t}\n}\n\n/**\n * Tests a given value against a pattern, only if the value is not null or undefined\n */\nexport const Optional = (pattern) => \n\t(data) => (data==null || typeof data == 'undefined') ? false : fails(data, pattern)\n\n/**\n * Tests a given value against a pattern, always.\n */\nexport const Required = (pattern) =>\n\t(data) => fails(data, pattern)\n\n/**\n * Tests a given value against a pattern, only if the value is not null or undefined\n * If null or undefined, it does print a warning to the console.\n */\nexport const Recommended = (pattern) =>\n\t(data) => (data==null || typeof data == 'undefined') ? (() => {\n\t\tconsole.warning('data does not contain recommended value', data, pattern)\n\t\treturn false\n\t})() : fails(data, pattern)\n\n/**\n * Tests a given value against a set of patterns, untill one succeeds\n * Returns an error if none succeed\n */\nexport const oneOf = (...patterns) => \n\t(data) => {\n\t\tfor(let pattern of patterns) {\n\t\t\tif (!fails(data, pattern)) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn error('data does not match oneOf patterns',data,patterns)\n\t}\n\n/**\n * Tests a given array of values against a set of patterns\n * If any value does not match one of the patterns, it will return an error\n * If not given an array to test, it will return an error\n */\nexport const anyOf = (...patterns) =>\n\t(data) => {\n\t\tif (!Array.isArray(data)) {\n\t\t\treturn error('data is not an array',data,'anyOf')\n\t\t}\n\t\tfor (let value of data) {\n\t\t\tif (oneOf(...patterns)(value)) {\n\t\t\t\treturn error('data does not match anyOf patterns',value,patterns)\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n\n/**\n * Tests a given value to see if it is a valid (and absolute) URL, by\n * parsing it with the URL() constructor, and then testing the href\n * value to be equal to the initial value.\n */\nexport function validURL(data) {\n\ttry {\n\t\tif (data instanceof URL) {\n\t\t\tdata = data.href\n\t\t}\n\t\tlet url = new URL(data)\n\t\tif (url.href!=data) {\n\t\t\treturn error('data is not a valid url',data,'validURL')\n\t\t}\n\t} catch(e) {\n\t\treturn error('data is not a valid url',data,'validURL')\n\t}\n\treturn false\n}\n\n/**\n * Tests a given value to see if it looks like a valid email address, by\n * testing it against a regular expression. So there are no guarantees that\n * it is an actual working email address, just that it looks like one.\n */\nexport function validEmail(data) {\n\tif (/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(data)) {\n\t\treturn false // data matches email regex\n\t}\n\treturn error('data is not a valid email',data,'validEmail')\n}\n\n/**\n * Tests a given value to see if it is an object which is an instance of the given\n * constructor\n */\nexport const instanceOf = (constructor) =>\n\t(data) => !(data instanceof constructor) \n\t\t? error('data is not an instanceof pattern',data,constructor)\n\t\t: false\n\n/**\n * Runs the given test pattern on a value, if the test succeeds, it fails\n * the not() test.\n */\nexport const not = (pattern) =>\n\t(data) => fails(data, pattern) \n\t\t? false \n\t\t: error('data matches pattern, when required not to', data, pattern)\n\n/**\n * returns an array of problems if the data fails to satisfy \n * the assertions in the given pattern, false otherwise\n * @param {any} data The data to match\n * @param {any} pattern The pattern to match\n * @param {any} root Root object for assertions, set to data by default\n * @return {Array|false} Array with problems if the pattern fails, false otherwise\n */\nexport function fails(data, pattern, root) {\n\tif (!root) {\n\t\troot = data\n\t}\n\tlet problems = []\n\tif (pattern === Boolean) {\n\t\tif (typeof data != 'boolean') {\n\t\t\tproblems.push(error('data is not a boolean', data, pattern))\n\t\t}\t\t\n\t} else if (pattern === Number) {\n\t\tif (typeof data != 'number') {\n\t\t\tproblems.push(error('data is not a number', data, pattern))\n\t\t}\n\t} else if (pattern instanceof RegExp) {\n \tif (Array.isArray(data)) {\n\t\t\tlet index = data.findIndex(element => fails(element,pattern,root))\n if (index>-1) {\n \tproblems.push(error('data['+index+'] does not match pattern', data[index], pattern))\n }\n \t} else if (!pattern.test(data)) {\n \tproblems.push(error('data does not match pattern', data, pattern))\n }\n } else if (pattern instanceof Function) {\n if (pattern(data, root)) {\n \tproblems.push(error('data does not match function', data, pattern))\n }\n } else if (Array.isArray(pattern)) {\n\t\tif (!Array.isArray(data)) {\n\t\t\tproblems.push(error('data is not an array',data,[]))\n\t\t}\n\t\tfor (p of pattern) {\n\t\t\tlet problem = fails(data, p, root)\n\t\t\tif (Array.isArray(problem)) {\n\t\t\t\tproblems.concat(problem)\n\t\t\t} else if (problem) {\n\t\t\t\tproblems.push(problem)\n\t\t\t}\n \t}\n } else if (pattern && typeof pattern == 'object') {\n if (Array.isArray(data)) {\n let index = data.findIndex(element => fails(element,pattern,root))\n if (index>-1) {\n \tproblems.push(error('data['+index+'] does not match pattern', data[index], pattern))\n }\n } else if (!data || typeof data != 'object') {\n \tproblems.push(error('data is not an object, pattern is', data, pattern))\n } else {\n \tif (data instanceof URLSearchParams) {\n \t\tdata = Object.fromEntries(data)\n \t}\n\t let p = problems[problems.length-1]\n\t for (const [wKey, wVal] of Object.entries(pattern)) {\n\t let result = fails(data[wKey], wVal, root)\n\t if (result) {\n\t \tif (!p || typeof p == 'string') {\n\t \t\tp = {}\n\t \t\tproblems.push(error(p, data[wKey], wVal))\n\t \t}\n\t \tp[wKey] = result.problems\n\t }\n\t }\n\t }\n } else {\n \tif (pattern!=data) {\n \t\tproblems.push(error('data and pattern are not equal', data, pattern))\n \t}\n }\n if (problems.length) {\n \treturn problems\n }\n return false\n}\n\n\n/**\n * Class used in assert() to add problems found and details to the error object\n */\nclass assertError extends Error {\n\tconstructor(message, problems, ...details) {\n\t\tsuper(message)\n\t\tthis.problems = problems\n\t\tthis.details = details\n\t}\n}\n\n/**\n * Returns an object with message, found and expected properties\n */ \nexport function error(message, found, expected) {\n\treturn {\n\t\tmessage,\n\t\tfound,\n\t\texpected\n\t}\n}\n"],"names":["$parcel$export","e","n","v","s","Object","defineProperty","get","set","enumerable","configurable","$2ddd80d1adc2ea42$exports","$2ddd80d1adc2ea42$export$d7c4a0dd6a4567e5","globalThis","assertEnabled","$2ddd80d1adc2ea42$export$e20fbacbb41798b","$2ddd80d1adc2ea42$export$a7a9523472993e97","$2ddd80d1adc2ea42$export$478159de811fd37d","$2ddd80d1adc2ea42$export$7acb7b24c478f9c6","$2ddd80d1adc2ea42$export$1788c381af06add2","$2ddd80d1adc2ea42$export$a83bcf183f49ea9","$2ddd80d1adc2ea42$export$a9a18ae5ba42aeab","$2ddd80d1adc2ea42$export$a3bc9b8ed74fc","$2ddd80d1adc2ea42$export$b26c150f612c10f7","$2ddd80d1adc2ea42$export$c6f1a4382426409f","$2ddd80d1adc2ea42$export$9ab921aaffe56820","$2ddd80d1adc2ea42$export$ca03416d6c9e029e","$2ddd80d1adc2ea42$export$6003a5f097c73977","source","test","problems","$2ddd80d1adc2ea42$var$assertError","pattern","data","console","warning","patterns","Array","isArray","value","URL","href","url","constructor","root","Boolean","push","Number","RegExp","index","findIndex","element","Function","p","problem","concat","URLSearchParams","fromEntries","p1","length","wKey","wVal","entries","result","Error","message","details","found","expected","assert"],"version":3,"file":"browser.js.map","sourceRoot":"../"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muze-nl/assert",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "light optional assert library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/assert.mjs",
|
|
@@ -26,7 +26,16 @@
|
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
28
|
"/src",
|
|
29
|
+
"/dist",
|
|
29
30
|
"/LICENSE",
|
|
30
31
|
"/README.md"
|
|
31
|
-
]
|
|
32
|
+
],
|
|
33
|
+
"source": [ "src/browser.mjs" ],
|
|
34
|
+
"targets": {
|
|
35
|
+
"default": {
|
|
36
|
+
"distDir": "./dist",
|
|
37
|
+
"optimize": true
|
|
38
|
+
},
|
|
39
|
+
"main": false
|
|
40
|
+
}
|
|
32
41
|
}
|
package/src/assert.mjs
CHANGED
|
@@ -1,35 +1,154 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* assertEnabled (Boolean) used to toggle whether the assert()
|
|
3
|
+
* method should test assertions or not.
|
|
4
|
+
*/
|
|
4
5
|
globalThis.assertEnabled = false
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Enables assertion testing with assert()
|
|
9
|
+
*/
|
|
6
10
|
export function enable() {
|
|
7
11
|
globalThis.assertEnabled = true
|
|
8
12
|
}
|
|
9
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Disables assertion testing with assert()
|
|
16
|
+
*/
|
|
10
17
|
export function disable() {
|
|
11
18
|
globalThis.assertEnabled = false
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
/**
|
|
22
|
+
* This function will check the source for the assertions in test, if
|
|
23
|
+
* assertion checking is enabled globally.
|
|
24
|
+
* If it is, and any assertion fails, it will throw an assertError
|
|
25
|
+
* with a list of problems and other details.
|
|
26
|
+
*/
|
|
27
|
+
export const assert = (source, test) => {
|
|
28
|
+
if (globalThis.assertEnabled) {
|
|
29
|
+
let problems = fails(source,test)
|
|
30
|
+
if (problems) {
|
|
31
|
+
throw new assertError('Assertions failed', problems, source)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Tests a given value against a pattern, only if the value is not null or undefined
|
|
38
|
+
*/
|
|
39
|
+
export const Optional = (pattern) =>
|
|
40
|
+
(data) => (data==null || typeof data == 'undefined') ? false : fails(data, pattern)
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Tests a given value against a pattern, always.
|
|
44
|
+
*/
|
|
45
|
+
export const Required = (pattern) =>
|
|
46
|
+
(data) => fails(data, pattern)
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Tests a given value against a pattern, only if the value is not null or undefined
|
|
50
|
+
* If null or undefined, it does print a warning to the console.
|
|
51
|
+
*/
|
|
52
|
+
export const Recommended = (pattern) =>
|
|
53
|
+
(data) => (data==null || typeof data == 'undefined') ? (() => {
|
|
54
|
+
console.warning('data does not contain recommended value', data, pattern)
|
|
55
|
+
return false
|
|
56
|
+
})() : fails(data, pattern)
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Tests a given value against a set of patterns, untill one succeeds
|
|
60
|
+
* Returns an error if none succeed
|
|
61
|
+
*/
|
|
62
|
+
export const oneOf = (...patterns) =>
|
|
63
|
+
(data) => {
|
|
64
|
+
for(let pattern of patterns) {
|
|
65
|
+
if (!fails(data, pattern)) {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return error('data does not match oneOf patterns',data,patterns)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Tests a given array of values against a set of patterns
|
|
74
|
+
* If any value does not match one of the patterns, it will return an error
|
|
75
|
+
* If not given an array to test, it will return an error
|
|
76
|
+
*/
|
|
77
|
+
export const anyOf = (...patterns) =>
|
|
78
|
+
(data) => {
|
|
79
|
+
if (!Array.isArray(data)) {
|
|
80
|
+
return error('data is not an array',data,'anyOf')
|
|
81
|
+
}
|
|
82
|
+
for (let value of data) {
|
|
83
|
+
if (oneOf(...patterns)(value)) {
|
|
84
|
+
return error('data does not match anyOf patterns',value,patterns)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return false
|
|
19
88
|
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Tests a given value to see if it is a valid (and absolute) URL, by
|
|
92
|
+
* parsing it with the URL() constructor, and then testing the href
|
|
93
|
+
* value to be equal to the initial value.
|
|
94
|
+
*/
|
|
95
|
+
export function validURL(data) {
|
|
96
|
+
try {
|
|
97
|
+
if (data instanceof URL) {
|
|
98
|
+
data = data.href
|
|
99
|
+
}
|
|
100
|
+
let url = new URL(data)
|
|
101
|
+
if (url.href!=data) {
|
|
102
|
+
return error('data is not a valid url',data,'validURL')
|
|
103
|
+
}
|
|
104
|
+
} catch(e) {
|
|
105
|
+
return error('data is not a valid url',data,'validURL')
|
|
106
|
+
}
|
|
107
|
+
return false
|
|
20
108
|
}
|
|
21
109
|
|
|
22
110
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
111
|
+
* Tests a given value to see if it looks like a valid email address, by
|
|
112
|
+
* testing it against a regular expression. So there are no guarantees that
|
|
113
|
+
* it is an actual working email address, just that it looks like one.
|
|
114
|
+
*/
|
|
115
|
+
export function validEmail(data) {
|
|
116
|
+
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data)) {
|
|
117
|
+
return false // data matches email regex
|
|
118
|
+
}
|
|
119
|
+
return error('data is not a valid email',data,'validEmail')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Tests a given value to see if it is an object which is an instance of the given
|
|
124
|
+
* constructor
|
|
125
|
+
*/
|
|
126
|
+
export const instanceOf = (constructor) =>
|
|
127
|
+
(data) => !(data instanceof constructor)
|
|
128
|
+
? error('data is not an instanceof pattern',data,constructor)
|
|
129
|
+
: false
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Runs the given test pattern on a value, if the test succeeds, it fails
|
|
133
|
+
* the not() test.
|
|
31
134
|
*/
|
|
32
|
-
export
|
|
135
|
+
export const not = (pattern) =>
|
|
136
|
+
(data) => fails(data, pattern)
|
|
137
|
+
? false
|
|
138
|
+
: error('data matches pattern, when required not to', data, pattern)
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* returns an array of problems if the data fails to satisfy
|
|
142
|
+
* the assertions in the given pattern, false otherwise
|
|
143
|
+
* @param {any} data The data to match
|
|
144
|
+
* @param {any} pattern The pattern to match
|
|
145
|
+
* @param {any} root Root object for assertions, set to data by default
|
|
146
|
+
* @return {Array|false} Array with problems if the pattern fails, false otherwise
|
|
147
|
+
*/
|
|
148
|
+
export function fails(data, pattern, root) {
|
|
149
|
+
if (!root) {
|
|
150
|
+
root = data
|
|
151
|
+
}
|
|
33
152
|
let problems = []
|
|
34
153
|
if (pattern === Boolean) {
|
|
35
154
|
if (typeof data != 'boolean') {
|
|
@@ -41,7 +160,7 @@ export function fails(data, pattern, container) {
|
|
|
41
160
|
}
|
|
42
161
|
} else if (pattern instanceof RegExp) {
|
|
43
162
|
if (Array.isArray(data)) {
|
|
44
|
-
let index = data.findIndex(element => fails(element,pattern))
|
|
163
|
+
let index = data.findIndex(element => fails(element,pattern,root))
|
|
45
164
|
if (index>-1) {
|
|
46
165
|
problems.push(error('data['+index+'] does not match pattern', data[index], pattern))
|
|
47
166
|
}
|
|
@@ -49,7 +168,7 @@ export function fails(data, pattern, container) {
|
|
|
49
168
|
problems.push(error('data does not match pattern', data, pattern))
|
|
50
169
|
}
|
|
51
170
|
} else if (pattern instanceof Function) {
|
|
52
|
-
if (pattern(data,
|
|
171
|
+
if (pattern(data, root)) {
|
|
53
172
|
problems.push(error('data does not match function', data, pattern))
|
|
54
173
|
}
|
|
55
174
|
} else if (Array.isArray(pattern)) {
|
|
@@ -57,7 +176,7 @@ export function fails(data, pattern, container) {
|
|
|
57
176
|
problems.push(error('data is not an array',data,[]))
|
|
58
177
|
}
|
|
59
178
|
for (p of pattern) {
|
|
60
|
-
let problem = fails(data, p)
|
|
179
|
+
let problem = fails(data, p, root)
|
|
61
180
|
if (Array.isArray(problem)) {
|
|
62
181
|
problems.concat(problem)
|
|
63
182
|
} else if (problem) {
|
|
@@ -66,7 +185,7 @@ export function fails(data, pattern, container) {
|
|
|
66
185
|
}
|
|
67
186
|
} else if (pattern && typeof pattern == 'object') {
|
|
68
187
|
if (Array.isArray(data)) {
|
|
69
|
-
let index = data.findIndex(element => fails(element,pattern))
|
|
188
|
+
let index = data.findIndex(element => fails(element,pattern,root))
|
|
70
189
|
if (index>-1) {
|
|
71
190
|
problems.push(error('data['+index+'] does not match pattern', data[index], pattern))
|
|
72
191
|
}
|
|
@@ -78,7 +197,7 @@ export function fails(data, pattern, container) {
|
|
|
78
197
|
}
|
|
79
198
|
let p = problems[problems.length-1]
|
|
80
199
|
for (const [wKey, wVal] of Object.entries(pattern)) {
|
|
81
|
-
let result = fails(data[wKey], wVal,
|
|
200
|
+
let result = fails(data[wKey], wVal, root)
|
|
82
201
|
if (result) {
|
|
83
202
|
if (!p || typeof p == 'string') {
|
|
84
203
|
p = {}
|
|
@@ -99,99 +218,25 @@ export function fails(data, pattern, container) {
|
|
|
99
218
|
return false
|
|
100
219
|
}
|
|
101
220
|
|
|
102
|
-
export function assert(source, test) {
|
|
103
|
-
if (!globalThis.assertEnabled) {
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
let result = fails(source,test)
|
|
107
|
-
if (result) {
|
|
108
|
-
throw new assertError(result,source)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function Optional(pattern) {
|
|
113
|
-
return function(data) {
|
|
114
|
-
if (data==null || typeof data == 'undefined') {
|
|
115
|
-
return false
|
|
116
|
-
}
|
|
117
|
-
return fails(data, pattern)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function Required(pattern) {
|
|
122
|
-
return function(data) {
|
|
123
|
-
return fails(data, pattern)
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
221
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
export function oneOf(...patterns) {
|
|
136
|
-
return function(data) {
|
|
137
|
-
for(let pattern of patterns) {
|
|
138
|
-
if (!fails(data, pattern)) {
|
|
139
|
-
return false
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return error('data does not match oneOf patterns',data,patterns)
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function anyOf(...patterns) {
|
|
147
|
-
return function(data) {
|
|
148
|
-
if (!Array.isArray(data)) {
|
|
149
|
-
return error('data is not an array',data,'anyOf')
|
|
150
|
-
}
|
|
151
|
-
for (let value of data) {
|
|
152
|
-
if (oneOf(...patterns)(value)) {
|
|
153
|
-
return error('data does not match anyOf patterns',value,patterns)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return false
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function validURL(data) {
|
|
161
|
-
try {
|
|
162
|
-
let url = new URL(data)
|
|
163
|
-
if (url.href!=data) {
|
|
164
|
-
return error('data is not a fully qualified url',data,'validURL')
|
|
165
|
-
}
|
|
166
|
-
} catch(e) {
|
|
167
|
-
return error('data is not a valid url',data,'validURL')
|
|
168
|
-
}
|
|
169
|
-
return false
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function instanceOf(constructor) {
|
|
173
|
-
return function(data) {
|
|
174
|
-
if (!(data instanceof constructor)) {
|
|
175
|
-
return error('data is not an instanceof pattern',data,constructor)
|
|
176
|
-
}
|
|
177
|
-
return false
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function not(pattern) {
|
|
182
|
-
return function(data) {
|
|
183
|
-
let problem = fails(data, pattern)
|
|
184
|
-
if (problem) {
|
|
185
|
-
return false
|
|
186
|
-
}
|
|
187
|
-
return error('data matches pattern, when required not to', data, pattern)
|
|
222
|
+
/**
|
|
223
|
+
* Class used in assert() to add problems found and details to the error object
|
|
224
|
+
*/
|
|
225
|
+
class assertError extends Error {
|
|
226
|
+
constructor(message, problems, ...details) {
|
|
227
|
+
super(message)
|
|
228
|
+
this.problems = problems
|
|
229
|
+
this.details = details
|
|
188
230
|
}
|
|
189
231
|
}
|
|
190
232
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Returns an object with message, found and expected properties
|
|
235
|
+
*/
|
|
236
|
+
export function error(message, found, expected) {
|
|
237
|
+
return {
|
|
238
|
+
message,
|
|
239
|
+
found,
|
|
240
|
+
expected
|
|
196
241
|
}
|
|
197
242
|
}
|
package/src/browser.mjs
ADDED