rtt 1.0.0 → 1.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/README.md +2 -2
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/model.d.ts +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ npm install rtt
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
24
|
-
import {$NewType,
|
|
24
|
+
import {$NewType, is, Model} from 'rtt';
|
|
25
25
|
|
|
26
26
|
// Define types
|
|
27
27
|
const $A = () => $NewType('MyPackage', 'A');
|
|
@@ -29,7 +29,7 @@ const $B = () => $NewType('MyPackage', 'B', $A());
|
|
|
29
29
|
|
|
30
30
|
// Create a model instance
|
|
31
31
|
const model: Model = {
|
|
32
|
-
|
|
32
|
+
$type: $B(),
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
// Runtime type checks
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
function is(model, type) {
|
|
2
|
-
return model
|
|
2
|
+
return model?.$type?.is(type) ?? false;
|
|
3
3
|
}
|
|
4
|
-
const $type = Symbol("type");
|
|
5
4
|
const $Model = () => $NewType("RuntimeType", "Model");
|
|
6
5
|
function $NewType(packageName, typeName, parent, generics) {
|
|
7
6
|
return {
|
|
@@ -32,7 +31,6 @@ function $NewType(packageName, typeName, parent, generics) {
|
|
|
32
31
|
export {
|
|
33
32
|
$Model,
|
|
34
33
|
$NewType,
|
|
35
|
-
$type,
|
|
36
34
|
is
|
|
37
35
|
};
|
|
38
36
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/is.ts","../src/model.ts","../src/type.factory.ts"],"sourcesContent":["import {$Type
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/is.ts","../src/model.ts","../src/type.factory.ts"],"sourcesContent":["import {$Type} from '#lib';\n\nexport function is<T extends $Type>(model: any, type: T): model is Exclude<T['type'], undefined> {\n return (model?.$type as $Type)?.is(type) ?? false;\n}\n","import {$NewType, $Type} from '#lib';\n\nexport interface Model {\n $type: $Type;\n}\n\nexport const $Model = () => $NewType<Model>('RuntimeType', 'Model');\n","import {$Model, $Type, Model} from '#lib';\n\nexport function $NewType<T extends Model>(\n packageName: string,\n typeName: string,\n parent?: $Type | null | undefined,\n generics?: $Type[] | null | undefined,\n): $Type<T> {\n return {\n packageName,\n typeName,\n parent,\n generics,\n\n toString(): string {\n return `${this.packageName}.${this.typeName}`;\n },\n\n nameEquals(other: $Type): boolean {\n return this.typeName === other.typeName && this.packageName == other.packageName;\n },\n\n is(other: $Type): boolean {\n if (this.parent?.is(other) || other.nameEquals($Model())) {\n return true;\n }\n\n if (other.generics && other.generics?.length > 0) {\n if (this.generics && this.generics.length === other.generics.length) {\n // todo Covariance and contravariance ???\n return this.generics.every((x, i) => x.is(other.generics![i]));\n }\n\n return false;\n }\n\n return this.nameEquals(other);\n },\n };\n}\n"],"names":[],"mappings":"AAEO,SAAS,GAAoB,OAAY,MAAiD;AAC/F,SAAQ,OAAO,OAAiB,GAAG,IAAI,KAAK;AAC9C;ACEO,MAAM,SAAS,MAAM,SAAgB,eAAe,OAAO;ACJ3D,SAAS,SACd,aACA,UACA,QACA,UACU;AACV,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAEA,WAAmB;AACjB,aAAO,GAAG,KAAK,WAAW,IAAI,KAAK,QAAQ;AAAA,IAC7C;AAAA,IAEA,WAAW,OAAuB;AAChC,aAAO,KAAK,aAAa,MAAM,YAAY,KAAK,eAAe,MAAM;AAAA,IACvE;AAAA,IAEA,GAAG,OAAuB;AACxB,UAAI,KAAK,QAAQ,GAAG,KAAK,KAAK,MAAM,WAAW,OAAA,CAAQ,GAAG;AACxD,eAAO;AAAA,MACT;AAEA,UAAI,MAAM,YAAY,MAAM,UAAU,SAAS,GAAG;AAChD,YAAI,KAAK,YAAY,KAAK,SAAS,WAAW,MAAM,SAAS,QAAQ;AAEnE,iBAAO,KAAK,SAAS,MAAM,CAAC,GAAG,MAAM,EAAE,GAAG,MAAM,SAAU,CAAC,CAAC,CAAC;AAAA,QAC/D;AAEA,eAAO;AAAA,MACT;AAEA,aAAO,KAAK,WAAW,KAAK;AAAA,IAC9B;AAAA,EAAA;AAEJ;"}
|
package/dist/model.d.ts
CHANGED