cno-bedrock 0.0.1
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/.github/workflows/ci.yml +48 -0
- package/CHANGELOG.md +33 -0
- package/LICENSE +16 -0
- package/README.md +39 -0
- package/ci/github-actions.hjson +72 -0
- package/docs/src/cno-assert.md +158 -0
- package/docs/src/cno-noop.md +29 -0
- package/docs/src/cno-options.md +61 -0
- package/docs/src/cno-time.md +145 -0
- package/docs/src/cno-utility.md +76 -0
- package/docs/src/lib.md +73 -0
- package/docs/src/lib.test.md +29 -0
- package/package.json +111 -0
- package/scripts/generate-docs.js +6 -0
- package/src/cno-assert.js +314 -0
- package/src/cno-noop.js +75 -0
- package/src/cno-options.js +108 -0
- package/src/cno-time.js +348 -0
- package/src/cno-utility.js +151 -0
- package/src/lib.js +173 -0
- package/src/lib.test.js +156 -0
package/docs/src/lib.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
# [lib.js](src/lib.js)
|
|
3
|
+
> `cno-bedrock`: micropackage: a test bed for various small pieces of reusable code.
|
|
4
|
+
|
|
5
|
+
Author: Anadian
|
|
6
|
+
|
|
7
|
+
Code license: MIT
|
|
8
|
+
```
|
|
9
|
+
Copyright 2024 Anadian
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
11
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
12
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
13
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
14
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
15
|
+
conditions:
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
17
|
+
or substantial portions of the Software.
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
19
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
20
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
22
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
23
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
```
|
|
25
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
26
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Functions
|
|
30
|
+
|
|
31
|
+
### isStrictlyNotEqual
|
|
32
|
+
> Returns `true` if the a give property `received` is the same type but not the same value as `invalid`.
|
|
33
|
+
|
|
34
|
+
#### Parametres
|
|
35
|
+
| name | type | description |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
38
|
+
|
|
39
|
+
##### `options` Properties
|
|
40
|
+
| name | type | default | description |
|
|
41
|
+
| --- | --- | --- | --- |
|
|
42
|
+
| noop | boolean | false | Skip primary functionality. |
|
|
43
|
+
| noDefaults | boolean | false | Don't apply static default options. |
|
|
44
|
+
| noDynamic | boolean | false | Don't apply dynamic default options. |
|
|
45
|
+
| received | object | null | The value to be checked against `invalid`. |
|
|
46
|
+
| invalid | object | null | The test condition; same type as that of the expected value in `received` but a different value. |
|
|
47
|
+
| code | string | null | The sort of variable/property being checked, used to give more descriptive error messages; should be either 'ARG', 'PROPERTY', or 'RETURN'. |
|
|
48
|
+
| name | string | null | The name of the variable/property being checked, used to give more descriptive error messages; dynamically defaults to either 'value' or 'type' depending on which part of operation failed. |
|
|
49
|
+
| value | string | null | A string representation of `invalid`, used to give more descriptive error messages; dynamically defaults to `invalid` if not specified. |
|
|
50
|
+
| expectedType | string | null | A string representation of the type expected, used to give more descriptive error messages; dynamically defaults to `typeof(invalid)` if not specified. |
|
|
51
|
+
|
|
52
|
+
#### Returns
|
|
53
|
+
| type | description |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| boolean | Returns `true` if `received` is the same type but not the same value as `invalid`; throws an descriptive error message otherwise. |
|
|
56
|
+
|
|
57
|
+
#### Throws
|
|
58
|
+
| code | type | condition |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| 'ERR_INVALID_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is not provided. |
|
|
61
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'ARG'. |
|
|
62
|
+
| 'ERR_INVALID_PROPERTY_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'PROPERTY'. |
|
|
63
|
+
| 'ERR_INVALID_RETURN_TYPE' | TypeError | Thrown if `received` and `invalid` aren't of the same type and `code` is given as 'RETURN'. |
|
|
64
|
+
| 'ERR_INVALID_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is not provided. |
|
|
65
|
+
| 'ERR_INVALID_ARG_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'ARG'. |
|
|
66
|
+
| 'ERR_INVALID_PROPERTY_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'PROPERTY'. |
|
|
67
|
+
| 'ERR_INVALID_RETURN_VALUE' | Error | Thrown if `received` and `invalid` are an equal value and `code` is given as 'RETURN'. |
|
|
68
|
+
|
|
69
|
+
#### History
|
|
70
|
+
| version | change |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| 0.0.1 | WIP |
|
|
73
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
# [lib.test.js](src/lib.test.js)
|
|
3
|
+
> Simple practical test for CNO-Bedrock.
|
|
4
|
+
|
|
5
|
+
Author: Anadian
|
|
6
|
+
|
|
7
|
+
Code license: MIT
|
|
8
|
+
```
|
|
9
|
+
Copyright 2024 Anadian
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
11
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
12
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
13
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
14
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
15
|
+
conditions:
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
17
|
+
or substantial portions of the Software.
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
19
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
20
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
22
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
23
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
```
|
|
25
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
26
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Functions
|
package/package.json
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cno-bedrock",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Micropackage: a test bed for various small pieces of reusable code.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/Anadian/cno-bedrock.git"
|
|
8
|
+
},
|
|
9
|
+
"author": "Anadian",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/lib.js"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/Anadian/cno-bedrock/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/Anadian/cno-bedrock#readme",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"libre",
|
|
24
|
+
"free",
|
|
25
|
+
"open",
|
|
26
|
+
"mit"
|
|
27
|
+
],
|
|
28
|
+
"standard-version": {
|
|
29
|
+
"types": [
|
|
30
|
+
{
|
|
31
|
+
"type": "build",
|
|
32
|
+
"section": "Build",
|
|
33
|
+
"hidden": false
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"type": "ci",
|
|
37
|
+
"section": "Continuous Integration",
|
|
38
|
+
"hidden": false
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"type": "chore",
|
|
42
|
+
"section": "Chores",
|
|
43
|
+
"hidden": false
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"type": "docs",
|
|
47
|
+
"section": "Documentation",
|
|
48
|
+
"hidden": false
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"type": "feat",
|
|
52
|
+
"section": "Features",
|
|
53
|
+
"hidden": false
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"type": "fix",
|
|
57
|
+
"section": "Bug Fixes",
|
|
58
|
+
"hidden": false
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"type": "perf",
|
|
62
|
+
"section": "Performance",
|
|
63
|
+
"hidden": false
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"type": "refactor",
|
|
67
|
+
"section": "Refactoring",
|
|
68
|
+
"hidden": false
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"type": "style",
|
|
72
|
+
"section": "Code Style",
|
|
73
|
+
"hidden": false
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"type": "test",
|
|
77
|
+
"section": "Tests",
|
|
78
|
+
"hidden": false
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
"bin": {
|
|
83
|
+
"cno-bedrock": "./src/cli.js"
|
|
84
|
+
},
|
|
85
|
+
"devDependencies": {
|
|
86
|
+
"c8": "^10.1.3",
|
|
87
|
+
"eslint": "^9.24.0",
|
|
88
|
+
"extract-documentation-comments": "^0.4.2",
|
|
89
|
+
"hjson": "^3.2.2",
|
|
90
|
+
"npm-check-updates": "^17.1.18",
|
|
91
|
+
"shelljs": "^0.10.0",
|
|
92
|
+
"standard-version": "^9.5.0"
|
|
93
|
+
},
|
|
94
|
+
"dependencies": {
|
|
95
|
+
"lodash": "^4.17.21",
|
|
96
|
+
"mathjs": "^14.5.2",
|
|
97
|
+
"nanoid": "^5.1.5"
|
|
98
|
+
},
|
|
99
|
+
"scripts": {
|
|
100
|
+
"test": "node --test ./src/lib.test.js",
|
|
101
|
+
"coverage": "c8 pnpm test",
|
|
102
|
+
"coverage-report": "c8 report -r=text-lcov > coverage/lcov.txt",
|
|
103
|
+
"do-ci": "pnpm coverage && pnpm coverage-report",
|
|
104
|
+
"lint": "eslint ./src/main.js",
|
|
105
|
+
"generate-docs": "scripts/generate-docs.js",
|
|
106
|
+
"update-config": "hjson -j ci/github-actions.hjson | json2yaml -o .github/workflows/ci.yml && git change 'chore: Updated GitHub Actions.'",
|
|
107
|
+
"update-deps": "npm-check-updates -u",
|
|
108
|
+
"release": "standard-version",
|
|
109
|
+
"publish-release": "git push --follow-tags origin main && pnpm publish"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import Sh from 'shelljs';
|
|
3
|
+
Sh.cmd( 'pnpm', 'extract-documentation-comments', '-I', 'src/*.js', '-O', 'docs' );
|
|
4
|
+
Sh.ls( 'docs/src/' ).forEach( ( basename ) => {
|
|
5
|
+
Sh.mv( `docs/src/${basename}`, `docs/src/${basename.replace('.js','.md')}` );
|
|
6
|
+
} );
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-assert.js](src/cno-assert.js)
|
|
4
|
+
> Custom assert functions.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import { annotateThis } from './cno-utility.js'
|
|
34
|
+
//## Standard
|
|
35
|
+
import AssertNS from 'node:assert/strict';
|
|
36
|
+
//## External
|
|
37
|
+
import _ from 'lodash';
|
|
38
|
+
//# Constants
|
|
39
|
+
const FILENAME = 'cno-assert.js';
|
|
40
|
+
//## Errors
|
|
41
|
+
|
|
42
|
+
//# Global Variables
|
|
43
|
+
/**## Functions*/
|
|
44
|
+
/**
|
|
45
|
+
### assertSameType
|
|
46
|
+
> Throws if actual's type is not the same as expected's, according to a strict `typeof` comparison.
|
|
47
|
+
|
|
48
|
+
#### Parametres
|
|
49
|
+
| name | type | description |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| actual | any | The actual value. |
|
|
52
|
+
| expected | any | The expected value. |
|
|
53
|
+
| message | string | A custom message for the assertion. \[default: ''\] |
|
|
54
|
+
|
|
55
|
+
#### Throws
|
|
56
|
+
| code | type | condition |
|
|
57
|
+
| --- | --- | --- |
|
|
58
|
+
| 'ERR_ASSERTION_SAMETYPE' | AssertionError | Thrown if `typeof(actual) !== typeof(expected)`. |
|
|
59
|
+
|
|
60
|
+
#### History
|
|
61
|
+
| version | change |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| 0.0.1 | WIP |
|
|
64
|
+
*/
|
|
65
|
+
function assertSameType( actual, expected, message = '' ){
|
|
66
|
+
// Constants
|
|
67
|
+
const FUNCTION_NAME = 'assertSameType';
|
|
68
|
+
// Variables
|
|
69
|
+
var return_error = null;
|
|
70
|
+
// Function
|
|
71
|
+
if( typeof(actual) !== typeof(expected) ){
|
|
72
|
+
message ??= `Type of 'actual' (${typeof(actual)}) is not the same type as 'expected' (${typeof(expected)}).`;
|
|
73
|
+
return_error = new AssertNS.AssertionError( { message: message, actual: actual, expected: expected, operator: 'SameType' } );
|
|
74
|
+
return_error.code += '_SAMETYPE';
|
|
75
|
+
annotateThis.call( return_error );
|
|
76
|
+
throw return_error;
|
|
77
|
+
}
|
|
78
|
+
} // assertSameType
|
|
79
|
+
/**
|
|
80
|
+
### assertNonstrictNotEqual
|
|
81
|
+
> Throws if actual and expected can be coerced to equal one another.
|
|
82
|
+
|
|
83
|
+
#### Parametres
|
|
84
|
+
| name | type | description |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| actual | any | The actual value. |
|
|
87
|
+
| expected | any | The expected value. |
|
|
88
|
+
| message, | string | A custom message for the assertion. \[default: ''\] |
|
|
89
|
+
|
|
90
|
+
#### Throws
|
|
91
|
+
| code | type | condition |
|
|
92
|
+
| --- | --- | --- |
|
|
93
|
+
| 'ERR_ASSERTION_NONSTRICTNOTEQUAL' | AssertionError | Thrown if `actual == expected`. |
|
|
94
|
+
|
|
95
|
+
#### History
|
|
96
|
+
| version | change |
|
|
97
|
+
| --- | --- |
|
|
98
|
+
| 0.0.1 | WIP |
|
|
99
|
+
*/
|
|
100
|
+
function assertNonstrictNotEqual( actual, expected, message = '' ){
|
|
101
|
+
// Constants
|
|
102
|
+
const FUNCTION_NAME = 'assertNonstrictNotEqual';
|
|
103
|
+
// Variables
|
|
104
|
+
var return_error = null;
|
|
105
|
+
// Function
|
|
106
|
+
if( actual == expected ){
|
|
107
|
+
message ??= `Value of 'actual' (${actual}) is equal to 'expected' (${expected}).`;
|
|
108
|
+
return_error = new AssertNS.AssertionError( { message: message, actual: actual, expected: expected, operator: 'NonstrictNotEqual' } );
|
|
109
|
+
return_error.code += '_NONSTRICTNOTEQUAL';
|
|
110
|
+
annotateThis.call( return_error );
|
|
111
|
+
throw return_error;
|
|
112
|
+
}
|
|
113
|
+
} // assertNonstrictNotEqual
|
|
114
|
+
/**
|
|
115
|
+
### assertStrictlyNotEqual
|
|
116
|
+
> Throws if actual is not the same type as expected or is the value as expected.
|
|
117
|
+
|
|
118
|
+
#### Parametres
|
|
119
|
+
| name | type | description |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| actual | any | The actual value. |
|
|
122
|
+
| expected | any | The expected/invalid value. |
|
|
123
|
+
| name | string | The name of variable as a string, for a more informative auto message. \[default: ''\] |
|
|
124
|
+
| value | string | The expected value as a string, for a more informative auto message. \[default: ''\] |
|
|
125
|
+
| expectedType | string | The expected's type as a string, for a more informative auto message. \[default: ''\] |
|
|
126
|
+
| message | string | A custom message for the assertion. \[default: ''\] |
|
|
127
|
+
|
|
128
|
+
#### Throws
|
|
129
|
+
| code | type | condition |
|
|
130
|
+
| --- | --- | --- |
|
|
131
|
+
| 'ERR_ASSERTION_STRICTLYNOTEQUAL' | AssertionError | Thrown if actual is not strictly-not-equal (`=!=`) to expected. |
|
|
132
|
+
|
|
133
|
+
#### History
|
|
134
|
+
| version | change |
|
|
135
|
+
| --- | --- |
|
|
136
|
+
| 0.0.1 | WIP |
|
|
137
|
+
*/
|
|
138
|
+
function assertStrictlyNotEqual( actual, expected, name = '', value = '', expectedType = '', message = '' ){
|
|
139
|
+
// Constants
|
|
140
|
+
const FUNCTION_NAME = 'assertStrictlyNotEqual';
|
|
141
|
+
// Variables
|
|
142
|
+
var return_error = null;
|
|
143
|
+
var cause_error = null;
|
|
144
|
+
// Function
|
|
145
|
+
try{
|
|
146
|
+
assertSameType( actual, expected );
|
|
147
|
+
try{
|
|
148
|
+
assertNonstrictNotEqual( actual, expected );
|
|
149
|
+
} catch( error ){
|
|
150
|
+
cause_error = error;
|
|
151
|
+
message ??= `${name ?? 'Value'} must not be equal to ${value ?? expected}.`;
|
|
152
|
+
}
|
|
153
|
+
} catch( error ){
|
|
154
|
+
cause_error = error;
|
|
155
|
+
message ??= `${name ?? 'Type of \'actual\''} must be the same type as ${value ?? expected} (${expectedType ?? typeof(expected)}).`;
|
|
156
|
+
}
|
|
157
|
+
if( cause_error ){
|
|
158
|
+
return_error = new AssertNS.AssertionError( { message: message, actual: actual, expected: expected, operator: 'StrictlyNotEqual' } );
|
|
159
|
+
return_error.code += '_STRICTLYNOTEQUAL';
|
|
160
|
+
return_error.cause = cause_error;
|
|
161
|
+
annotateThis.call( return_error );
|
|
162
|
+
throw return_error;
|
|
163
|
+
}
|
|
164
|
+
} // assertStrictlyNotEqual
|
|
165
|
+
/**
|
|
166
|
+
### assertInstanceof
|
|
167
|
+
> Throws if actual is not an instance of (via `instanceof`) of expected.
|
|
168
|
+
|
|
169
|
+
#### Parametres
|
|
170
|
+
| name | type | description |
|
|
171
|
+
| --- | --- | --- |
|
|
172
|
+
| actual | any | The actual value. |
|
|
173
|
+
| expected | any | The expected value. |
|
|
174
|
+
| message | string | A custom message for the assertion. \[default: ''\] |
|
|
175
|
+
|
|
176
|
+
#### Throws
|
|
177
|
+
| code | type | condition |
|
|
178
|
+
| --- | --- | --- |
|
|
179
|
+
| 'ERR_ASSERTION_INSTANCEOF' | AssertionError | Thrown if actual is not an instance of expected. |
|
|
180
|
+
|
|
181
|
+
#### History
|
|
182
|
+
| version | change |
|
|
183
|
+
| --- | --- |
|
|
184
|
+
| 0.0.1 | WIP |
|
|
185
|
+
*/
|
|
186
|
+
function assertInstanceof( actual, expected, message = '' ){
|
|
187
|
+
// Constants
|
|
188
|
+
const FUNCTION_NAME = 'assertInstanceof';
|
|
189
|
+
// Variables
|
|
190
|
+
var return_error = null;
|
|
191
|
+
// Function
|
|
192
|
+
if( !( actual instanceof expected ) ){
|
|
193
|
+
message ??= `${variable_name ?? 'Actual'} is not an instance of ${constructor_name ?? 'expected'}.`;
|
|
194
|
+
return_error = new AssertNS.AssertionError( { message: message, actual: actual, expected: expected, operator: 'Instanceof' } );
|
|
195
|
+
return_error.code += '_INSTANCEOF';
|
|
196
|
+
annotateThis.call( return_error );
|
|
197
|
+
throw return_error;
|
|
198
|
+
}
|
|
199
|
+
} // assertInstanceof
|
|
200
|
+
/**
|
|
201
|
+
### assertExpectedError
|
|
202
|
+
> Throws if the actual error doesn't match the the object describe in expected.
|
|
203
|
+
|
|
204
|
+
#### Parametres
|
|
205
|
+
| name | type | description |
|
|
206
|
+
| --- | --- | --- |
|
|
207
|
+
| actual | any | The actual error. |
|
|
208
|
+
| expected | any | An object with a the properties 'constructor' with the constructor function used to create the error and 'code' the expected code property of the error. |
|
|
209
|
+
| message | string | A custom message for the assertion. \[default: ''\] |
|
|
210
|
+
|
|
211
|
+
#### Throws
|
|
212
|
+
| code | type | condition |
|
|
213
|
+
| --- | --- | --- |
|
|
214
|
+
| 'ERR_ASSERTION_EXPECTEDERROR' | AssertionError | Thrown if actual doesn't match the error described by expected. |
|
|
215
|
+
|
|
216
|
+
#### History
|
|
217
|
+
| version | change |
|
|
218
|
+
| --- | --- |
|
|
219
|
+
| 0.0.1 | WIP |
|
|
220
|
+
*/
|
|
221
|
+
function assertExpectedError( actual, expected, message = '' ){
|
|
222
|
+
// Constants
|
|
223
|
+
const FUNCTION_NAME = 'assertExpectedError';
|
|
224
|
+
// Variables
|
|
225
|
+
var return_error = null;
|
|
226
|
+
var cause_error = null;
|
|
227
|
+
// Function
|
|
228
|
+
try{
|
|
229
|
+
assertInstanceof( actual, expected.constructor, 'Error \'actual\' ', expected.constructor_string ?? 'expected.constructor' );
|
|
230
|
+
try{
|
|
231
|
+
AssertNS.strictEqual( actual.code, expected.code );
|
|
232
|
+
} catch( error ){
|
|
233
|
+
cause_error = error;
|
|
234
|
+
}
|
|
235
|
+
} catch( error ){
|
|
236
|
+
cause_error = error;
|
|
237
|
+
}
|
|
238
|
+
if( cause_error ){
|
|
239
|
+
return_error = new AssertNS.AssertionError( { message: `'actual' didn't match the error specified by the 'expected'. Cause: ${cause_error.message}`, actual: actual, expected: expected, operator: 'ExpectedError' } );
|
|
240
|
+
return_error.code += '_EXPECTEDERROR';
|
|
241
|
+
return_error.cause = cause_error;
|
|
242
|
+
annotateThis.call( return_error );
|
|
243
|
+
throw return_error;
|
|
244
|
+
}
|
|
245
|
+
} // assertExpectedError
|
|
246
|
+
/**
|
|
247
|
+
### assertObjectsQuantitativelyEqual
|
|
248
|
+
> Throws if acutal doesn't have the same properties and values as expected. Compared via [lodash's `_.isEqual`](https://lodash.com/docs/4.17.15#isEqual).
|
|
249
|
+
|
|
250
|
+
#### Parametres
|
|
251
|
+
| name | type | description |
|
|
252
|
+
| --- | --- | --- |
|
|
253
|
+
| actual | any | The actual object. |
|
|
254
|
+
| expected | any | The expected object. |
|
|
255
|
+
| message | string | A custom message for the assertion. \[default: ''\] |
|
|
256
|
+
|
|
257
|
+
#### Throws
|
|
258
|
+
| code | type | condition |
|
|
259
|
+
| --- | --- | --- |
|
|
260
|
+
| 'ERR_ASSERTION_OBJECTSQUANTITATIVELYEQUAL' | AssertionError | Thrown if `_.isEqual( actual, expected )` is not true. |
|
|
261
|
+
|
|
262
|
+
#### History
|
|
263
|
+
| version | change |
|
|
264
|
+
| --- | --- |
|
|
265
|
+
| 0.0.1 | WIP |
|
|
266
|
+
*/
|
|
267
|
+
function assertObjectsQuantitativelyEqual( actual, expected, message = '' ){
|
|
268
|
+
|
|
269
|
+
// Constants
|
|
270
|
+
const FUNCTION_NAME = 'assertObjectsQuantitativelyEqual';
|
|
271
|
+
// Variables
|
|
272
|
+
var return_error = null;
|
|
273
|
+
// Function
|
|
274
|
+
if( !_.isEqual( actual, expected ) ){
|
|
275
|
+
message ??= `'actual' object (${inspThis.call( actual )}) doesn't strictly match 'expected' object (${inspThis.call( expected )}).`;
|
|
276
|
+
return_error = new AssertNS.AssertionError( { message: message, actual: actual, expected: expected, operator: 'ObjectsQuantitativelyEqual' } );
|
|
277
|
+
return_error.code += '_OBJECTSQUANTITAIVELYEQUAL';
|
|
278
|
+
annotateThis.call( return_error );
|
|
279
|
+
throw return_error;
|
|
280
|
+
}
|
|
281
|
+
} // assertObjectsQuantitativelyEqual
|
|
282
|
+
|
|
283
|
+
const NAMESPACE = { ...AssertNS };
|
|
284
|
+
Object.defineProperties( NAMESPACE, {
|
|
285
|
+
assertSameType: {
|
|
286
|
+
value: assertSameType,
|
|
287
|
+
enumerable: true
|
|
288
|
+
},
|
|
289
|
+
assertNonstrictNotEqual: {
|
|
290
|
+
value: assertNonstrictNotEqual,
|
|
291
|
+
enumerable: true
|
|
292
|
+
},
|
|
293
|
+
assertStrictlyNotEqual: {
|
|
294
|
+
value: assertStrictlyNotEqual,
|
|
295
|
+
enumerable: true
|
|
296
|
+
},
|
|
297
|
+
assertInstanceof: {
|
|
298
|
+
value: assertInstanceof,
|
|
299
|
+
enumerable: true
|
|
300
|
+
},
|
|
301
|
+
assertExpectedError: {
|
|
302
|
+
value: assertExpectedError,
|
|
303
|
+
enumerable: true
|
|
304
|
+
},
|
|
305
|
+
assertObjectsQuantitativelyEqual: {
|
|
306
|
+
value: assertObjectsQuantitativelyEqual,
|
|
307
|
+
enumerable: true
|
|
308
|
+
}
|
|
309
|
+
} );
|
|
310
|
+
|
|
311
|
+
export default NAMESPACE;
|
|
312
|
+
|
|
313
|
+
// cno-assert.js EOF
|
|
314
|
+
|
package/src/cno-noop.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-noop.js](src/cno-noop.js)
|
|
4
|
+
> NOOP functions.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
//## Standard
|
|
34
|
+
//## External
|
|
35
|
+
import _ from 'lodash';
|
|
36
|
+
//# Constants
|
|
37
|
+
const FILENAME = 'cno-noop.js';
|
|
38
|
+
const { noop } = _;
|
|
39
|
+
//## Errors
|
|
40
|
+
|
|
41
|
+
//# Global Variables
|
|
42
|
+
/**## Functions*/
|
|
43
|
+
function returnTrue(){
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
function returnFalse(){
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
function returnNull(){
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const NAMESPACE = {};
|
|
53
|
+
Object.defineProperties( NAMESPACE, {
|
|
54
|
+
returnTrue: {
|
|
55
|
+
value: returnTrue,
|
|
56
|
+
enumerable: true
|
|
57
|
+
},
|
|
58
|
+
returnFalse: {
|
|
59
|
+
value: returnFalse,
|
|
60
|
+
enumerable: true
|
|
61
|
+
},
|
|
62
|
+
returnNull: {
|
|
63
|
+
value: returnNull,
|
|
64
|
+
enumerable: true
|
|
65
|
+
},
|
|
66
|
+
returnUndefined: {
|
|
67
|
+
value: noop,
|
|
68
|
+
enumerable: true
|
|
69
|
+
}
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
export { NAMESPACE as default, returnTrue, returnFalse, returnNull, noop as returnUndefined };
|
|
73
|
+
|
|
74
|
+
// cno-noop.js EOF
|
|
75
|
+
|