@jbrowse/mobx-state-tree 5.6.3 → 5.6.9
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/core/action.d.ts +87 -0
- package/dist/core/action.js +228 -0
- package/dist/core/action.js.map +1 -0
- package/dist/core/actionContext.d.ts +27 -0
- package/dist/core/actionContext.js +42 -0
- package/dist/core/actionContext.js.map +1 -0
- package/dist/core/flow.d.ts +69 -0
- package/dist/core/flow.js +180 -0
- package/dist/core/flow.js.map +1 -0
- package/dist/core/json-patch.d.ts +46 -0
- package/dist/core/json-patch.js +133 -0
- package/dist/core/json-patch.js.map +1 -0
- package/dist/core/mst-operations.d.ts +459 -0
- package/dist/core/mst-operations.js +885 -0
- package/dist/core/mst-operations.js.map +1 -0
- package/dist/core/node/BaseNode.d.ts +62 -0
- package/dist/core/node/BaseNode.js +152 -0
- package/dist/core/node/BaseNode.js.map +1 -0
- package/dist/core/node/Hook.d.ts +17 -0
- package/dist/core/node/Hook.js +15 -0
- package/dist/core/node/Hook.js.map +1 -0
- package/dist/core/node/create-node.d.ts +16 -0
- package/dist/core/node/create-node.js +41 -0
- package/dist/core/node/create-node.js.map +1 -0
- package/dist/core/node/identifier-cache.d.ts +19 -0
- package/dist/core/node/identifier-cache.js +115 -0
- package/dist/core/node/identifier-cache.js.map +1 -0
- package/dist/core/node/livelinessChecking.d.ts +37 -0
- package/dist/core/node/livelinessChecking.js +38 -0
- package/dist/core/node/livelinessChecking.js.map +1 -0
- package/dist/core/node/node-utils.d.ts +83 -0
- package/dist/core/node/node-utils.js +165 -0
- package/dist/core/node/node-utils.js.map +1 -0
- package/dist/core/node/object-node.d.ts +99 -0
- package/dist/core/node/object-node.js +545 -0
- package/dist/core/node/object-node.js.map +1 -0
- package/dist/core/node/scalar-node.d.ts +19 -0
- package/dist/core/node/scalar-node.js +89 -0
- package/dist/core/node/scalar-node.js.map +1 -0
- package/dist/core/process.d.ts +50 -0
- package/dist/core/process.js +39 -0
- package/dist/core/process.js.map +1 -0
- package/dist/core/type/type-checker.d.ts +69 -0
- package/dist/core/type/type-checker.js +154 -0
- package/dist/core/type/type-checker.js.map +1 -0
- package/dist/core/type/type.d.ts +317 -0
- package/dist/core/type/type.js +251 -0
- package/dist/core/type/type.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +39 -0
- package/dist/internal.js +60 -0
- package/dist/internal.js.map +1 -0
- package/dist/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/dist/middlewares/create-action-tracking-middleware.js +81 -0
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/dist/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js +133 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/dist/middlewares/on-action.d.ts +87 -0
- package/dist/middlewares/on-action.js +215 -0
- package/dist/middlewares/on-action.js.map +1 -0
- package/dist/mobx-state-tree.js +85 -0
- package/dist/mobx-state-tree.module.js +83 -0
- package/dist/types/complex-types/array.d.ts +80 -0
- package/dist/types/complex-types/array.js +352 -0
- package/dist/types/complex-types/array.js.map +1 -0
- package/dist/types/complex-types/map.d.ts +110 -0
- package/dist/types/complex-types/map.js +361 -0
- package/dist/types/complex-types/map.js.map +1 -0
- package/dist/types/complex-types/model.d.ts +192 -0
- package/dist/types/complex-types/model.js +477 -0
- package/dist/types/complex-types/model.js.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.js +38 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/primitives.d.ts +124 -0
- package/dist/types/primitives.js +182 -0
- package/dist/types/primitives.js.map +1 -0
- package/dist/types/utility-types/custom.d.ts +74 -0
- package/dist/types/utility-types/custom.js +110 -0
- package/dist/types/utility-types/custom.js.map +1 -0
- package/dist/types/utility-types/enumeration.d.ts +5 -0
- package/dist/types/utility-types/enumeration.js +34 -0
- package/dist/types/utility-types/enumeration.js.map +1 -0
- package/dist/types/utility-types/frozen.d.ts +23 -0
- package/dist/types/utility-types/frozen.js +97 -0
- package/dist/types/utility-types/frozen.js.map +1 -0
- package/dist/types/utility-types/identifier.d.ts +86 -0
- package/dist/types/utility-types/identifier.js +129 -0
- package/dist/types/utility-types/identifier.js.map +1 -0
- package/dist/types/utility-types/late.d.ts +10 -0
- package/dist/types/utility-types/late.js +109 -0
- package/dist/types/utility-types/late.js.map +1 -0
- package/dist/types/utility-types/lazy.d.ts +22 -0
- package/dist/types/utility-types/lazy.js +76 -0
- package/dist/types/utility-types/lazy.js.map +1 -0
- package/dist/types/utility-types/literal.d.ts +37 -0
- package/dist/types/utility-types/literal.js +63 -0
- package/dist/types/utility-types/literal.js.map +1 -0
- package/dist/types/utility-types/maybe.d.ts +26 -0
- package/dist/types/utility-types/maybe.js +30 -0
- package/dist/types/utility-types/maybe.js.map +1 -0
- package/dist/types/utility-types/optional.d.ts +41 -0
- package/dist/types/utility-types/optional.js +140 -0
- package/dist/types/utility-types/optional.js.map +1 -0
- package/dist/types/utility-types/reference.d.ts +89 -0
- package/dist/types/utility-types/reference.js +392 -0
- package/dist/types/utility-types/reference.js.map +1 -0
- package/dist/types/utility-types/refinement.d.ts +10 -0
- package/dist/types/utility-types/refinement.js +85 -0
- package/dist/types/utility-types/refinement.js.map +1 -0
- package/dist/types/utility-types/resilient.d.ts +18 -0
- package/dist/types/utility-types/resilient.js +120 -0
- package/dist/types/utility-types/resilient.js.map +1 -0
- package/dist/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/dist/types/utility-types/snapshotProcessor.js +161 -0
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -0
- package/dist/types/utility-types/union.d.ts +77 -0
- package/dist/types/utility-types/union.js +245 -0
- package/dist/types/utility-types/union.js.map +1 -0
- package/dist/utils.d.ts +230 -0
- package/dist/utils.js +483 -0
- package/dist/utils.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { SimpleType, TypeFlags } from "../internal.ts";
|
|
2
|
+
import type { AnyObjectNode, ISimpleType, IType, IValidationContext, IValidationResult } from "../internal.ts";
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
* @hidden
|
|
6
|
+
*/
|
|
7
|
+
export declare class CoreType<C, S, T> extends SimpleType<C, S, T> {
|
|
8
|
+
readonly flags: TypeFlags;
|
|
9
|
+
private readonly checker;
|
|
10
|
+
private readonly initializer;
|
|
11
|
+
constructor(name: string, flags: TypeFlags, checker: (value: C) => boolean, initializer?: (v: C) => T);
|
|
12
|
+
describe(): string;
|
|
13
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: C): this["N"];
|
|
14
|
+
createNewInstance(snapshot: C): T;
|
|
15
|
+
isValidSnapshot(value: C, context: IValidationContext): IValidationResult;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* `types.string` - Creates a type that can only contain a string value.
|
|
19
|
+
* This type is used for string values by default
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
* ```ts
|
|
23
|
+
* const Person = types.model({
|
|
24
|
+
* firstName: types.string,
|
|
25
|
+
* lastName: "Doe"
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare const string: ISimpleType<string>;
|
|
30
|
+
/**
|
|
31
|
+
* `types.number` - Creates a type that can only contain a numeric value.
|
|
32
|
+
* This type is used for numeric values by default
|
|
33
|
+
*
|
|
34
|
+
* Example:
|
|
35
|
+
* ```ts
|
|
36
|
+
* const Vector = types.model({
|
|
37
|
+
* x: types.number,
|
|
38
|
+
* y: 1.5
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare const number: ISimpleType<number>;
|
|
43
|
+
/**
|
|
44
|
+
* `types.integer` - Creates a type that can only contain an integer value.
|
|
45
|
+
*
|
|
46
|
+
* Example:
|
|
47
|
+
* ```ts
|
|
48
|
+
* const Size = types.model({
|
|
49
|
+
* width: types.integer,
|
|
50
|
+
* height: 10
|
|
51
|
+
* })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const integer: ISimpleType<number>;
|
|
55
|
+
/**
|
|
56
|
+
* `types.float` - Creates a type that can only contain an float value.
|
|
57
|
+
*
|
|
58
|
+
* Example:
|
|
59
|
+
* ```ts
|
|
60
|
+
* const Size = types.model({
|
|
61
|
+
* width: types.float,
|
|
62
|
+
* height: 10
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare const float: ISimpleType<number>;
|
|
67
|
+
/**
|
|
68
|
+
* `types.finite` - Creates a type that can only contain an finite value.
|
|
69
|
+
*
|
|
70
|
+
* Example:
|
|
71
|
+
* ```ts
|
|
72
|
+
* const Size = types.model({
|
|
73
|
+
* width: types.finite,
|
|
74
|
+
* height: 10
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare const finite: ISimpleType<number>;
|
|
79
|
+
/**
|
|
80
|
+
* `types.boolean` - Creates a type that can only contain a boolean value.
|
|
81
|
+
* This type is used for boolean values by default
|
|
82
|
+
*
|
|
83
|
+
* Example:
|
|
84
|
+
* ```ts
|
|
85
|
+
* const Thing = types.model({
|
|
86
|
+
* isCool: types.boolean,
|
|
87
|
+
* isAwesome: false
|
|
88
|
+
* })
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare const boolean: ISimpleType<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
* `types.null` - The type of the value `null`
|
|
94
|
+
*/
|
|
95
|
+
export declare const nullType: ISimpleType<null>;
|
|
96
|
+
/**
|
|
97
|
+
* `types.undefined` - The type of the value `undefined`
|
|
98
|
+
*/
|
|
99
|
+
export declare const undefinedType: ISimpleType<undefined>;
|
|
100
|
+
/**
|
|
101
|
+
* `types.Date` - Creates a type that can only contain a javascript Date value.
|
|
102
|
+
*
|
|
103
|
+
* Example:
|
|
104
|
+
* ```ts
|
|
105
|
+
* const LogLine = types.model({
|
|
106
|
+
* timestamp: types.Date,
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* LogLine.create({ timestamp: new Date() })
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare const DatePrimitive: IType<number | Date, number, Date>;
|
|
113
|
+
/**
|
|
114
|
+
* @internal
|
|
115
|
+
* @hidden
|
|
116
|
+
*/
|
|
117
|
+
export declare function getPrimitiveFactoryFromValue(value: any): ISimpleType<any>;
|
|
118
|
+
/**
|
|
119
|
+
* Returns if a given value represents a primitive type.
|
|
120
|
+
*
|
|
121
|
+
* @param type
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
export declare function isPrimitiveType<IT extends ISimpleType<string> | ISimpleType<number> | ISimpleType<boolean> | typeof DatePrimitive>(type: IT): type is IT;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatePrimitive = exports.undefinedType = exports.nullType = exports.boolean = exports.finite = exports.float = exports.integer = exports.number = exports.string = exports.CoreType = void 0;
|
|
4
|
+
exports.getPrimitiveFactoryFromValue = getPrimitiveFactoryFromValue;
|
|
5
|
+
exports.isPrimitiveType = isPrimitiveType;
|
|
6
|
+
const internal_ts_1 = require("../internal.js");
|
|
7
|
+
// TODO: implement CoreType using types.custom ?
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
* @hidden
|
|
11
|
+
*/
|
|
12
|
+
class CoreType extends internal_ts_1.SimpleType {
|
|
13
|
+
flags;
|
|
14
|
+
checker;
|
|
15
|
+
initializer;
|
|
16
|
+
constructor(name, flags, checker, initializer = internal_ts_1.identity) {
|
|
17
|
+
super(name);
|
|
18
|
+
this.flags = flags;
|
|
19
|
+
this.checker = checker;
|
|
20
|
+
this.initializer = initializer;
|
|
21
|
+
this.flags = flags;
|
|
22
|
+
}
|
|
23
|
+
describe() {
|
|
24
|
+
return this.name;
|
|
25
|
+
}
|
|
26
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
27
|
+
return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, initialValue);
|
|
28
|
+
}
|
|
29
|
+
createNewInstance(snapshot) {
|
|
30
|
+
return this.initializer(snapshot);
|
|
31
|
+
}
|
|
32
|
+
isValidSnapshot(value, context) {
|
|
33
|
+
if ((0, internal_ts_1.isPrimitive)(value) && this.checker(value)) {
|
|
34
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
35
|
+
}
|
|
36
|
+
const typeName = this.name === "Date" ? "Date or a unix milliseconds timestamp" : this.name;
|
|
37
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, `Value is not a ${typeName}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.CoreType = CoreType;
|
|
41
|
+
/**
|
|
42
|
+
* `types.string` - Creates a type that can only contain a string value.
|
|
43
|
+
* This type is used for string values by default
|
|
44
|
+
*
|
|
45
|
+
* Example:
|
|
46
|
+
* ```ts
|
|
47
|
+
* const Person = types.model({
|
|
48
|
+
* firstName: types.string,
|
|
49
|
+
* lastName: "Doe"
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
// tslint:disable-next-line:variable-name
|
|
54
|
+
exports.string = new CoreType("string", internal_ts_1.TypeFlags.String, v => typeof v === "string");
|
|
55
|
+
/**
|
|
56
|
+
* `types.number` - Creates a type that can only contain a numeric value.
|
|
57
|
+
* This type is used for numeric values by default
|
|
58
|
+
*
|
|
59
|
+
* Example:
|
|
60
|
+
* ```ts
|
|
61
|
+
* const Vector = types.model({
|
|
62
|
+
* x: types.number,
|
|
63
|
+
* y: 1.5
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
// tslint:disable-next-line:variable-name
|
|
68
|
+
exports.number = new CoreType("number", internal_ts_1.TypeFlags.Number, v => typeof v === "number");
|
|
69
|
+
/**
|
|
70
|
+
* `types.integer` - Creates a type that can only contain an integer value.
|
|
71
|
+
*
|
|
72
|
+
* Example:
|
|
73
|
+
* ```ts
|
|
74
|
+
* const Size = types.model({
|
|
75
|
+
* width: types.integer,
|
|
76
|
+
* height: 10
|
|
77
|
+
* })
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
// tslint:disable-next-line:variable-name
|
|
81
|
+
exports.integer = new CoreType("integer", internal_ts_1.TypeFlags.Integer, v => (0, internal_ts_1.isInteger)(v));
|
|
82
|
+
/**
|
|
83
|
+
* `types.float` - Creates a type that can only contain an float value.
|
|
84
|
+
*
|
|
85
|
+
* Example:
|
|
86
|
+
* ```ts
|
|
87
|
+
* const Size = types.model({
|
|
88
|
+
* width: types.float,
|
|
89
|
+
* height: 10
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
// tslint:disable-next-line:variable-name
|
|
94
|
+
exports.float = new CoreType("float", internal_ts_1.TypeFlags.Float, v => (0, internal_ts_1.isFloat)(v));
|
|
95
|
+
/**
|
|
96
|
+
* `types.finite` - Creates a type that can only contain an finite value.
|
|
97
|
+
*
|
|
98
|
+
* Example:
|
|
99
|
+
* ```ts
|
|
100
|
+
* const Size = types.model({
|
|
101
|
+
* width: types.finite,
|
|
102
|
+
* height: 10
|
|
103
|
+
* })
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
// tslint:disable-next-line:variable-name
|
|
107
|
+
exports.finite = new CoreType("finite", internal_ts_1.TypeFlags.Finite, v => (0, internal_ts_1.isFinite)(v));
|
|
108
|
+
/**
|
|
109
|
+
* `types.boolean` - Creates a type that can only contain a boolean value.
|
|
110
|
+
* This type is used for boolean values by default
|
|
111
|
+
*
|
|
112
|
+
* Example:
|
|
113
|
+
* ```ts
|
|
114
|
+
* const Thing = types.model({
|
|
115
|
+
* isCool: types.boolean,
|
|
116
|
+
* isAwesome: false
|
|
117
|
+
* })
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
// tslint:disable-next-line:variable-name
|
|
121
|
+
exports.boolean = new CoreType("boolean", internal_ts_1.TypeFlags.Boolean, v => typeof v === "boolean");
|
|
122
|
+
/**
|
|
123
|
+
* `types.null` - The type of the value `null`
|
|
124
|
+
*/
|
|
125
|
+
exports.nullType = new CoreType("null", internal_ts_1.TypeFlags.Null, v => v === null);
|
|
126
|
+
/**
|
|
127
|
+
* `types.undefined` - The type of the value `undefined`
|
|
128
|
+
*/
|
|
129
|
+
exports.undefinedType = new CoreType("undefined", internal_ts_1.TypeFlags.Undefined, v => v === undefined);
|
|
130
|
+
const _DatePrimitive = new CoreType("Date", internal_ts_1.TypeFlags.Date, v => typeof v === "number" || v instanceof Date, v => (v instanceof Date ? v : new Date(v)));
|
|
131
|
+
_DatePrimitive.getSnapshot = function (node) {
|
|
132
|
+
return node.storedValue.getTime();
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* `types.Date` - Creates a type that can only contain a javascript Date value.
|
|
136
|
+
*
|
|
137
|
+
* Example:
|
|
138
|
+
* ```ts
|
|
139
|
+
* const LogLine = types.model({
|
|
140
|
+
* timestamp: types.Date,
|
|
141
|
+
* })
|
|
142
|
+
*
|
|
143
|
+
* LogLine.create({ timestamp: new Date() })
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
exports.DatePrimitive = _DatePrimitive;
|
|
147
|
+
/**
|
|
148
|
+
* @internal
|
|
149
|
+
* @hidden
|
|
150
|
+
*/
|
|
151
|
+
function getPrimitiveFactoryFromValue(value) {
|
|
152
|
+
switch (typeof value) {
|
|
153
|
+
case "string":
|
|
154
|
+
return exports.string;
|
|
155
|
+
case "number":
|
|
156
|
+
return exports.number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
|
|
157
|
+
case "boolean":
|
|
158
|
+
return exports.boolean;
|
|
159
|
+
case "object":
|
|
160
|
+
if (value instanceof Date) {
|
|
161
|
+
return exports.DatePrimitive;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
throw (0, internal_ts_1.fail)("Cannot determine primitive type from value " + value);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns if a given value represents a primitive type.
|
|
168
|
+
*
|
|
169
|
+
* @param type
|
|
170
|
+
* @returns
|
|
171
|
+
*/
|
|
172
|
+
function isPrimitiveType(type) {
|
|
173
|
+
return ((0, internal_ts_1.isType)(type) &&
|
|
174
|
+
(type.flags &
|
|
175
|
+
(internal_ts_1.TypeFlags.String |
|
|
176
|
+
internal_ts_1.TypeFlags.Number |
|
|
177
|
+
internal_ts_1.TypeFlags.Integer |
|
|
178
|
+
internal_ts_1.TypeFlags.Boolean |
|
|
179
|
+
internal_ts_1.TypeFlags.Date)) >
|
|
180
|
+
0);
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=primitives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":";;;AAgOA,oEAcC;AAQD,0CAiBC;AAvQD,gDAauB;AAWvB,gDAAgD;AAChD;;;GAGG;AACH,MAAa,QAAkB,SAAQ,wBAAmB;IAG7C;IACQ;IACA;IAJnB,YACE,IAAY,EACH,KAAgB,EACR,OAA8B,EAC9B,cAA2B,sBAAQ;QAEpD,KAAK,CAAC,IAAI,CAAC,CAAA;QAJF,UAAK,GAAL,KAAK,CAAW;QACR,YAAO,GAAP,OAAO,CAAuB;QAC9B,gBAAW,GAAX,WAAW,CAAwB;QAGpD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,YAAe;QAEf,OAAO,IAAA,8BAAgB,EAAC,IAAI,EA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AAE/E;;;GAGG;AACH,SAAgB,4BAA4B,CAAC,KAAU;IACrD,QAAQ,OAAO,KAAK,EAAE,CAAC;QACrB,KAAK,QAAQ;YACX,OAAO,cAAM,CAAA;QACf,KAAK,QAAQ;YACX,OAAO,cAAM,CAAA,CAAC,6GAA6G;QAC7H,KAAK,SAAS;YACZ,OAAO,eAAO,CAAA;QAChB,KAAK,QAAQ;YACX,IAAI,KAAK,YAAY,IAAI,EAAE,CAAC;gBAC1B,OAAO,qBAAa,CAAA;YACtB,CAAC;IACL,CAAC;IACD,MAAM,IAAA,kBAAI,EAAC,6CAA6C,GAAG,KAAK,CAAC,CAAA;AACnE,CAAC;AAED;;;;;GAKG;AACH,SAAgB,eAAe,CAM7B,IAAQ;IACR,OAAO,CACL,IAAA,oBAAM,EAAC,IAAI,CAAC;QACZ,CAAC,IAAI,CAAC,KAAK;YACT,CAAC,uBAAS,CAAC,MAAM;gBACf,uBAAS,CAAC,MAAM;gBAChB,uBAAS,CAAC,OAAO;gBACjB,uBAAS,CAAC,OAAO;gBACjB,uBAAS,CAAC,IAAI,CAAC,CAAC;YAClB,CAAC,CACJ,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type AnyObjectNode, type IType, type IValidationContext, type IValidationResult, SimpleType, TypeFlags } from "../../internal.ts";
|
|
2
|
+
export interface CustomTypeOptions<S, T> {
|
|
3
|
+
/** Friendly name */
|
|
4
|
+
name: string;
|
|
5
|
+
/** given a serialized value and environment, how to turn it into the target type */
|
|
6
|
+
fromSnapshot(snapshot: S, env?: any): T;
|
|
7
|
+
/** return the serialization of the current value */
|
|
8
|
+
toSnapshot(value: T): S;
|
|
9
|
+
/** if true, this is a converted value, if false, it's a snapshot */
|
|
10
|
+
isTargetType(value: T | S): boolean;
|
|
11
|
+
/** a non empty string is assumed to be a validation error */
|
|
12
|
+
getValidationMessage(snapshot: S): string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* `types.custom` - Creates a custom type. Custom types can be used for arbitrary immutable values, that have a serializable representation. For example, to create your own Date representation, Decimal type etc.
|
|
16
|
+
*
|
|
17
|
+
* The signature of the options is:
|
|
18
|
+
* ```ts
|
|
19
|
+
* export interface CustomTypeOptions<S, T> {
|
|
20
|
+
* // Friendly name
|
|
21
|
+
* name: string
|
|
22
|
+
* // given a serialized value and environment, how to turn it into the target type
|
|
23
|
+
* fromSnapshot(snapshot: S, env: any): T
|
|
24
|
+
* // return the serialization of the current value
|
|
25
|
+
* toSnapshot(value: T): S
|
|
26
|
+
* // if true, this is a converted value, if false, it's a snapshot
|
|
27
|
+
* isTargetType(value: T | S): value is T
|
|
28
|
+
* // a non empty string is assumed to be a validation error
|
|
29
|
+
* getValidationMessage?(snapshot: S): string
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Example:
|
|
34
|
+
* ```ts
|
|
35
|
+
* const DecimalPrimitive = types.custom<string, Decimal>({
|
|
36
|
+
* name: "Decimal",
|
|
37
|
+
* fromSnapshot(value: string) {
|
|
38
|
+
* return new Decimal(value)
|
|
39
|
+
* },
|
|
40
|
+
* toSnapshot(value: Decimal) {
|
|
41
|
+
* return value.toString()
|
|
42
|
+
* },
|
|
43
|
+
* isTargetType(value: string | Decimal): boolean {
|
|
44
|
+
* return value instanceof Decimal
|
|
45
|
+
* },
|
|
46
|
+
* getValidationMessage(value: string): string {
|
|
47
|
+
* if (/^-?\d+\.\d+$/.test(value)) return "" // OK
|
|
48
|
+
* return `'${value}' doesn't look like a valid decimal number`
|
|
49
|
+
* }
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* const Wallet = types.model({
|
|
53
|
+
* balance: DecimalPrimitive
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @param options
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
export declare function custom<S, T>(options: CustomTypeOptions<S, T>): IType<S | T, S, T>;
|
|
61
|
+
/**
|
|
62
|
+
* @internal
|
|
63
|
+
* @hidden
|
|
64
|
+
*/
|
|
65
|
+
export declare class CustomType<S, T> extends SimpleType<S | T, S, T> {
|
|
66
|
+
protected readonly options: CustomTypeOptions<S, T>;
|
|
67
|
+
readonly flags = TypeFlags.Custom;
|
|
68
|
+
constructor(options: CustomTypeOptions<S, T>);
|
|
69
|
+
describe(): string;
|
|
70
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
71
|
+
getSnapshot(node: this["N"]): S;
|
|
72
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: S | T): this["N"];
|
|
73
|
+
reconcile(current: this["N"], value: S | T, parent: AnyObjectNode, subpath: string): this["N"];
|
|
74
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomType = void 0;
|
|
4
|
+
exports.custom = custom;
|
|
5
|
+
const internal_ts_1 = require("../../internal.js");
|
|
6
|
+
/**
|
|
7
|
+
* `types.custom` - Creates a custom type. Custom types can be used for arbitrary immutable values, that have a serializable representation. For example, to create your own Date representation, Decimal type etc.
|
|
8
|
+
*
|
|
9
|
+
* The signature of the options is:
|
|
10
|
+
* ```ts
|
|
11
|
+
* export interface CustomTypeOptions<S, T> {
|
|
12
|
+
* // Friendly name
|
|
13
|
+
* name: string
|
|
14
|
+
* // given a serialized value and environment, how to turn it into the target type
|
|
15
|
+
* fromSnapshot(snapshot: S, env: any): T
|
|
16
|
+
* // return the serialization of the current value
|
|
17
|
+
* toSnapshot(value: T): S
|
|
18
|
+
* // if true, this is a converted value, if false, it's a snapshot
|
|
19
|
+
* isTargetType(value: T | S): value is T
|
|
20
|
+
* // a non empty string is assumed to be a validation error
|
|
21
|
+
* getValidationMessage?(snapshot: S): string
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Example:
|
|
26
|
+
* ```ts
|
|
27
|
+
* const DecimalPrimitive = types.custom<string, Decimal>({
|
|
28
|
+
* name: "Decimal",
|
|
29
|
+
* fromSnapshot(value: string) {
|
|
30
|
+
* return new Decimal(value)
|
|
31
|
+
* },
|
|
32
|
+
* toSnapshot(value: Decimal) {
|
|
33
|
+
* return value.toString()
|
|
34
|
+
* },
|
|
35
|
+
* isTargetType(value: string | Decimal): boolean {
|
|
36
|
+
* return value instanceof Decimal
|
|
37
|
+
* },
|
|
38
|
+
* getValidationMessage(value: string): string {
|
|
39
|
+
* if (/^-?\d+\.\d+$/.test(value)) return "" // OK
|
|
40
|
+
* return `'${value}' doesn't look like a valid decimal number`
|
|
41
|
+
* }
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* const Wallet = types.model({
|
|
45
|
+
* balance: DecimalPrimitive
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param options
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
function custom(options) {
|
|
53
|
+
return new CustomType(options);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
* @hidden
|
|
58
|
+
*/
|
|
59
|
+
class CustomType extends internal_ts_1.SimpleType {
|
|
60
|
+
options;
|
|
61
|
+
flags = internal_ts_1.TypeFlags.Custom;
|
|
62
|
+
constructor(options) {
|
|
63
|
+
super(options.name);
|
|
64
|
+
this.options = options;
|
|
65
|
+
}
|
|
66
|
+
describe() {
|
|
67
|
+
return this.name;
|
|
68
|
+
}
|
|
69
|
+
isValidSnapshot(value, context) {
|
|
70
|
+
if (this.options.isTargetType(value)) {
|
|
71
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
72
|
+
}
|
|
73
|
+
const typeError = this.options.getValidationMessage(value);
|
|
74
|
+
if (typeError) {
|
|
75
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, `Invalid value for type '${this.name}': ${typeError}`);
|
|
76
|
+
}
|
|
77
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
78
|
+
}
|
|
79
|
+
getSnapshot(node) {
|
|
80
|
+
return this.options.toSnapshot(node.storedValue);
|
|
81
|
+
}
|
|
82
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
83
|
+
const valueToStore = this.options.isTargetType(initialValue)
|
|
84
|
+
? initialValue
|
|
85
|
+
: this.options.fromSnapshot(initialValue, parent && parent.root.environment);
|
|
86
|
+
return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, valueToStore);
|
|
87
|
+
}
|
|
88
|
+
reconcile(current, value, parent, subpath) {
|
|
89
|
+
const isSnapshot = !this.options.isTargetType(value);
|
|
90
|
+
// in theory customs use scalar nodes which cannot be detached, but still...
|
|
91
|
+
if (!current.isDetaching) {
|
|
92
|
+
const unchanged = current.type === this &&
|
|
93
|
+
(isSnapshot
|
|
94
|
+
? value === current.snapshot
|
|
95
|
+
: value === current.storedValue);
|
|
96
|
+
if (unchanged) {
|
|
97
|
+
current.setParent(parent, subpath);
|
|
98
|
+
return current;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const valueToStore = isSnapshot
|
|
102
|
+
? this.options.fromSnapshot(value, parent.root.environment)
|
|
103
|
+
: value;
|
|
104
|
+
const newNode = this.instantiate(parent, subpath, undefined, valueToStore);
|
|
105
|
+
current.die(); // noop if detaching
|
|
106
|
+
return newNode;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.CustomType = CustomType;
|
|
110
|
+
//# sourceMappingURL=custom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom.js","sourceRoot":"","sources":["../../../src/types/utility-types/custom.ts"],"names":[],"mappings":";;;AAyEA,wBAIC;AA7ED,mDAU0B;AAiB1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,SAAgB,MAAM,CACpB,OAAgC;IAEhC,OAAO,IAAI,UAAU,CAAO,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AAA;IAC3E,CAAC;IAED,SAAS,CACP,OAAkB,EAClB,KAAY,EACZ,MAAqB,EACrB,OAAe;QAEf,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;QACpD,4EAA4E;QAC5E,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;YACzB,MAAM,SAAS,GACb,OAAO,CAAC,IAAI,KAAK,IAAI;gBACrB,CAAC,UAAU;oBACT,CAAC,CAAC,KAAK,KAAK,OAAO,CAAC,QAAQ;oBAC5B,CAAC,CAAC,KAAK,KAAK,OAAO,CAAC,WAAW,CAAC,CAAA;YACpC,IAAI,SAAS,EAAE,CAAC;gBACd,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;gBAClC,OAAO,OAAO,CAAA;YAChB,CAAC;QACH,CAAC;QACD,MAAM,YAAY,GAAM,UAAU;YAChC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,KAAU,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;YAChE,CAAC,CAAE,KAAW,CAAA;QAChB,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,CAAC,CAAA;QAC1E,OAAO,CAAC,GAAG,EAAE,CAAA,CAAC,oBAAoB;QAClC,OAAO,OAAO,CAAA;IAChB,CAAC;CACF;AA3ED,gCA2EC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ISimpleType } from "../../internal.ts";
|
|
2
|
+
/** @hidden */
|
|
3
|
+
export type UnionStringArray<T extends readonly string[]> = T[number];
|
|
4
|
+
export declare function enumeration<T extends readonly string[]>(options: T): ISimpleType<UnionStringArray<T>>;
|
|
5
|
+
export declare function enumeration<T extends string>(name: string, options: T[]): ISimpleType<UnionStringArray<T[]>>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.enumeration = enumeration;
|
|
4
|
+
const internal_ts_1 = require("../../internal.js");
|
|
5
|
+
/**
|
|
6
|
+
* `types.enumeration` - Can be used to create an string based enumeration.
|
|
7
|
+
* (note: this methods is just sugar for a union of string literals)
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
* ```ts
|
|
11
|
+
* const TrafficLight = types.model({
|
|
12
|
+
* color: types.enumeration("Color", ["Red", "Orange", "Green"])
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param name descriptive name of the enumeration (optional)
|
|
17
|
+
* @param options possible values this enumeration can have
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
function enumeration(name, options) {
|
|
21
|
+
const realOptions = typeof name === "string" ? options : name;
|
|
22
|
+
// check all options
|
|
23
|
+
if ((0, internal_ts_1.devMode)()) {
|
|
24
|
+
realOptions.forEach((option, i) => {
|
|
25
|
+
(0, internal_ts_1.assertIsString)(option, i + 1);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const type = (0, internal_ts_1.union)(...realOptions.map(option => (0, internal_ts_1.literal)("" + option)));
|
|
29
|
+
if (typeof name === "string") {
|
|
30
|
+
type.name = name;
|
|
31
|
+
}
|
|
32
|
+
return type;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=enumeration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enumeration.js","sourceRoot":"","sources":["../../../src/types/utility-types/enumeration.ts"],"names":[],"mappings":";;AAuCA,kCAgBC;AAvDD,mDAM0B;AAkB1B;;;;;;;;;;;;;;GAcG;AACH,SAAgB,WAAW,CACzB,IAAuB,EACvB,OAAa;IAEb,MAAM,WAAW,GAAa,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAQ,CAAC,CAAC,CAAC,IAAI,CAAA;IACxE,oBAAoB;IACpB,IAAI,IAAA,qBAAO,GAAE,EAAE,CAAC;QACd,WAAW,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAChC,IAAA,4BAAc,EAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;QAC/B,CAAC,CAAC,CAAA;IACJ,CAAC;IACD,MAAM,IAAI,GAAG,IAAA,mBAAK,EAAC,GAAG,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAA,qBAAO,EAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IACtE,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type AnyObjectNode, type IAnyType, type IType, type IValidationContext, type IValidationResult, SimpleType, TypeFlags } from "../../internal.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
* @hidden
|
|
5
|
+
*/
|
|
6
|
+
export declare class Frozen<T> extends SimpleType<T, T, T> {
|
|
7
|
+
private subType?;
|
|
8
|
+
flags: TypeFlags;
|
|
9
|
+
constructor(subType?: IAnyType | undefined);
|
|
10
|
+
describe(): string;
|
|
11
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, value: this["C"]): this["N"];
|
|
12
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
13
|
+
}
|
|
14
|
+
export declare function frozen<C>(subType: IType<C, any, any>): IType<C, C, C>;
|
|
15
|
+
export declare function frozen<T>(defaultValue: T): IType<T | undefined | null, T, T>;
|
|
16
|
+
export declare function frozen<T = any>(): IType<T, T, T>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns if a given value represents a frozen type.
|
|
19
|
+
*
|
|
20
|
+
* @param type
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export declare function isFrozenType<IT extends IType<T | any, T, T>, T = any>(type: IT): type is IT;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Frozen = void 0;
|
|
4
|
+
exports.frozen = frozen;
|
|
5
|
+
exports.isFrozenType = isFrozenType;
|
|
6
|
+
const internal_ts_1 = require("../../internal.js");
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* @hidden
|
|
10
|
+
*/
|
|
11
|
+
class Frozen extends internal_ts_1.SimpleType {
|
|
12
|
+
subType;
|
|
13
|
+
flags = internal_ts_1.TypeFlags.Frozen;
|
|
14
|
+
constructor(subType) {
|
|
15
|
+
super(subType ? `frozen(${subType.name})` : "frozen");
|
|
16
|
+
this.subType = subType;
|
|
17
|
+
}
|
|
18
|
+
describe() {
|
|
19
|
+
return "<any immutable value>";
|
|
20
|
+
}
|
|
21
|
+
instantiate(parent, subpath, environment, value) {
|
|
22
|
+
// create the node
|
|
23
|
+
return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, (0, internal_ts_1.deepFreeze)(value));
|
|
24
|
+
}
|
|
25
|
+
isValidSnapshot(value, context) {
|
|
26
|
+
if (!(0, internal_ts_1.isSerializable)(value)) {
|
|
27
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, "Value is not serializable and cannot be frozen");
|
|
28
|
+
}
|
|
29
|
+
if (this.subType) {
|
|
30
|
+
return this.subType.validate(value, context);
|
|
31
|
+
}
|
|
32
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.Frozen = Frozen;
|
|
36
|
+
const untypedFrozenInstance = new Frozen();
|
|
37
|
+
/**
|
|
38
|
+
* `types.frozen` - Frozen can be used to store any value that is serializable in itself (that is valid JSON).
|
|
39
|
+
* Frozen values need to be immutable or treated as if immutable. They need be serializable as well.
|
|
40
|
+
* Values stored in frozen will snapshotted as-is by MST, and internal changes will not be tracked.
|
|
41
|
+
*
|
|
42
|
+
* This is useful to store complex, but immutable values like vectors etc. It can form a powerful bridge to parts of your application that should be immutable, or that assume data to be immutable.
|
|
43
|
+
*
|
|
44
|
+
* Note: if you want to store free-form state that is mutable, or not serializeable, consider using volatile state instead.
|
|
45
|
+
*
|
|
46
|
+
* Frozen properties can be defined in three different ways
|
|
47
|
+
* 1. `types.frozen(SubType)` - provide a valid MST type and frozen will check if the provided data conforms the snapshot for that type
|
|
48
|
+
* 2. `types.frozen({ someDefaultValue: true})` - provide a primitive value, object or array, and MST will infer the type from that object, and also make it the default value for the field
|
|
49
|
+
* 3. `types.frozen<TypeScriptType>()` - provide a typescript type, to help in strongly typing the field (design time only)
|
|
50
|
+
*
|
|
51
|
+
* Example:
|
|
52
|
+
* ```ts
|
|
53
|
+
* const GameCharacter = types.model({
|
|
54
|
+
* name: string,
|
|
55
|
+
* location: types.frozen({ x: 0, y: 0})
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* const hero = GameCharacter.create({
|
|
59
|
+
* name: "Mario",
|
|
60
|
+
* location: { x: 7, y: 4 }
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* hero.location = { x: 10, y: 2 } // OK
|
|
64
|
+
* hero.location.x = 7 // Not ok!
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* type Point = { x: number, y: number }
|
|
69
|
+
* const Mouse = types.model({
|
|
70
|
+
* loc: types.frozen<Point>()
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @param defaultValueOrType
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
function frozen(arg) {
|
|
78
|
+
if (arguments.length === 0) {
|
|
79
|
+
return untypedFrozenInstance;
|
|
80
|
+
}
|
|
81
|
+
else if ((0, internal_ts_1.isType)(arg)) {
|
|
82
|
+
return new Frozen(arg);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return (0, internal_ts_1.optional)(untypedFrozenInstance, arg);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns if a given value represents a frozen type.
|
|
90
|
+
*
|
|
91
|
+
* @param type
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
function isFrozenType(type) {
|
|
95
|
+
return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Frozen) > 0;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=frozen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frozen.js","sourceRoot":"","sources":["../../../src/types/utility-types/frozen.ts"],"names":[],"mappings":";;;AA+GA,wBAQC;AAQD,oCAIC;AAnID,mDAe0B;AAE1B;;;GAGG;AACH,MAAa,MAAU,SAAQ,wBAAmB;IAG5B;IAFpB,KAAK,GAAG,uBAAS,CAAC,MAAM,CAAA;IAExB,YAAoB,OAAkB;QACpC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;QADnC,YAAO,GAAP,OAAO,CAAW;IAEtC,CAAC;IAED,QAAQ;QACN,OAAO,uBAAuB,CAAA;IAChC,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,KAAgB;QAEhB,kBAAkB;QAClB,OAAO,IAAA,8BAAgB,EACrB,IAAI,EACJ,MAAM,EACN,OAAO,EACP,WAAW,EACX,IAAA,wBAAU,EAAC,KAAK,CAAC,CAClB,CAAA;IACH,CAAC;IAED,eAAe,CACb,KAAgB,EAChB,OAA2B;QAE3B,IAAI,CAAC,IAAA,4BAAc,EAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAA,8BAAgB,EACrB,OAAO,EACP,KAAK,EACL,gDAAgD,CACjD,CAAA;Q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