@tsdotnet/reflection 1.1.2 → 1.1.4
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/cjs/TypeInfo.js +0 -34
- package/dist/cjs/TypeInfo.js.map +1 -1
- package/dist/cjs/TypeValidator.js +0 -28
- package/dist/cjs/TypeValidator.js.map +1 -1
- package/dist/esm/TypeInfo.js +7 -38
- package/dist/esm/TypeInfo.js.map +1 -1
- package/dist/esm/TypeValidator.js +9 -34
- package/dist/esm/TypeValidator.js.map +1 -1
- package/dist/{TypeInfo.d.ts → types/TypeInfo.d.ts} +0 -32
- package/dist/types/TypeValidator.d.ts +14 -0
- package/package.json +27 -35
- package/rollup.config.js +37 -0
- package/vitest.config.ts +22 -0
- package/dist/TypeValidator.d.ts +0 -36
- package/dist/umd/TypeInfo.js +0 -155
- package/dist/umd/TypeInfo.js.map +0 -1
- package/dist/umd/TypeValidator.js +0 -119
- package/dist/umd/TypeValidator.js.map +0 -1
package/dist/cjs/TypeInfo.js
CHANGED
|
@@ -8,14 +8,9 @@ exports.TypeInfo = void 0;
|
|
|
8
8
|
exports.default = typeInfo;
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
10
|
const type_1 = tslib_1.__importDefault(require("@tsdotnet/type"));
|
|
11
|
-
// Only used for primitives.
|
|
12
11
|
const typeInfoRegistry = {};
|
|
13
12
|
const VOID0 = void 0;
|
|
14
|
-
/**
|
|
15
|
-
* Exposes easy access to type information including inquiring about members.
|
|
16
|
-
*/
|
|
17
13
|
class TypeInfo {
|
|
18
|
-
// noinspection DuplicatedCode
|
|
19
14
|
constructor(target) {
|
|
20
15
|
this.target = target;
|
|
21
16
|
this.isBoolean = false;
|
|
@@ -77,43 +72,20 @@ class TypeInfo {
|
|
|
77
72
|
}
|
|
78
73
|
Object.freeze(this);
|
|
79
74
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Returns a TypeInfo for any target object.
|
|
82
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
83
|
-
* @param target
|
|
84
|
-
* @returns {TypeInfo}
|
|
85
|
-
*/
|
|
86
75
|
static for(target) {
|
|
87
76
|
return typeInfo(target);
|
|
88
77
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Returns a TypeInfo for any member or non-member,
|
|
91
|
-
* where non-members are of type undefined.
|
|
92
|
-
* @param name
|
|
93
|
-
* @returns {TypeInfo}
|
|
94
|
-
*/
|
|
95
78
|
member(name) {
|
|
96
79
|
const t = this.target;
|
|
97
80
|
const m = !this.isPrimitive && t && name in t ? t[name] : VOID0;
|
|
98
81
|
return typeInfo(m);
|
|
99
82
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Returns true if the target matches the type (instanceof).
|
|
102
|
-
* @param type
|
|
103
|
-
* @returns {boolean}
|
|
104
|
-
*/
|
|
105
83
|
is(type) {
|
|
106
84
|
const t = this.target;
|
|
107
85
|
if (t == null)
|
|
108
86
|
throw '\'type\' is null or undefined';
|
|
109
87
|
return t instanceof type;
|
|
110
88
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Returns null if the target does not match the type (instanceof).
|
|
113
|
-
* Otherwise returns the target as the type.
|
|
114
|
-
* @param type
|
|
115
|
-
* @returns {T|null}
|
|
116
|
-
*/
|
|
117
89
|
as(type) {
|
|
118
90
|
const t = this.target;
|
|
119
91
|
if (t == null)
|
|
@@ -122,12 +94,6 @@ class TypeInfo {
|
|
|
122
94
|
}
|
|
123
95
|
}
|
|
124
96
|
exports.TypeInfo = TypeInfo;
|
|
125
|
-
/**
|
|
126
|
-
* Returns a TypeInfo for any target object.
|
|
127
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
128
|
-
* @param target
|
|
129
|
-
* @returns {TypeInfo}
|
|
130
|
-
*/
|
|
131
97
|
function typeInfo(target) {
|
|
132
98
|
const t = target === null ? 'null' : typeof target;
|
|
133
99
|
switch (t) {
|
package/dist/cjs/TypeInfo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeInfo.js","sourceRoot":"","sources":["../../src/TypeInfo.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA6IH,2BAWC;;AAtJD,kEAAkC;
|
|
1
|
+
{"version":3,"file":"TypeInfo.js","sourceRoot":"","sources":["../../src/TypeInfo.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA6IH,2BAWC;;AAtJD,kEAAkC;AAGlC,MAAM,gBAAgB,GAAgC,EAAE,CAAC;AACzD,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC;AAKrB,MAAa,QAAQ;IAqBpB,YAAgC,MAAW;QAAX,WAAM,GAAN,MAAM,CAAK;QAlBlC,cAAS,GAAY,KAAK,CAAC;QAC3B,aAAQ,GAAY,KAAK,CAAC;QAC1B,aAAQ,GAAY,KAAK,CAAC;QAC1B,kBAAa,GAAY,KAAK,CAAC;QAC/B,aAAQ,GAAY,KAAK,CAAC;QAC1B,cAAS,GAAY,KAAK,CAAC;QAC3B,aAAQ,GAAY,KAAK,CAAC;QAC1B,YAAO,GAAY,KAAK,CAAC;QACzB,gBAAW,GAAY,KAAK,CAAC;QAC7B,eAAU,GAAY,KAAK,CAAC;QAC5B,gBAAW,GAAY,KAAK,CAAC;QAC7B,WAAM,GAAY,KAAK,CAAC;QACxB,sBAAiB,GAAY,KAAK,CAAC;QACnC,gBAAW,GAAY,KAAK,CAAC;QAC7B,aAAQ,GAAY,KAAK,CAAC;QAMlC,QAAO,CAAC,IAAI,CAAC,IAAI,GAAG,OAAO,MAAM,CAAC,EAClC,CAAC;YACA,KAAK,SAAS;gBACb,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;gBACxB,MAAM;YACP,KAAK,QAAQ;gBACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;gBACrB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;gBAC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C;AACb,CAAC"}
|
|
@@ -10,27 +10,11 @@ const TypeInfo_1 = tslib_1.__importStar(require("./TypeInfo"));
|
|
|
10
10
|
exports.typeInfo = TypeInfo_1.default;
|
|
11
11
|
Object.defineProperty(exports, "TypeInfo", { enumerable: true, get: function () { return TypeInfo_1.TypeInfo; } });
|
|
12
12
|
const compare_1 = require("@tsdotnet/compare");
|
|
13
|
-
/**
|
|
14
|
-
* A descriptor is simply a JSON tree that either has an actual value or a type that identifies what the expect type should be at that leaf in the tree.
|
|
15
|
-
*
|
|
16
|
-
* var descriptor = {
|
|
17
|
-
* a : Object,
|
|
18
|
-
* b : String,
|
|
19
|
-
* c : {
|
|
20
|
-
* d : true ,
|
|
21
|
-
* e : Array,
|
|
22
|
-
* f : []
|
|
23
|
-
* },
|
|
24
|
-
* g : "literal"
|
|
25
|
-
* }
|
|
26
|
-
*/
|
|
27
13
|
class TypeInfoHelper extends TypeInfo_1.TypeInfo {
|
|
28
14
|
contains(descriptor) {
|
|
29
15
|
const value = this.target;
|
|
30
|
-
// works with literals and values.
|
|
31
16
|
if (value === descriptor)
|
|
32
17
|
return true;
|
|
33
|
-
// First check against simple types.
|
|
34
18
|
switch (descriptor) {
|
|
35
19
|
case Function:
|
|
36
20
|
return this.isFunction;
|
|
@@ -47,7 +31,6 @@ class TypeInfoHelper extends TypeInfo_1.TypeInfo {
|
|
|
47
31
|
}
|
|
48
32
|
if (this.type !== typeof descriptor || (this.isPrimitive && !(0, compare_1.areEqual)(value, descriptor)))
|
|
49
33
|
return false;
|
|
50
|
-
// Check array contents and confirm intersections.
|
|
51
34
|
if (this.isArrayLike && descriptor instanceof Array) {
|
|
52
35
|
const vLen = value.length, dLen = descriptor.length;
|
|
53
36
|
if (vLen !== dLen && (isNaN(vLen) || vLen < dLen))
|
|
@@ -61,15 +44,12 @@ class TypeInfoHelper extends TypeInfo_1.TypeInfo {
|
|
|
61
44
|
if (this.isObject) {
|
|
62
45
|
const targetKeys = Object.keys(value);
|
|
63
46
|
const dKeys = Object.keys(descriptor);
|
|
64
|
-
// Quick check...
|
|
65
47
|
if (dKeys.length > targetKeys.length)
|
|
66
48
|
return false;
|
|
67
|
-
// Quick check #2...
|
|
68
49
|
for (const key of dKeys) {
|
|
69
50
|
if (targetKeys.indexOf(key) === -1)
|
|
70
51
|
return false;
|
|
71
52
|
}
|
|
72
|
-
// Final pass with recursive...
|
|
73
53
|
for (const key of dKeys) {
|
|
74
54
|
if (areInvalid(value[key], descriptor[key]))
|
|
75
55
|
return false;
|
|
@@ -79,19 +59,11 @@ class TypeInfoHelper extends TypeInfo_1.TypeInfo {
|
|
|
79
59
|
}
|
|
80
60
|
}
|
|
81
61
|
exports.TypeInfoHelper = TypeInfoHelper;
|
|
82
|
-
/**
|
|
83
|
-
* A class for validating if an object matches the type profile of a descriptor.
|
|
84
|
-
*/
|
|
85
62
|
class TypeValidator {
|
|
86
63
|
constructor(_typeDescriptor) {
|
|
87
64
|
this._typeDescriptor = _typeDescriptor;
|
|
88
65
|
Object.freeze(this);
|
|
89
66
|
}
|
|
90
|
-
/**
|
|
91
|
-
* Returns true if the parameter matches the descriptor.
|
|
92
|
-
* @param o
|
|
93
|
-
* @returns {o is T}
|
|
94
|
-
*/
|
|
95
67
|
isSubsetOf(o) {
|
|
96
68
|
return new TypeInfoHelper(o).contains(this._typeDescriptor);
|
|
97
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeValidator.js","sourceRoot":"","sources":["../../src/TypeValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;AAEH,+DAA8C;AAGtC,mBAHD,kBAAQ,CAGC;AAAE,yFAHA,mBAAQ,OAGA;AAF1B,+CAA2C;
|
|
1
|
+
{"version":3,"file":"TypeValidator.js","sourceRoot":"","sources":["../../src/TypeValidator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;AAEH,+DAA8C;AAGtC,mBAHD,kBAAQ,CAGC;AAAE,yFAHA,mBAAQ,OAGA;AAF1B,+C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CAAC"}
|
package/dist/esm/TypeInfo.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
+
import type from '@tsdotnet/type';
|
|
2
|
+
|
|
1
3
|
/*!
|
|
2
4
|
* @author electricessence / https://github.com/electricessence/
|
|
3
5
|
* @license MIT
|
|
4
6
|
*/
|
|
5
|
-
import type from '@tsdotnet/type';
|
|
6
|
-
// Only used for primitives.
|
|
7
7
|
const typeInfoRegistry = {};
|
|
8
8
|
const VOID0 = void 0;
|
|
9
|
-
|
|
10
|
-
* Exposes easy access to type information including inquiring about members.
|
|
11
|
-
*/
|
|
12
|
-
export class TypeInfo {
|
|
9
|
+
class TypeInfo {
|
|
13
10
|
target;
|
|
14
11
|
type;
|
|
15
12
|
isBoolean = false;
|
|
@@ -27,7 +24,6 @@ export class TypeInfo {
|
|
|
27
24
|
isNullOrUndefined = false;
|
|
28
25
|
isPrimitive = false;
|
|
29
26
|
isSymbol = false;
|
|
30
|
-
// noinspection DuplicatedCode
|
|
31
27
|
constructor(target) {
|
|
32
28
|
this.target = target;
|
|
33
29
|
switch ((this.type = typeof target)) {
|
|
@@ -74,43 +70,20 @@ export class TypeInfo {
|
|
|
74
70
|
}
|
|
75
71
|
Object.freeze(this);
|
|
76
72
|
}
|
|
77
|
-
/**
|
|
78
|
-
* Returns a TypeInfo for any target object.
|
|
79
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
80
|
-
* @param target
|
|
81
|
-
* @returns {TypeInfo}
|
|
82
|
-
*/
|
|
83
73
|
static for(target) {
|
|
84
74
|
return typeInfo(target);
|
|
85
75
|
}
|
|
86
|
-
/**
|
|
87
|
-
* Returns a TypeInfo for any member or non-member,
|
|
88
|
-
* where non-members are of type undefined.
|
|
89
|
-
* @param name
|
|
90
|
-
* @returns {TypeInfo}
|
|
91
|
-
*/
|
|
92
76
|
member(name) {
|
|
93
77
|
const t = this.target;
|
|
94
78
|
const m = !this.isPrimitive && t && name in t ? t[name] : VOID0;
|
|
95
79
|
return typeInfo(m);
|
|
96
80
|
}
|
|
97
|
-
/**
|
|
98
|
-
* Returns true if the target matches the type (instanceof).
|
|
99
|
-
* @param type
|
|
100
|
-
* @returns {boolean}
|
|
101
|
-
*/
|
|
102
81
|
is(type) {
|
|
103
82
|
const t = this.target;
|
|
104
83
|
if (t == null)
|
|
105
84
|
throw '\'type\' is null or undefined';
|
|
106
85
|
return t instanceof type;
|
|
107
86
|
}
|
|
108
|
-
/**
|
|
109
|
-
* Returns null if the target does not match the type (instanceof).
|
|
110
|
-
* Otherwise returns the target as the type.
|
|
111
|
-
* @param type
|
|
112
|
-
* @returns {T|null}
|
|
113
|
-
*/
|
|
114
87
|
as(type) {
|
|
115
88
|
const t = this.target;
|
|
116
89
|
if (t == null)
|
|
@@ -118,13 +91,7 @@ export class TypeInfo {
|
|
|
118
91
|
return t instanceof type ? t : null;
|
|
119
92
|
}
|
|
120
93
|
}
|
|
121
|
-
|
|
122
|
-
* Returns a TypeInfo for any target object.
|
|
123
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
124
|
-
* @param target
|
|
125
|
-
* @returns {TypeInfo}
|
|
126
|
-
*/
|
|
127
|
-
export default function typeInfo(target) {
|
|
94
|
+
function typeInfo(target) {
|
|
128
95
|
const t = target === null ? 'null' : typeof target;
|
|
129
96
|
switch (t) {
|
|
130
97
|
case 'object':
|
|
@@ -136,4 +103,6 @@ export default function typeInfo(target) {
|
|
|
136
103
|
typeInfoRegistry[t] = info = new TypeInfo(target);
|
|
137
104
|
return info;
|
|
138
105
|
}
|
|
139
|
-
|
|
106
|
+
|
|
107
|
+
export { TypeInfo, typeInfo as default };
|
|
108
|
+
//# sourceMappingURL=TypeInfo.js.map
|
package/dist/esm/TypeInfo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeInfo.js","
|
|
1
|
+
{"version":3,"file":"TypeInfo.js","sources":["../../../src/TypeInfo.ts"],"sourcesContent":[null],"names":[],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AAM,CAAC,GAAG,IAAI,CAAC,MAAM;QACrB,IAAG,CAAC,IAAE,IAAI;AAAE,YAAA,MAAM,+BAA+B;QACjD,OAAO,CAAC,YAAY,IAAI;IACzB;AASA,IAAA,EAAE,CAAoB,IAAiC,EAAA;AAEtD,QAAA,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM;QACrB,IAAG,CAAC,IAAE,IAAI;AAAE,YAAA,MAAM,+BAA+B;QACjD,OAAO,CAAC,YAAY,IAAI,GAAG,CAAC,GAAG,IAAI;IACpC;AACA;AAQa,SAAU,QAAQ,CAAE,MAAe,EAAA;AAChD,IAAA,MAAM,CAAC,GAAW,MAAM,KAAG,IAAI,GAAG,MAAM,GAAG,OAAO,MAAM;IACxD,QAAO,CAAC;AAEP,QAAA,KAAK,QAAQ;AACb,QAAA,KAAK,UAAU;AACd,YAAA,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC;;AAE7B,IAAA,IAAI,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC;AAC9B,IAAA,IAAG,CAAC,IAAI;QAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC;AAC3D,IAAA,OAAO,IAAI;AACZ;;;;"}
|
|
@@ -1,31 +1,16 @@
|
|
|
1
|
+
import { TypeInfo } from './TypeInfo.js';
|
|
2
|
+
export { default as typeInfo } from './TypeInfo.js';
|
|
3
|
+
import { areEqual } from '@tsdotnet/compare';
|
|
4
|
+
|
|
1
5
|
/*!
|
|
2
6
|
* @author electricessence / https://github.com/electricessence/
|
|
3
7
|
* @license MIT
|
|
4
8
|
*/
|
|
5
|
-
|
|
6
|
-
import { areEqual } from '@tsdotnet/compare';
|
|
7
|
-
export { typeInfo, TypeInfo };
|
|
8
|
-
/**
|
|
9
|
-
* A descriptor is simply a JSON tree that either has an actual value or a type that identifies what the expect type should be at that leaf in the tree.
|
|
10
|
-
*
|
|
11
|
-
* var descriptor = {
|
|
12
|
-
* a : Object,
|
|
13
|
-
* b : String,
|
|
14
|
-
* c : {
|
|
15
|
-
* d : true ,
|
|
16
|
-
* e : Array,
|
|
17
|
-
* f : []
|
|
18
|
-
* },
|
|
19
|
-
* g : "literal"
|
|
20
|
-
* }
|
|
21
|
-
*/
|
|
22
|
-
export class TypeInfoHelper extends TypeInfo {
|
|
9
|
+
class TypeInfoHelper extends TypeInfo {
|
|
23
10
|
contains(descriptor) {
|
|
24
11
|
const value = this.target;
|
|
25
|
-
// works with literals and values.
|
|
26
12
|
if (value === descriptor)
|
|
27
13
|
return true;
|
|
28
|
-
// First check against simple types.
|
|
29
14
|
switch (descriptor) {
|
|
30
15
|
case Function:
|
|
31
16
|
return this.isFunction;
|
|
@@ -42,7 +27,6 @@ export class TypeInfoHelper extends TypeInfo {
|
|
|
42
27
|
}
|
|
43
28
|
if (this.type !== typeof descriptor || (this.isPrimitive && !areEqual(value, descriptor)))
|
|
44
29
|
return false;
|
|
45
|
-
// Check array contents and confirm intersections.
|
|
46
30
|
if (this.isArrayLike && descriptor instanceof Array) {
|
|
47
31
|
const vLen = value.length, dLen = descriptor.length;
|
|
48
32
|
if (vLen !== dLen && (isNaN(vLen) || vLen < dLen))
|
|
@@ -56,15 +40,12 @@ export class TypeInfoHelper extends TypeInfo {
|
|
|
56
40
|
if (this.isObject) {
|
|
57
41
|
const targetKeys = Object.keys(value);
|
|
58
42
|
const dKeys = Object.keys(descriptor);
|
|
59
|
-
// Quick check...
|
|
60
43
|
if (dKeys.length > targetKeys.length)
|
|
61
44
|
return false;
|
|
62
|
-
// Quick check #2...
|
|
63
45
|
for (const key of dKeys) {
|
|
64
46
|
if (targetKeys.indexOf(key) === -1)
|
|
65
47
|
return false;
|
|
66
48
|
}
|
|
67
|
-
// Final pass with recursive...
|
|
68
49
|
for (const key of dKeys) {
|
|
69
50
|
if (areInvalid(value[key], descriptor[key]))
|
|
70
51
|
return false;
|
|
@@ -73,20 +54,12 @@ export class TypeInfoHelper extends TypeInfo {
|
|
|
73
54
|
return true;
|
|
74
55
|
}
|
|
75
56
|
}
|
|
76
|
-
|
|
77
|
-
* A class for validating if an object matches the type profile of a descriptor.
|
|
78
|
-
*/
|
|
79
|
-
export default class TypeValidator {
|
|
57
|
+
class TypeValidator {
|
|
80
58
|
_typeDescriptor;
|
|
81
59
|
constructor(_typeDescriptor) {
|
|
82
60
|
this._typeDescriptor = _typeDescriptor;
|
|
83
61
|
Object.freeze(this);
|
|
84
62
|
}
|
|
85
|
-
/**
|
|
86
|
-
* Returns true if the parameter matches the descriptor.
|
|
87
|
-
* @param o
|
|
88
|
-
* @returns {o is T}
|
|
89
|
-
*/
|
|
90
63
|
isSubsetOf(o) {
|
|
91
64
|
return new TypeInfoHelper(o).contains(this._typeDescriptor);
|
|
92
65
|
}
|
|
@@ -99,4 +72,6 @@ function areInvalid(v, d) {
|
|
|
99
72
|
}
|
|
100
73
|
return false;
|
|
101
74
|
}
|
|
102
|
-
|
|
75
|
+
|
|
76
|
+
export { TypeInfo, TypeInfoHelper, TypeValidator as default };
|
|
77
|
+
//# sourceMappingURL=TypeValidator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TypeValidator.js","
|
|
1
|
+
{"version":3,"file":"TypeValidator.js","sources":["../../../src/TypeValidator.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAAA;;;AAGG;AAqBG,MAAO,cACZ,SAAQ,QAAQ,CAAA;AAEhB,IAAA,QAAQ,CAAe,UAAmB,EAAA;AAEzC,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM;QAGzB,IAAG,KAAK,KAAG,UAAU;AAAE,YAAA,OAAO,IAAI;QAGlC,QAAO,UAAU;AAEhB,YAAA,KAAK,QAAQ;gBACZ,OAAO,IAAI,CAAC,UAAU;AACvB,YAAA,KAAK,MAAM;gBACV,OAAO,IAAI,CAAC,QAAQ;AACrB,YAAA,KAAK,KAAK;gBACT,OAAO,IAAI,CAAC,OAAO;AACpB,YAAA,KAAK,MA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GAAG,IAAI,KAAK,EACtB;gBACC,IAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,EAAG,UAAkB,CAAC,GAAG,CAAC,CAAC;AAAE,oBAAA,OAAO,KAAK;YAClE;QACD;AAEA,QAAA,OAAO,IAAI;IACZ;AACA;AAKa,MAAO,aAAa,CAAA;AAEH,IAAA,eAAA;AAA9B,IAAA,WAAA,CAA8B,eAAoB,EAAA;QAApB,IAAA,CAAA,eAAe,GAAf,eAAe;AAE5C,QAAA,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IACpB;AAOA,IAAA,UAAU,CAAE,CAAU,EAAA;AAErB,QAAA,OAAO,IAAI,cAAc,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC;IAC5D;AACA;AAGD,SAAS,UAAU,CAAE,CAAM,EAAE,CAAM,EAAA;IAElC,IAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAClB;AACC,QAAA,MAAM,UAAU,GAAG,IAAI,cAAc,CAAC,CAAC,CAAC;AACxC,QAAA,IAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC;AAAE,YAAA,OAAO,IAAI;IACxC;AACA,IAAA,OAAO,KAAK;AACb;;;;"}
|
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
* @author electricessence / https://github.com/electricessence/
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
|
-
/**
|
|
6
|
-
* Exposes easy access to type information including inquiring about members.
|
|
7
|
-
*/
|
|
8
5
|
export declare class TypeInfo {
|
|
9
6
|
protected readonly target: any;
|
|
10
7
|
readonly type: string;
|
|
@@ -24,38 +21,9 @@ export declare class TypeInfo {
|
|
|
24
21
|
readonly isPrimitive: boolean;
|
|
25
22
|
readonly isSymbol: boolean;
|
|
26
23
|
constructor(target: any);
|
|
27
|
-
/**
|
|
28
|
-
* Returns a TypeInfo for any target object.
|
|
29
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
30
|
-
* @param target
|
|
31
|
-
* @returns {TypeInfo}
|
|
32
|
-
*/
|
|
33
24
|
static for(target: unknown): TypeInfo;
|
|
34
|
-
/**
|
|
35
|
-
* Returns a TypeInfo for any member or non-member,
|
|
36
|
-
* where non-members are of type undefined.
|
|
37
|
-
* @param name
|
|
38
|
-
* @returns {TypeInfo}
|
|
39
|
-
*/
|
|
40
25
|
member(name: string | number | symbol): TypeInfo;
|
|
41
|
-
/**
|
|
42
|
-
* Returns true if the target matches the type (instanceof).
|
|
43
|
-
* @param type
|
|
44
|
-
* @returns {boolean}
|
|
45
|
-
*/
|
|
46
26
|
is<T extends object>(type: new (...params: any[]) => T): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* Returns null if the target does not match the type (instanceof).
|
|
49
|
-
* Otherwise returns the target as the type.
|
|
50
|
-
* @param type
|
|
51
|
-
* @returns {T|null}
|
|
52
|
-
*/
|
|
53
27
|
as<T extends object>(type: new (...params: any[]) => T): T | null;
|
|
54
28
|
}
|
|
55
|
-
/**
|
|
56
|
-
* Returns a TypeInfo for any target object.
|
|
57
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
58
|
-
* @param target
|
|
59
|
-
* @returns {TypeInfo}
|
|
60
|
-
*/
|
|
61
29
|
export default function typeInfo(target: unknown): TypeInfo;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @author electricessence / https://github.com/electricessence/
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import typeInfo, { TypeInfo } from './TypeInfo';
|
|
6
|
+
export { typeInfo, TypeInfo };
|
|
7
|
+
export declare class TypeInfoHelper extends TypeInfo {
|
|
8
|
+
contains<TDescriptor>(descriptor: unknown): this is TDescriptor;
|
|
9
|
+
}
|
|
10
|
+
export default class TypeValidator<T> {
|
|
11
|
+
private readonly _typeDescriptor;
|
|
12
|
+
constructor(_typeDescriptor: any);
|
|
13
|
+
isSubsetOf(o: unknown): o is T;
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsdotnet/reflection",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "electricessence",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
10
|
+
"types": "./dist/types/TypeValidator.d.ts",
|
|
9
11
|
"import": "./dist/esm/TypeValidator.js",
|
|
10
|
-
"require": "./dist/cjs/TypeValidator.js"
|
|
11
|
-
"types": "./dist/TypeValidator.d.ts"
|
|
12
|
+
"require": "./dist/cjs/TypeValidator.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
|
-
"types": "dist/TypeValidator.d.ts",
|
|
15
|
-
"main": "dist/cjs/TypeValidator.js",
|
|
16
|
-
"module": "dist/esm/TypeValidator.js",
|
|
17
|
-
"umd:main": "dist/umd/TypeValidator.js",
|
|
18
|
-
"unpkg": "dist/umd/TypeValidator.js",
|
|
19
|
-
"jsdelivr": "dist/umd/TypeValidator.js",
|
|
15
|
+
"types": "./dist/types/types/TypeValidator.d.ts",
|
|
20
16
|
"keywords": [
|
|
21
17
|
"reflection",
|
|
22
18
|
"type inspection",
|
|
@@ -26,20 +22,20 @@
|
|
|
26
22
|
],
|
|
27
23
|
"scripts": {
|
|
28
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",
|
|
29
27
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
30
|
-
"build
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"bump": "pnpm run precommit && pnpm version patch",
|
|
35
|
-
"docs": "rimraf docs && typedoc --options typedoc.json --readme none",
|
|
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",
|
|
36
32
|
"lint": "eslint src/**/*.ts",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
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",
|
|
40
37
|
"postversion": "git push && git push --tags && pnpm run docs && git add -A && git commit -m \"Updated docs.\" docs && git push",
|
|
41
|
-
"
|
|
42
|
-
"validate": "node ./.build/validate-package.js"
|
|
38
|
+
"prepublishOnly": "pnpm build && pnpm test && pnpm lint"
|
|
43
39
|
},
|
|
44
40
|
"repository": {
|
|
45
41
|
"type": "git",
|
|
@@ -50,24 +46,23 @@
|
|
|
50
46
|
},
|
|
51
47
|
"homepage": "https://github.com/tsdotnet/reflection#readme",
|
|
52
48
|
"devDependencies": {
|
|
53
|
-
"@eslint/js": "^9.
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
49
|
+
"@eslint/js": "^9.34.0",
|
|
50
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
51
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
56
52
|
"@types/node": "^24.3.0",
|
|
57
|
-
"
|
|
58
|
-
"concurrently": "^9.2.0",
|
|
53
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
59
54
|
"cpy-cli": "^6.0.0",
|
|
60
|
-
"eslint": "^9.
|
|
61
|
-
"mocha": "^11.7.1",
|
|
55
|
+
"eslint": "^9.34.0",
|
|
62
56
|
"rimraf": "^6.0.1",
|
|
63
|
-
"
|
|
57
|
+
"rollup": "^4.48.0",
|
|
64
58
|
"typedoc": "^0.28.10",
|
|
65
59
|
"typescript": "^5.9.2",
|
|
66
|
-
"typescript-eslint": "^8.
|
|
60
|
+
"typescript-eslint": "^8.40.0",
|
|
61
|
+
"vitest": "^3.2.4"
|
|
67
62
|
},
|
|
68
63
|
"dependencies": {
|
|
69
|
-
"@tsdotnet/compare": "^1.
|
|
70
|
-
"@tsdotnet/type": "^1.2.
|
|
64
|
+
"@tsdotnet/compare": "^1.7.0",
|
|
65
|
+
"@tsdotnet/type": "^1.2.4",
|
|
71
66
|
"tslib": "^2.8.1"
|
|
72
67
|
},
|
|
73
68
|
"pnpm": {
|
|
@@ -86,9 +81,6 @@
|
|
|
86
81
|
"get-func-name@<2.0.1": ">=2.0.1",
|
|
87
82
|
"@babel/traverse@<7.23.2": ">=7.23.2",
|
|
88
83
|
"braces@<3.0.3": ">=3.0.3"
|
|
89
|
-
}
|
|
90
|
-
"ignoredBuiltDependencies": [
|
|
91
|
-
"esbuild"
|
|
92
|
-
]
|
|
84
|
+
}
|
|
93
85
|
}
|
|
94
86
|
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { defineConfig } from 'rollup';
|
|
2
|
+
import typescript from '@rollup/plugin-typescript';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)));
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
input: 'src/TypeValidator.ts', // Just use the main entry point
|
|
9
|
+
output: {
|
|
10
|
+
dir: 'dist/esm',
|
|
11
|
+
format: 'es',
|
|
12
|
+
preserveModules: true, // This will automatically include all imported files!
|
|
13
|
+
preserveModulesRoot: 'src',
|
|
14
|
+
entryFileNames: '[name].js',
|
|
15
|
+
sourcemap: true
|
|
16
|
+
},
|
|
17
|
+
external: [
|
|
18
|
+
// Node core modules
|
|
19
|
+
/^node:/,
|
|
20
|
+
// All dependencies and peer dependencies
|
|
21
|
+
...Object.keys(pkg.dependencies || {}),
|
|
22
|
+
...Object.keys(pkg.peerDependencies || {})
|
|
23
|
+
],
|
|
24
|
+
plugins: [
|
|
25
|
+
typescript({
|
|
26
|
+
tsconfig: './tsconfig.esm.json',
|
|
27
|
+
declaration: false, // Keep your existing types build
|
|
28
|
+
declarationMap: false,
|
|
29
|
+
sourceMap: true,
|
|
30
|
+
removeComments: true // Explicitly remove source comments
|
|
31
|
+
})
|
|
32
|
+
]
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
coverage: {
|
|
8
|
+
enabled: true,
|
|
9
|
+
reporter: ['text'],
|
|
10
|
+
include: ['src/**/*.ts'],
|
|
11
|
+
exclude: ['src/**/*.d.ts'],
|
|
12
|
+
thresholds: {
|
|
13
|
+
global: {
|
|
14
|
+
branches: 80,
|
|
15
|
+
functions: 80,
|
|
16
|
+
lines: 80,
|
|
17
|
+
statements: 80
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
package/dist/TypeValidator.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author electricessence / https://github.com/electricessence/
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
import typeInfo, { TypeInfo } from './TypeInfo';
|
|
6
|
-
export { typeInfo, TypeInfo };
|
|
7
|
-
/**
|
|
8
|
-
* A descriptor is simply a JSON tree that either has an actual value or a type that identifies what the expect type should be at that leaf in the tree.
|
|
9
|
-
*
|
|
10
|
-
* var descriptor = {
|
|
11
|
-
* a : Object,
|
|
12
|
-
* b : String,
|
|
13
|
-
* c : {
|
|
14
|
-
* d : true ,
|
|
15
|
-
* e : Array,
|
|
16
|
-
* f : []
|
|
17
|
-
* },
|
|
18
|
-
* g : "literal"
|
|
19
|
-
* }
|
|
20
|
-
*/
|
|
21
|
-
export declare class TypeInfoHelper extends TypeInfo {
|
|
22
|
-
contains<TDescriptor>(descriptor: unknown): this is TDescriptor;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* A class for validating if an object matches the type profile of a descriptor.
|
|
26
|
-
*/
|
|
27
|
-
export default class TypeValidator<T> {
|
|
28
|
-
private readonly _typeDescriptor;
|
|
29
|
-
constructor(_typeDescriptor: any);
|
|
30
|
-
/**
|
|
31
|
-
* Returns true if the parameter matches the descriptor.
|
|
32
|
-
* @param o
|
|
33
|
-
* @returns {o is T}
|
|
34
|
-
*/
|
|
35
|
-
isSubsetOf(o: unknown): o is T;
|
|
36
|
-
}
|
package/dist/umd/TypeInfo.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author electricessence / https://github.com/electricessence/
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
(function (factory) {
|
|
6
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
-
var v = factory(require, exports);
|
|
8
|
-
if (v !== undefined) module.exports = v;
|
|
9
|
-
}
|
|
10
|
-
else if (typeof define === "function" && define.amd) {
|
|
11
|
-
define(["require", "exports", "tslib", "@tsdotnet/type"], factory);
|
|
12
|
-
}
|
|
13
|
-
})(function (require, exports) {
|
|
14
|
-
"use strict";
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.TypeInfo = void 0;
|
|
17
|
-
exports.default = typeInfo;
|
|
18
|
-
const tslib_1 = require("tslib");
|
|
19
|
-
const type_1 = tslib_1.__importDefault(require("@tsdotnet/type"));
|
|
20
|
-
// Only used for primitives.
|
|
21
|
-
const typeInfoRegistry = {};
|
|
22
|
-
const VOID0 = void 0;
|
|
23
|
-
/**
|
|
24
|
-
* Exposes easy access to type information including inquiring about members.
|
|
25
|
-
*/
|
|
26
|
-
class TypeInfo {
|
|
27
|
-
target;
|
|
28
|
-
type;
|
|
29
|
-
isBoolean = false;
|
|
30
|
-
isNumber = false;
|
|
31
|
-
isFinite = false;
|
|
32
|
-
isValidNumber = false;
|
|
33
|
-
isString = false;
|
|
34
|
-
isTrueNaN = false;
|
|
35
|
-
isObject = false;
|
|
36
|
-
isArray = false;
|
|
37
|
-
isArrayLike = false;
|
|
38
|
-
isFunction = false;
|
|
39
|
-
isUndefined = false;
|
|
40
|
-
isNull = false;
|
|
41
|
-
isNullOrUndefined = false;
|
|
42
|
-
isPrimitive = false;
|
|
43
|
-
isSymbol = false;
|
|
44
|
-
// noinspection DuplicatedCode
|
|
45
|
-
constructor(target) {
|
|
46
|
-
this.target = target;
|
|
47
|
-
switch ((this.type = typeof target)) {
|
|
48
|
-
case 'boolean':
|
|
49
|
-
this.isBoolean = true;
|
|
50
|
-
this.isPrimitive = true;
|
|
51
|
-
break;
|
|
52
|
-
case 'number':
|
|
53
|
-
this.isNumber = true;
|
|
54
|
-
this.isTrueNaN = isNaN(target);
|
|
55
|
-
this.isFinite = isFinite(target);
|
|
56
|
-
this.isValidNumber = !this.isTrueNaN;
|
|
57
|
-
this.isPrimitive = true;
|
|
58
|
-
break;
|
|
59
|
-
case 'string':
|
|
60
|
-
this.isString = true;
|
|
61
|
-
this.isPrimitive = true;
|
|
62
|
-
break;
|
|
63
|
-
case 'symbol':
|
|
64
|
-
this.isSymbol = true;
|
|
65
|
-
break;
|
|
66
|
-
case 'object':
|
|
67
|
-
if (target === null) {
|
|
68
|
-
this.isNull = true;
|
|
69
|
-
this.isNullOrUndefined = true;
|
|
70
|
-
this.isPrimitive = true;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
this.isObject = true;
|
|
74
|
-
this.isArray = target instanceof Array;
|
|
75
|
-
this.isArrayLike = this.isArray || type_1.default.isArrayLike(target);
|
|
76
|
-
}
|
|
77
|
-
break;
|
|
78
|
-
case 'function':
|
|
79
|
-
this.isFunction = true;
|
|
80
|
-
break;
|
|
81
|
-
case 'undefined':
|
|
82
|
-
this.isUndefined = true;
|
|
83
|
-
this.isNullOrUndefined = true;
|
|
84
|
-
this.isPrimitive = true;
|
|
85
|
-
break;
|
|
86
|
-
default:
|
|
87
|
-
throw new Error('Fatal type failure. Unknown type: ' + this.type);
|
|
88
|
-
}
|
|
89
|
-
Object.freeze(this);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Returns a TypeInfo for any target object.
|
|
93
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
94
|
-
* @param target
|
|
95
|
-
* @returns {TypeInfo}
|
|
96
|
-
*/
|
|
97
|
-
static for(target) {
|
|
98
|
-
return typeInfo(target);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Returns a TypeInfo for any member or non-member,
|
|
102
|
-
* where non-members are of type undefined.
|
|
103
|
-
* @param name
|
|
104
|
-
* @returns {TypeInfo}
|
|
105
|
-
*/
|
|
106
|
-
member(name) {
|
|
107
|
-
const t = this.target;
|
|
108
|
-
const m = !this.isPrimitive && t && name in t ? t[name] : VOID0;
|
|
109
|
-
return typeInfo(m);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Returns true if the target matches the type (instanceof).
|
|
113
|
-
* @param type
|
|
114
|
-
* @returns {boolean}
|
|
115
|
-
*/
|
|
116
|
-
is(type) {
|
|
117
|
-
const t = this.target;
|
|
118
|
-
if (t == null)
|
|
119
|
-
throw '\'type\' is null or undefined';
|
|
120
|
-
return t instanceof type;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Returns null if the target does not match the type (instanceof).
|
|
124
|
-
* Otherwise returns the target as the type.
|
|
125
|
-
* @param type
|
|
126
|
-
* @returns {T|null}
|
|
127
|
-
*/
|
|
128
|
-
as(type) {
|
|
129
|
-
const t = this.target;
|
|
130
|
-
if (t == null)
|
|
131
|
-
throw '\'type\' is null or undefined';
|
|
132
|
-
return t instanceof type ? t : null;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
exports.TypeInfo = TypeInfo;
|
|
136
|
-
/**
|
|
137
|
-
* Returns a TypeInfo for any target object.
|
|
138
|
-
* If the target object is of a primitive type, it returns the TypeInfo instance assigned to that type.
|
|
139
|
-
* @param target
|
|
140
|
-
* @returns {TypeInfo}
|
|
141
|
-
*/
|
|
142
|
-
function typeInfo(target) {
|
|
143
|
-
const t = target === null ? 'null' : typeof target;
|
|
144
|
-
switch (t) {
|
|
145
|
-
case 'object':
|
|
146
|
-
case 'function':
|
|
147
|
-
return new TypeInfo(target);
|
|
148
|
-
}
|
|
149
|
-
let info = typeInfoRegistry[t];
|
|
150
|
-
if (!info)
|
|
151
|
-
typeInfoRegistry[t] = info = new TypeInfo(target);
|
|
152
|
-
return info;
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
//# sourceMappingURL=TypeInfo.js.map
|
package/dist/umd/TypeInfo.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TypeInfo.js","sourceRoot":"","sources":["../../src/TypeInfo.ts"],"names":[],"mappings":"AAAA;;;GAGG;;;;;;;;;;;;;IA6IH,2BAWC;;IAtJD,kEAAkC;IAElC,4BAA4B;IAC5B,MAAM,gBAAgB,GAAgC,EAAE,CAAC;IACzD,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC;IAErB;;OAEG;IACH,MAAa,QAAQ;QAqBY;QAnBvB,IAAI,CAAS;QACb,SAAS,GAAY,KAAK,CAAC;QAC3B,QAAQ,GAAY,KAAK,CAAC;QAC1B,QAAQ,GAAY,KAAK,CAAC;QAC1B,aAAa,GAAY,KAAK,CAAC;QAC/B,QAAQ,GAAY,KAAK,CAAC;QAC1B,SAAS,GAAY,KAAK,CAAC;QAC3B,QAAQ,GAAY,KAAK,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AKG;QAEH,EAAE,CAAoB,IAAiC;YAEtD,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;YACtB,IAAG,CAAC,IAAE,IAAI;gBAAE,MAAM,+BAA+B,CAAC;YAClD,OAAO,CAAC,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACrC,CAAC;KACD;IA1HD,4BA0HC;IAED;;;;;OAKG;IACH,SAAwB,QAAQ,CAAE,MAAe;QAChD,MAAM,CAAC,GAAW,MAAM,KAAG,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,MAAM,CAAC;QACzD,QAAO,CAAC,EACR,CAAC;YACA,KAAK,QAAQ,CAAC;YACd,KAAK,UAAU;gBACd,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;QAC/B,IAAG,CAAC,IAAI;YAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC5D,OAAO,IAAI,CAAC;IACb,CAAC"}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @author electricessence / https://github.com/electricessence/
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
(function (factory) {
|
|
6
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
-
var v = factory(require, exports);
|
|
8
|
-
if (v !== undefined) module.exports = v;
|
|
9
|
-
}
|
|
10
|
-
else if (typeof define === "function" && define.amd) {
|
|
11
|
-
define(["require", "exports", "tslib", "./TypeInfo", "@tsdotnet/compare"], factory);
|
|
12
|
-
}
|
|
13
|
-
})(function (require, exports) {
|
|
14
|
-
"use strict";
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.TypeInfoHelper = exports.TypeInfo = exports.typeInfo = void 0;
|
|
17
|
-
const tslib_1 = require("tslib");
|
|
18
|
-
const TypeInfo_1 = tslib_1.__importStar(require("./TypeInfo"));
|
|
19
|
-
exports.typeInfo = TypeInfo_1.default;
|
|
20
|
-
Object.defineProperty(exports, "TypeInfo", { enumerable: true, get: function () { return TypeInfo_1.TypeInfo; } });
|
|
21
|
-
const compare_1 = require("@tsdotnet/compare");
|
|
22
|
-
/**
|
|
23
|
-
* A descriptor is simply a JSON tree that either has an actual value or a type that identifies what the expect type should be at that leaf in the tree.
|
|
24
|
-
*
|
|
25
|
-
* var descriptor = {
|
|
26
|
-
* a : Object,
|
|
27
|
-
* b : String,
|
|
28
|
-
* c : {
|
|
29
|
-
* d : true ,
|
|
30
|
-
* e : Array,
|
|
31
|
-
* f : []
|
|
32
|
-
* },
|
|
33
|
-
* g : "literal"
|
|
34
|
-
* }
|
|
35
|
-
*/
|
|
36
|
-
class TypeInfoHelper extends TypeInfo_1.TypeInfo {
|
|
37
|
-
contains(descriptor) {
|
|
38
|
-
const value = this.target;
|
|
39
|
-
// works with literals and values.
|
|
40
|
-
if (value === descriptor)
|
|
41
|
-
return true;
|
|
42
|
-
// First check against simple types.
|
|
43
|
-
switch (descriptor) {
|
|
44
|
-
case Function:
|
|
45
|
-
return this.isFunction;
|
|
46
|
-
case Object:
|
|
47
|
-
return this.isObject;
|
|
48
|
-
case Array:
|
|
49
|
-
return this.isArray;
|
|
50
|
-
case String:
|
|
51
|
-
return this.isString;
|
|
52
|
-
case Number:
|
|
53
|
-
return this.isNumber;
|
|
54
|
-
case Boolean:
|
|
55
|
-
return this.isBoolean;
|
|
56
|
-
}
|
|
57
|
-
if (this.type !== typeof descriptor || (this.isPrimitive && !(0, compare_1.areEqual)(value, descriptor)))
|
|
58
|
-
return false;
|
|
59
|
-
// Check array contents and confirm intersections.
|
|
60
|
-
if (this.isArrayLike && descriptor instanceof Array) {
|
|
61
|
-
const vLen = value.length, dLen = descriptor.length;
|
|
62
|
-
if (vLen !== dLen && (isNaN(vLen) || vLen < dLen))
|
|
63
|
-
return false;
|
|
64
|
-
for (let i = 0; i < descriptor.length; i++) {
|
|
65
|
-
if (areInvalid(value[i], descriptor[i]))
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
if (this.isObject) {
|
|
71
|
-
const targetKeys = Object.keys(value);
|
|
72
|
-
const dKeys = Object.keys(descriptor);
|
|
73
|
-
// Quick check...
|
|
74
|
-
if (dKeys.length > targetKeys.length)
|
|
75
|
-
return false;
|
|
76
|
-
// Quick check #2...
|
|
77
|
-
for (const key of dKeys) {
|
|
78
|
-
if (targetKeys.indexOf(key) === -1)
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
// Final pass with recursive...
|
|
82
|
-
for (const key of dKeys) {
|
|
83
|
-
if (areInvalid(value[key], descriptor[key]))
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.TypeInfoHelper = TypeInfoHelper;
|
|
91
|
-
/**
|
|
92
|
-
* A class for validating if an object matches the type profile of a descriptor.
|
|
93
|
-
*/
|
|
94
|
-
class TypeValidator {
|
|
95
|
-
_typeDescriptor;
|
|
96
|
-
constructor(_typeDescriptor) {
|
|
97
|
-
this._typeDescriptor = _typeDescriptor;
|
|
98
|
-
Object.freeze(this);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Returns true if the parameter matches the descriptor.
|
|
102
|
-
* @param o
|
|
103
|
-
* @returns {o is T}
|
|
104
|
-
*/
|
|
105
|
-
isSubsetOf(o) {
|
|
106
|
-
return new TypeInfoHelper(o).contains(this._typeDescriptor);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
exports.default = TypeValidator;
|
|
110
|
-
function areInvalid(v, d) {
|
|
111
|
-
if (!(0, compare_1.areEqual)(v, d)) {
|
|
112
|
-
const memberType = new TypeInfoHelper(v);
|
|
113
|
-
if (!memberType.contains(d))
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
//# sourceMappingURL=TypeValidator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TypeValidator.js","sourceRoot":"","sources":["../../src/TypeValidator.ts"],"names":[],"mappings":"AAAA;;;GAGG;;;;;;;;;;;;;;IAEH,+DAA8C;IAGtC,mBAHD,kBAAQ,CAGC;IAAE,yFAHA,mBAAQ,OAGA;IAF1B,+CAA2C;IAI3C;;;;;;;;;;;;;OAaG;IACH,MAAa,cACZ,SAAQ,mBAAQ;QAEhB,QAAQ,CAAe,UAAmB;YAEzC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;YAE1B,kCAAkC;YAClC,IAAG,KAAK,KAAG,UAAU;gBAAE,OAAO,IAAI,CAAC;YAEnC,oCAAoC;YACpC,QAAO,UAAU,EACjB,CAAC;gBACA,KAAK,QAAQ;oBACZ,OAAO,IAAI,CAAC,UAAU,CAAC;gBACxB,KAAK,MAAM;oBACV,OAAO,IAAI,CAAC,QAAQ,CAAC;gBACtB,KAAK,KAAK;oBACT,OAAO,IAAI,CAAC,OAAO,CAAC;gBACrB,KAAK,MAAM;oBACV,OAAO,IAAI,CAAC,QAAQ,CAAC;gBACtB,KAAK,MAAM;oBACV,OAAO,IAAI,CAAC,QAAQ,CAAC;gBACtB,KAAK,OAAO;oBACX,OAAO,IAAI,CAAC,SAAS,CAAC;YACxB,CAAC;YAED,IAAG,IAAI,CAAC,IAAI,KAAI,OAAO,UAAU,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAA,kBAAQ,EAAC,KAAK,EAAE,UAAU,CAAC,CAAC;gBAAE,OAAO,KAAK,CAAC;YAEtG,kDAAkD;YAClD,IAAG,IAAI,CAAC,WAAW,IAAI,UAAU,YAAY,KAAK,EAClD,CAAC;gBAC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