@tsdotnet/reflection 1.1.11 → 1.1.13
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/LICENSE +21 -21
- package/README.md +93 -94
- package/dist/cjs/TypeValidator.js +4 -4
- package/dist/cjs/TypeValidator.js.map +1 -1
- package/dist/types/TypeValidator.d.ts +1 -1
- package/package.json +32 -50
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 electricessence (Oren F.)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 electricessence (Oren F.)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,94 +1,93 @@
|
|
|
1
|
-
#  tsdotnet / reflection
|
|
2
|
-
|
|
3
|
-
[](https://github.com/tsdotnet/reflection/blob/master/LICENSE)
|
|
4
|
-
 tsdotnet / reflection
|
|
2
|
+
|
|
3
|
+
[](https://github.com/tsdotnet/reflection/blob/master/LICENSE)
|
|
4
|
+
[](https://www.npmjs.com/package/@tsdotnet/reflection)
|
|
5
|
+
|
|
6
|
+
A set of classes and utilities for JavaScript type inspection and validation.
|
|
7
|
+
|
|
8
|
+
## Docs
|
|
9
|
+
|
|
10
|
+
[tsdotnet.github.io/reflection](https://tsdotnet.github.io/reflection/)
|
|
11
|
+
|
|
12
|
+
## `TypeValidator<T>`
|
|
13
|
+
|
|
14
|
+
### Benefits
|
|
15
|
+
|
|
16
|
+
- Allows for run-time validation and checking of dynamic types as well as integrated **type-guarding**.
|
|
17
|
+
- Super easy to use, basically only requires a copy paste.
|
|
18
|
+
- Works with literals!
|
|
19
|
+
|
|
20
|
+
### Usage
|
|
21
|
+
|
|
22
|
+
#### Step 1: Declare the expected type/interface.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
interface MyType
|
|
26
|
+
{
|
|
27
|
+
a: object;
|
|
28
|
+
b: string;
|
|
29
|
+
c: number;
|
|
30
|
+
d: boolean;
|
|
31
|
+
e: {
|
|
32
|
+
f: string;
|
|
33
|
+
g: boolean;
|
|
34
|
+
h: [
|
|
35
|
+
number,
|
|
36
|
+
boolean,
|
|
37
|
+
string
|
|
38
|
+
];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Step 2: Copy the interface as an actual object and `<type>` the validator
|
|
44
|
+
|
|
45
|
+
The following can be done with pure JavaScript and still work.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const myTypeValidator = new TypeValidator<MyType>(
|
|
49
|
+
{
|
|
50
|
+
a: Object,
|
|
51
|
+
b: String,
|
|
52
|
+
c: Number,
|
|
53
|
+
d: Boolean,
|
|
54
|
+
e: {
|
|
55
|
+
f: String,
|
|
56
|
+
g: Boolean,
|
|
57
|
+
h: [
|
|
58
|
+
Number,
|
|
59
|
+
Boolean,
|
|
60
|
+
String
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Step 3: validate as many times as you want:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const myItem = {
|
|
70
|
+
a: {},
|
|
71
|
+
b: 'hello',
|
|
72
|
+
c: 1,
|
|
73
|
+
d: true,
|
|
74
|
+
e: {
|
|
75
|
+
f: 'whatever',
|
|
76
|
+
g: false,
|
|
77
|
+
h: [
|
|
78
|
+
0,
|
|
79
|
+
true,
|
|
80
|
+
'2'
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
i: 'noise'
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// no compile-time type errors!
|
|
87
|
+
if (MyTypeValidator.isSubsetOf(myItem)) {
|
|
88
|
+
console.log(myItem.e.h.length); // 3
|
|
89
|
+
console.log(myItem.b); // "hello"
|
|
90
|
+
} else {
|
|
91
|
+
throw new TypeError('Invalid type!');
|
|
92
|
+
}
|
|
93
|
+
```
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.TypeInfoHelper = exports.TypeInfo = exports.typeInfo = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
|
-
const
|
|
10
|
-
exports.typeInfo =
|
|
11
|
-
Object.defineProperty(exports, "TypeInfo", { enumerable: true, get: function () { return
|
|
9
|
+
const TypeInfo_js_1 = tslib_1.__importStar(require("./TypeInfo.js"));
|
|
10
|
+
exports.typeInfo = TypeInfo_js_1.default;
|
|
11
|
+
Object.defineProperty(exports, "TypeInfo", { enumerable: true, get: function () { return TypeInfo_js_1.TypeInfo; } });
|
|
12
12
|
const compare_1 = require("@tsdotnet/compare");
|
|
13
|
-
class TypeInfoHelper extends
|
|
13
|
+
class TypeInfoHelper extends TypeInfo_js_1.TypeInfo {
|
|
14
14
|
contains(descriptor) {
|
|
15
15
|
const value = this.target;
|
|
16
16
|
if (value === descriptor)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeValidator.js","sourceRoot":"","sources":["../../src/TypeValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;AAEH
|
|
1
|
+
{"version":3,"file":"TypeValidator.js","sourceRoot":"","sources":["../../src/TypeValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;AAEH,qEAAiD;AAGzC,mBAHD,qBAAQ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AAC;IACzC,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @author electricessence / https://github.com/electricessence/
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
|
-
import typeInfo, { TypeInfo } from './TypeInfo';
|
|
5
|
+
import typeInfo, { TypeInfo } from './TypeInfo.js';
|
|
6
6
|
export { typeInfo, TypeInfo };
|
|
7
7
|
export declare class TypeInfoHelper extends TypeInfo {
|
|
8
8
|
contains<TDescriptor>(descriptor: unknown): this is TDescriptor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsdotnet/reflection",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.13",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "electricessence",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,23 +20,6 @@
|
|
|
20
20
|
"runtime type",
|
|
21
21
|
"runtime typechecking"
|
|
22
22
|
],
|
|
23
|
-
"scripts": {
|
|
24
|
-
"clean": "rimraf dist",
|
|
25
|
-
"build:types": "cpy \"src/**/*.d.ts\" dist/types --parents && tsc -p tsconfig.types.json",
|
|
26
|
-
"build:esm": "rollup -c rollup.config.js",
|
|
27
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
28
|
-
"build": "pnpm clean && pnpm build:types && pnpm build:esm && pnpm build:cjs && pnpm typecheck:tests",
|
|
29
|
-
"test": "vitest run",
|
|
30
|
-
"typecheck": "tsc --noEmit",
|
|
31
|
-
"typecheck:tests": "tsc -p tests/tsconfig.json --noEmit",
|
|
32
|
-
"lint": "eslint src/**/*.ts",
|
|
33
|
-
"docs": "rimraf docs && typedoc --options typedoc.json --readme none",
|
|
34
|
-
"bump": "pnpm run precommit && pnpm version patch",
|
|
35
|
-
"precommit": "pnpm build && pnpm test && pnpm lint && git status -s",
|
|
36
|
-
"preversion": "pnpm lint && pnpm test",
|
|
37
|
-
"postversion": "git push && git push --tags && pnpm run docs && git add -A && git commit -m \"Updated docs.\" docs && git push",
|
|
38
|
-
"prepublishOnly": "pnpm build && pnpm test && pnpm lint"
|
|
39
|
-
},
|
|
40
23
|
"repository": {
|
|
41
24
|
"type": "git",
|
|
42
25
|
"url": "git+https://github.com/tsdotnet/reflection.git"
|
|
@@ -46,41 +29,40 @@
|
|
|
46
29
|
},
|
|
47
30
|
"homepage": "https://github.com/tsdotnet/reflection#readme",
|
|
48
31
|
"devDependencies": {
|
|
49
|
-
"@eslint/js": "^
|
|
50
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
51
|
-
"@rollup/plugin-typescript": "^12.
|
|
52
|
-
"@types/node": "^
|
|
53
|
-
"@vitest/coverage-v8": "^
|
|
54
|
-
"cpy-cli": "^
|
|
55
|
-
"eslint": "^
|
|
56
|
-
"rimraf": "^6.
|
|
57
|
-
"rollup": "^4.
|
|
58
|
-
"typedoc": "^0.28.
|
|
59
|
-
"typescript": "^5.9.
|
|
60
|
-
"typescript-eslint": "^8.
|
|
61
|
-
"vitest": "^
|
|
32
|
+
"@eslint/js": "^10.0.1",
|
|
33
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
34
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
35
|
+
"@types/node": "^25.5.0",
|
|
36
|
+
"@vitest/coverage-v8": "^4.1.0",
|
|
37
|
+
"cpy-cli": "^7.0.0",
|
|
38
|
+
"eslint": "^10.0.3",
|
|
39
|
+
"rimraf": "^6.1.3",
|
|
40
|
+
"rollup": "^4.59.0",
|
|
41
|
+
"typedoc": "^0.28.17",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.57.1",
|
|
44
|
+
"vitest": "^4.1.0"
|
|
62
45
|
},
|
|
63
46
|
"dependencies": {
|
|
64
|
-
"@tsdotnet/compare": "^1.7.
|
|
47
|
+
"@tsdotnet/compare": "^1.7.4",
|
|
65
48
|
"@tsdotnet/type": "^1.2.7",
|
|
66
49
|
"tslib": "^2.8.1"
|
|
67
50
|
},
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
51
|
+
"scripts": {
|
|
52
|
+
"clean": "rimraf dist",
|
|
53
|
+
"copy:dts": "cpy \"src/**/*.d.ts\" dist/types --parents || echo No .d.ts files to copy",
|
|
54
|
+
"build:types": "pnpm copy:dts && tsc -p tsconfig.types.json",
|
|
55
|
+
"build:esm": "rollup -c rollup.config.js",
|
|
56
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
57
|
+
"build": "pnpm clean && pnpm build:types && pnpm build:esm && pnpm build:cjs && pnpm typecheck:tests",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"typecheck": "tsc --noEmit",
|
|
60
|
+
"typecheck:tests": "tsc -p tests/tsconfig.json --noEmit",
|
|
61
|
+
"lint": "eslint src/**/*.ts",
|
|
62
|
+
"docs": "rimraf docs && typedoc --options typedoc.json --readme none",
|
|
63
|
+
"bump": "pnpm run precommit && pnpm version patch",
|
|
64
|
+
"precommit": "pnpm build && pnpm test && pnpm lint && git status -s",
|
|
65
|
+
"preversion": "pnpm lint && pnpm test",
|
|
66
|
+
"postversion": "git push && git push --tags && pnpm run docs && git add -A && git commit -m \"Updated docs.\" docs && git push"
|
|
85
67
|
}
|
|
86
|
-
}
|
|
68
|
+
}
|