@jbrowse/mobx-state-tree 5.7.0 → 5.8.6

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.
Files changed (253) hide show
  1. package/README.md +0 -10
  2. package/dist/index.d.ts +2135 -2
  3. package/dist/mobx-state-tree.cjs +6894 -0
  4. package/dist/mobx-state-tree.cjs.map +1 -0
  5. package/dist/mobx-state-tree.mjs +6814 -0
  6. package/dist/mobx-state-tree.mjs.map +1 -0
  7. package/package.json +30 -26
  8. package/dist/core/action.d.ts +0 -87
  9. package/dist/core/action.js +0 -228
  10. package/dist/core/action.js.map +0 -1
  11. package/dist/core/actionContext.d.ts +0 -27
  12. package/dist/core/actionContext.js +0 -42
  13. package/dist/core/actionContext.js.map +0 -1
  14. package/dist/core/flow.d.ts +0 -69
  15. package/dist/core/flow.js +0 -180
  16. package/dist/core/flow.js.map +0 -1
  17. package/dist/core/json-patch.d.ts +0 -46
  18. package/dist/core/json-patch.js +0 -133
  19. package/dist/core/json-patch.js.map +0 -1
  20. package/dist/core/mst-operations.d.ts +0 -459
  21. package/dist/core/mst-operations.js +0 -885
  22. package/dist/core/mst-operations.js.map +0 -1
  23. package/dist/core/node/BaseNode.d.ts +0 -62
  24. package/dist/core/node/BaseNode.js +0 -152
  25. package/dist/core/node/BaseNode.js.map +0 -1
  26. package/dist/core/node/Hook.d.ts +0 -17
  27. package/dist/core/node/Hook.js +0 -15
  28. package/dist/core/node/Hook.js.map +0 -1
  29. package/dist/core/node/create-node.d.ts +0 -16
  30. package/dist/core/node/create-node.js +0 -41
  31. package/dist/core/node/create-node.js.map +0 -1
  32. package/dist/core/node/identifier-cache.d.ts +0 -19
  33. package/dist/core/node/identifier-cache.js +0 -115
  34. package/dist/core/node/identifier-cache.js.map +0 -1
  35. package/dist/core/node/livelinessChecking.d.ts +0 -37
  36. package/dist/core/node/livelinessChecking.js +0 -38
  37. package/dist/core/node/livelinessChecking.js.map +0 -1
  38. package/dist/core/node/node-utils.d.ts +0 -83
  39. package/dist/core/node/node-utils.js +0 -165
  40. package/dist/core/node/node-utils.js.map +0 -1
  41. package/dist/core/node/object-node.d.ts +0 -101
  42. package/dist/core/node/object-node.js +0 -546
  43. package/dist/core/node/object-node.js.map +0 -1
  44. package/dist/core/node/scalar-node.d.ts +0 -21
  45. package/dist/core/node/scalar-node.js +0 -90
  46. package/dist/core/node/scalar-node.js.map +0 -1
  47. package/dist/core/process.d.ts +0 -50
  48. package/dist/core/process.js +0 -39
  49. package/dist/core/process.js.map +0 -1
  50. package/dist/core/type/type-checker.d.ts +0 -69
  51. package/dist/core/type/type-checker.js +0 -154
  52. package/dist/core/type/type-checker.js.map +0 -1
  53. package/dist/core/type/type.d.ts +0 -317
  54. package/dist/core/type/type.js +0 -251
  55. package/dist/core/type/type.js.map +0 -1
  56. package/dist/index.js +0 -85
  57. package/dist/index.js.map +0 -1
  58. package/dist/internal.d.ts +0 -39
  59. package/dist/internal.js +0 -60
  60. package/dist/internal.js.map +0 -1
  61. package/dist/middlewares/create-action-tracking-middleware.d.ts +0 -24
  62. package/dist/middlewares/create-action-tracking-middleware.js +0 -81
  63. package/dist/middlewares/create-action-tracking-middleware.js.map +0 -1
  64. package/dist/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  65. package/dist/middlewares/createActionTrackingMiddleware2.js +0 -133
  66. package/dist/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  67. package/dist/middlewares/on-action.d.ts +0 -87
  68. package/dist/middlewares/on-action.js +0 -215
  69. package/dist/middlewares/on-action.js.map +0 -1
  70. package/dist/package.json +0 -1
  71. package/dist/types/complex-types/array.d.ts +0 -81
  72. package/dist/types/complex-types/array.js +0 -353
  73. package/dist/types/complex-types/array.js.map +0 -1
  74. package/dist/types/complex-types/map.d.ts +0 -111
  75. package/dist/types/complex-types/map.js +0 -362
  76. package/dist/types/complex-types/map.js.map +0 -1
  77. package/dist/types/complex-types/model.d.ts +0 -193
  78. package/dist/types/complex-types/model.js +0 -478
  79. package/dist/types/complex-types/model.js.map +0 -1
  80. package/dist/types/index.d.ts +0 -33
  81. package/dist/types/index.js +0 -38
  82. package/dist/types/index.js.map +0 -1
  83. package/dist/types/primitives.d.ts +0 -125
  84. package/dist/types/primitives.js +0 -183
  85. package/dist/types/primitives.js.map +0 -1
  86. package/dist/types/utility-types/custom.d.ts +0 -75
  87. package/dist/types/utility-types/custom.js +0 -111
  88. package/dist/types/utility-types/custom.js.map +0 -1
  89. package/dist/types/utility-types/enumeration.d.ts +0 -5
  90. package/dist/types/utility-types/enumeration.js +0 -34
  91. package/dist/types/utility-types/enumeration.js.map +0 -1
  92. package/dist/types/utility-types/frozen.d.ts +0 -24
  93. package/dist/types/utility-types/frozen.js +0 -98
  94. package/dist/types/utility-types/frozen.js.map +0 -1
  95. package/dist/types/utility-types/identifier.d.ts +0 -87
  96. package/dist/types/utility-types/identifier.js +0 -130
  97. package/dist/types/utility-types/identifier.js.map +0 -1
  98. package/dist/types/utility-types/late.d.ts +0 -10
  99. package/dist/types/utility-types/late.js +0 -110
  100. package/dist/types/utility-types/late.js.map +0 -1
  101. package/dist/types/utility-types/lazy.d.ts +0 -23
  102. package/dist/types/utility-types/lazy.js +0 -77
  103. package/dist/types/utility-types/lazy.js.map +0 -1
  104. package/dist/types/utility-types/literal.d.ts +0 -38
  105. package/dist/types/utility-types/literal.js +0 -64
  106. package/dist/types/utility-types/literal.js.map +0 -1
  107. package/dist/types/utility-types/maybe.d.ts +0 -26
  108. package/dist/types/utility-types/maybe.js +0 -30
  109. package/dist/types/utility-types/maybe.js.map +0 -1
  110. package/dist/types/utility-types/optional.d.ts +0 -42
  111. package/dist/types/utility-types/optional.js +0 -141
  112. package/dist/types/utility-types/optional.js.map +0 -1
  113. package/dist/types/utility-types/reference.d.ts +0 -90
  114. package/dist/types/utility-types/reference.js +0 -393
  115. package/dist/types/utility-types/reference.js.map +0 -1
  116. package/dist/types/utility-types/refinement.d.ts +0 -10
  117. package/dist/types/utility-types/refinement.js +0 -86
  118. package/dist/types/utility-types/refinement.js.map +0 -1
  119. package/dist/types/utility-types/resilient.d.ts +0 -18
  120. package/dist/types/utility-types/resilient.js +0 -121
  121. package/dist/types/utility-types/resilient.js.map +0 -1
  122. package/dist/types/utility-types/snapshotProcessor.d.ts +0 -63
  123. package/dist/types/utility-types/snapshotProcessor.js +0 -162
  124. package/dist/types/utility-types/snapshotProcessor.js.map +0 -1
  125. package/dist/types/utility-types/union.d.ts +0 -78
  126. package/dist/types/utility-types/union.js +0 -246
  127. package/dist/types/utility-types/union.js.map +0 -1
  128. package/dist/utils.d.ts +0 -230
  129. package/dist/utils.js +0 -483
  130. package/dist/utils.js.map +0 -1
  131. package/esm/core/action.d.ts +0 -87
  132. package/esm/core/action.js +0 -219
  133. package/esm/core/action.js.map +0 -1
  134. package/esm/core/actionContext.d.ts +0 -27
  135. package/esm/core/actionContext.js +0 -37
  136. package/esm/core/actionContext.js.map +0 -1
  137. package/esm/core/flow.d.ts +0 -69
  138. package/esm/core/flow.js +0 -173
  139. package/esm/core/flow.js.map +0 -1
  140. package/esm/core/json-patch.d.ts +0 -46
  141. package/esm/core/json-patch.js +0 -125
  142. package/esm/core/json-patch.js.map +0 -1
  143. package/esm/core/mst-operations.d.ts +0 -459
  144. package/esm/core/mst-operations.js +0 -844
  145. package/esm/core/mst-operations.js.map +0 -1
  146. package/esm/core/node/BaseNode.d.ts +0 -62
  147. package/esm/core/node/BaseNode.js +0 -148
  148. package/esm/core/node/BaseNode.js.map +0 -1
  149. package/esm/core/node/Hook.d.ts +0 -17
  150. package/esm/core/node/Hook.js +0 -12
  151. package/esm/core/node/Hook.js.map +0 -1
  152. package/esm/core/node/create-node.d.ts +0 -16
  153. package/esm/core/node/create-node.js +0 -36
  154. package/esm/core/node/create-node.js.map +0 -1
  155. package/esm/core/node/identifier-cache.d.ts +0 -19
  156. package/esm/core/node/identifier-cache.js +0 -111
  157. package/esm/core/node/identifier-cache.js.map +0 -1
  158. package/esm/core/node/livelinessChecking.d.ts +0 -37
  159. package/esm/core/node/livelinessChecking.js +0 -33
  160. package/esm/core/node/livelinessChecking.js.map +0 -1
  161. package/esm/core/node/node-utils.d.ts +0 -83
  162. package/esm/core/node/node-utils.js +0 -153
  163. package/esm/core/node/node-utils.js.map +0 -1
  164. package/esm/core/node/object-node.d.ts +0 -101
  165. package/esm/core/node/object-node.js +0 -542
  166. package/esm/core/node/object-node.js.map +0 -1
  167. package/esm/core/node/scalar-node.d.ts +0 -21
  168. package/esm/core/node/scalar-node.js +0 -86
  169. package/esm/core/node/scalar-node.js.map +0 -1
  170. package/esm/core/process.d.ts +0 -50
  171. package/esm/core/process.js +0 -35
  172. package/esm/core/process.js.map +0 -1
  173. package/esm/core/type/type-checker.d.ts +0 -69
  174. package/esm/core/type/type-checker.js +0 -144
  175. package/esm/core/type/type-checker.js.map +0 -1
  176. package/esm/core/type/type.d.ts +0 -317
  177. package/esm/core/type/type.js +0 -243
  178. package/esm/core/type/type.js.map +0 -1
  179. package/esm/index.d.ts +0 -2
  180. package/esm/index.js +0 -2
  181. package/esm/index.js.map +0 -1
  182. package/esm/internal.d.ts +0 -39
  183. package/esm/internal.js +0 -44
  184. package/esm/internal.js.map +0 -1
  185. package/esm/middlewares/create-action-tracking-middleware.d.ts +0 -24
  186. package/esm/middlewares/create-action-tracking-middleware.js +0 -78
  187. package/esm/middlewares/create-action-tracking-middleware.js.map +0 -1
  188. package/esm/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  189. package/esm/middlewares/createActionTrackingMiddleware2.js +0 -130
  190. package/esm/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  191. package/esm/middlewares/on-action.d.ts +0 -87
  192. package/esm/middlewares/on-action.js +0 -210
  193. package/esm/middlewares/on-action.js.map +0 -1
  194. package/esm/types/complex-types/array.d.ts +0 -81
  195. package/esm/types/complex-types/array.js +0 -347
  196. package/esm/types/complex-types/array.js.map +0 -1
  197. package/esm/types/complex-types/map.d.ts +0 -111
  198. package/esm/types/complex-types/map.js +0 -356
  199. package/esm/types/complex-types/map.js.map +0 -1
  200. package/esm/types/complex-types/model.d.ts +0 -193
  201. package/esm/types/complex-types/model.js +0 -471
  202. package/esm/types/complex-types/model.js.map +0 -1
  203. package/esm/types/index.d.ts +0 -33
  204. package/esm/types/index.js +0 -35
  205. package/esm/types/index.js.map +0 -1
  206. package/esm/types/primitives.d.ts +0 -125
  207. package/esm/types/primitives.js +0 -177
  208. package/esm/types/primitives.js.map +0 -1
  209. package/esm/types/utility-types/custom.d.ts +0 -75
  210. package/esm/types/utility-types/custom.js +0 -106
  211. package/esm/types/utility-types/custom.js.map +0 -1
  212. package/esm/types/utility-types/enumeration.d.ts +0 -5
  213. package/esm/types/utility-types/enumeration.js +0 -31
  214. package/esm/types/utility-types/enumeration.js.map +0 -1
  215. package/esm/types/utility-types/frozen.d.ts +0 -24
  216. package/esm/types/utility-types/frozen.js +0 -92
  217. package/esm/types/utility-types/frozen.js.map +0 -1
  218. package/esm/types/utility-types/identifier.d.ts +0 -87
  219. package/esm/types/utility-types/identifier.js +0 -121
  220. package/esm/types/utility-types/identifier.js.map +0 -1
  221. package/esm/types/utility-types/late.d.ts +0 -10
  222. package/esm/types/utility-types/late.js +0 -106
  223. package/esm/types/utility-types/late.js.map +0 -1
  224. package/esm/types/utility-types/lazy.d.ts +0 -23
  225. package/esm/types/utility-types/lazy.js +0 -72
  226. package/esm/types/utility-types/lazy.js.map +0 -1
  227. package/esm/types/utility-types/literal.d.ts +0 -38
  228. package/esm/types/utility-types/literal.js +0 -58
  229. package/esm/types/utility-types/literal.js.map +0 -1
  230. package/esm/types/utility-types/maybe.d.ts +0 -26
  231. package/esm/types/utility-types/maybe.js +0 -26
  232. package/esm/types/utility-types/maybe.js.map +0 -1
  233. package/esm/types/utility-types/optional.d.ts +0 -42
  234. package/esm/types/utility-types/optional.js +0 -135
  235. package/esm/types/utility-types/optional.js.map +0 -1
  236. package/esm/types/utility-types/reference.d.ts +0 -90
  237. package/esm/types/utility-types/reference.js +0 -383
  238. package/esm/types/utility-types/reference.js.map +0 -1
  239. package/esm/types/utility-types/refinement.d.ts +0 -10
  240. package/esm/types/utility-types/refinement.js +0 -82
  241. package/esm/types/utility-types/refinement.js.map +0 -1
  242. package/esm/types/utility-types/resilient.d.ts +0 -18
  243. package/esm/types/utility-types/resilient.js +0 -118
  244. package/esm/types/utility-types/resilient.js.map +0 -1
  245. package/esm/types/utility-types/snapshotProcessor.d.ts +0 -63
  246. package/esm/types/utility-types/snapshotProcessor.js +0 -159
  247. package/esm/types/utility-types/snapshotProcessor.js.map +0 -1
  248. package/esm/types/utility-types/union.d.ts +0 -78
  249. package/esm/types/utility-types/union.js +0 -240
  250. package/esm/types/utility-types/union.js.map +0 -1
  251. package/esm/utils.d.ts +0 -230
  252. package/esm/utils.js +0 -449
  253. package/esm/utils.js.map +0 -1
@@ -1,35 +0,0 @@
1
- // we import the types to re-export them inside types.
2
- import { DatePrimitive, array, boolean, compose, custom, enumeration, finite, float, frozen, identifier, identifierNumber, integer, late, lazy, literal, map, maybe, maybeNull, model, nullType, number, optional, reference, refinement, safeReference, resilient, snapshotProcessor, string, undefinedType, union } from "../internal.js";
3
- export const types = {
4
- enumeration,
5
- model,
6
- compose,
7
- custom,
8
- reference,
9
- safeReference,
10
- union,
11
- optional,
12
- literal,
13
- maybe,
14
- maybeNull,
15
- refinement,
16
- string,
17
- boolean,
18
- number,
19
- integer,
20
- float,
21
- finite,
22
- Date: DatePrimitive,
23
- map,
24
- array,
25
- frozen,
26
- identifier,
27
- identifierNumber,
28
- late,
29
- lazy,
30
- undefined: undefinedType,
31
- null: nullType,
32
- snapshotProcessor,
33
- resilient
34
- };
35
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,OAAO,EACL,aAAa,EACb,KAAK,EACL,OAAO,EACP,OAAO,EACP,MAAM,EACN,WAAW,EACX,MAAM,EACN,KAAK,EACL,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,OAAO,EACP,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,GAAG,EACH,KAAK,EACL,SAAS,EACT,KAAK,EACL,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,SAAS,EACT,UAAU,EACV,aAAa,EACb,SAAS,EACT,iBAAiB,EACjB,MAAM,EACN,aAAa,EACb,KAAK,EACN,MAAM,gBAAgB,CAAA;AAEvB,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,WAAW;IACX,KAAK;IACL,OAAO;IACP,MAAM;IACN,SAAS;IACT,aAAa;IACb,KAAK;IACL,QAAQ;IACR,OAAO;IACP,KAAK;IACL,SAAS;IACT,UAAU;IACV,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;IACP,KAAK;IACL,MAAM;IACN,IAAI,EAAE,aAAa;IACnB,GAAG;IACH,KAAK;IACL,MAAM;IACN,UAAU;IACV,gBAAgB;IAChB,IAAI;IACJ,IAAI;IACJ,SAAS,EAAE,aAAa;IACxB,IAAI,EAAE,QAAQ;IACd,iBAAiB;IACjB,SAAS;CACV,CAAA","sourcesContent":["// we import the types to re-export them inside types.\nimport {\n DatePrimitive,\n array,\n boolean,\n compose,\n custom,\n enumeration,\n finite,\n float,\n frozen,\n identifier,\n identifierNumber,\n integer,\n late,\n lazy,\n literal,\n map,\n maybe,\n maybeNull,\n model,\n nullType,\n number,\n optional,\n reference,\n refinement,\n safeReference,\n resilient,\n snapshotProcessor,\n string,\n undefinedType,\n union\n} from \"../internal.ts\"\n\nexport const types = {\n enumeration,\n model,\n compose,\n custom,\n reference,\n safeReference,\n union,\n optional,\n literal,\n maybe,\n maybeNull,\n refinement,\n string,\n boolean,\n number,\n integer,\n float,\n finite,\n Date: DatePrimitive,\n map,\n array,\n frozen,\n identifier,\n identifierNumber,\n late,\n lazy,\n undefined: undefinedType,\n null: nullType,\n snapshotProcessor,\n resilient\n}\n"]}
@@ -1,125 +0,0 @@
1
- import { SimpleType } from "../core/type/type.ts";
2
- import { TypeFlags } from "../internal.ts";
3
- import type { AnyObjectNode, ISimpleType, IType, IValidationContext, IValidationResult } from "../internal.ts";
4
- /**
5
- * @internal
6
- * @hidden
7
- */
8
- export declare class CoreType<C, S, T> extends SimpleType<C, S, T> {
9
- readonly flags: TypeFlags;
10
- private readonly checker;
11
- private readonly initializer;
12
- constructor(name: string, flags: TypeFlags, checker: (value: C) => boolean, initializer?: (v: C) => T);
13
- describe(): string;
14
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: C): this["N"];
15
- createNewInstance(snapshot: C): T;
16
- isValidSnapshot(value: C, context: IValidationContext): IValidationResult;
17
- }
18
- /**
19
- * `types.string` - Creates a type that can only contain a string value.
20
- * This type is used for string values by default
21
- *
22
- * Example:
23
- * ```ts
24
- * const Person = types.model({
25
- * firstName: types.string,
26
- * lastName: "Doe"
27
- * })
28
- * ```
29
- */
30
- export declare const string: ISimpleType<string>;
31
- /**
32
- * `types.number` - Creates a type that can only contain a numeric value.
33
- * This type is used for numeric values by default
34
- *
35
- * Example:
36
- * ```ts
37
- * const Vector = types.model({
38
- * x: types.number,
39
- * y: 1.5
40
- * })
41
- * ```
42
- */
43
- export declare const number: ISimpleType<number>;
44
- /**
45
- * `types.integer` - Creates a type that can only contain an integer value.
46
- *
47
- * Example:
48
- * ```ts
49
- * const Size = types.model({
50
- * width: types.integer,
51
- * height: 10
52
- * })
53
- * ```
54
- */
55
- export declare const integer: ISimpleType<number>;
56
- /**
57
- * `types.float` - Creates a type that can only contain an float value.
58
- *
59
- * Example:
60
- * ```ts
61
- * const Size = types.model({
62
- * width: types.float,
63
- * height: 10
64
- * })
65
- * ```
66
- */
67
- export declare const float: ISimpleType<number>;
68
- /**
69
- * `types.finite` - Creates a type that can only contain an finite value.
70
- *
71
- * Example:
72
- * ```ts
73
- * const Size = types.model({
74
- * width: types.finite,
75
- * height: 10
76
- * })
77
- * ```
78
- */
79
- export declare const finite: ISimpleType<number>;
80
- /**
81
- * `types.boolean` - Creates a type that can only contain a boolean value.
82
- * This type is used for boolean values by default
83
- *
84
- * Example:
85
- * ```ts
86
- * const Thing = types.model({
87
- * isCool: types.boolean,
88
- * isAwesome: false
89
- * })
90
- * ```
91
- */
92
- export declare const boolean: ISimpleType<boolean>;
93
- /**
94
- * `types.null` - The type of the value `null`
95
- */
96
- export declare const nullType: ISimpleType<null>;
97
- /**
98
- * `types.undefined` - The type of the value `undefined`
99
- */
100
- export declare const undefinedType: ISimpleType<undefined>;
101
- /**
102
- * `types.Date` - Creates a type that can only contain a javascript Date value.
103
- *
104
- * Example:
105
- * ```ts
106
- * const LogLine = types.model({
107
- * timestamp: types.Date,
108
- * })
109
- *
110
- * LogLine.create({ timestamp: new Date() })
111
- * ```
112
- */
113
- export declare const DatePrimitive: IType<number | Date, number, Date>;
114
- /**
115
- * @internal
116
- * @hidden
117
- */
118
- export declare function getPrimitiveFactoryFromValue(value: any): ISimpleType<any>;
119
- /**
120
- * Returns if a given value represents a primitive type.
121
- *
122
- * @param type
123
- * @returns
124
- */
125
- export declare function isPrimitiveType<IT extends ISimpleType<string> | ISimpleType<number> | ISimpleType<boolean> | typeof DatePrimitive>(type: IT): type is IT;
@@ -1,177 +0,0 @@
1
- import { SimpleType } from "../core/type/type.js";
2
- import { TypeFlags, createScalarNode, fail, identity, isFinite, isFloat, isInteger, isPrimitive, isType, typeCheckFailure, typeCheckSuccess } from "../internal.js";
3
- // TODO: implement CoreType using types.custom ?
4
- /**
5
- * @internal
6
- * @hidden
7
- */
8
- export class CoreType extends SimpleType {
9
- flags;
10
- checker;
11
- initializer;
12
- constructor(name, flags, checker, initializer = identity) {
13
- super(name);
14
- this.flags = flags;
15
- this.checker = checker;
16
- this.initializer = initializer;
17
- this.flags = flags;
18
- }
19
- describe() {
20
- return this.name;
21
- }
22
- instantiate(parent, subpath, environment, initialValue) {
23
- return createScalarNode(this, parent, subpath, environment, initialValue);
24
- }
25
- createNewInstance(snapshot) {
26
- return this.initializer(snapshot);
27
- }
28
- isValidSnapshot(value, context) {
29
- if (isPrimitive(value) && this.checker(value)) {
30
- return typeCheckSuccess();
31
- }
32
- const typeName = this.name === "Date" ? "Date or a unix milliseconds timestamp" : this.name;
33
- return typeCheckFailure(context, value, `Value is not a ${typeName}`);
34
- }
35
- }
36
- /**
37
- * `types.string` - Creates a type that can only contain a string value.
38
- * This type is used for string values by default
39
- *
40
- * Example:
41
- * ```ts
42
- * const Person = types.model({
43
- * firstName: types.string,
44
- * lastName: "Doe"
45
- * })
46
- * ```
47
- */
48
- // tslint:disable-next-line:variable-name
49
- export const string = new CoreType("string", TypeFlags.String, v => typeof v === "string");
50
- /**
51
- * `types.number` - Creates a type that can only contain a numeric value.
52
- * This type is used for numeric values by default
53
- *
54
- * Example:
55
- * ```ts
56
- * const Vector = types.model({
57
- * x: types.number,
58
- * y: 1.5
59
- * })
60
- * ```
61
- */
62
- // tslint:disable-next-line:variable-name
63
- export const number = new CoreType("number", TypeFlags.Number, v => typeof v === "number");
64
- /**
65
- * `types.integer` - Creates a type that can only contain an integer value.
66
- *
67
- * Example:
68
- * ```ts
69
- * const Size = types.model({
70
- * width: types.integer,
71
- * height: 10
72
- * })
73
- * ```
74
- */
75
- // tslint:disable-next-line:variable-name
76
- export const integer = new CoreType("integer", TypeFlags.Integer, v => isInteger(v));
77
- /**
78
- * `types.float` - Creates a type that can only contain an float value.
79
- *
80
- * Example:
81
- * ```ts
82
- * const Size = types.model({
83
- * width: types.float,
84
- * height: 10
85
- * })
86
- * ```
87
- */
88
- // tslint:disable-next-line:variable-name
89
- export const float = new CoreType("float", TypeFlags.Float, v => isFloat(v));
90
- /**
91
- * `types.finite` - Creates a type that can only contain an finite value.
92
- *
93
- * Example:
94
- * ```ts
95
- * const Size = types.model({
96
- * width: types.finite,
97
- * height: 10
98
- * })
99
- * ```
100
- */
101
- // tslint:disable-next-line:variable-name
102
- export const finite = new CoreType("finite", TypeFlags.Finite, v => isFinite(v));
103
- /**
104
- * `types.boolean` - Creates a type that can only contain a boolean value.
105
- * This type is used for boolean values by default
106
- *
107
- * Example:
108
- * ```ts
109
- * const Thing = types.model({
110
- * isCool: types.boolean,
111
- * isAwesome: false
112
- * })
113
- * ```
114
- */
115
- // tslint:disable-next-line:variable-name
116
- export const boolean = new CoreType("boolean", TypeFlags.Boolean, v => typeof v === "boolean");
117
- /**
118
- * `types.null` - The type of the value `null`
119
- */
120
- export const nullType = new CoreType("null", TypeFlags.Null, v => v === null);
121
- /**
122
- * `types.undefined` - The type of the value `undefined`
123
- */
124
- export const undefinedType = new CoreType("undefined", TypeFlags.Undefined, v => v === undefined);
125
- const _DatePrimitive = new CoreType("Date", TypeFlags.Date, v => typeof v === "number" || v instanceof Date, v => (v instanceof Date ? v : new Date(v)));
126
- _DatePrimitive.getSnapshot = function (node) {
127
- return node.storedValue.getTime();
128
- };
129
- /**
130
- * `types.Date` - Creates a type that can only contain a javascript Date value.
131
- *
132
- * Example:
133
- * ```ts
134
- * const LogLine = types.model({
135
- * timestamp: types.Date,
136
- * })
137
- *
138
- * LogLine.create({ timestamp: new Date() })
139
- * ```
140
- */
141
- export const DatePrimitive = _DatePrimitive;
142
- /**
143
- * @internal
144
- * @hidden
145
- */
146
- export function getPrimitiveFactoryFromValue(value) {
147
- switch (typeof value) {
148
- case "string":
149
- return string;
150
- case "number":
151
- return number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
152
- case "boolean":
153
- return boolean;
154
- case "object":
155
- if (value instanceof Date) {
156
- return DatePrimitive;
157
- }
158
- }
159
- throw fail("Cannot determine primitive type from value " + value);
160
- }
161
- /**
162
- * Returns if a given value represents a primitive type.
163
- *
164
- * @param type
165
- * @returns
166
- */
167
- export function isPrimitiveType(type) {
168
- return (isType(type) &&
169
- (type.flags &
170
- (TypeFlags.String |
171
- TypeFlags.Number |
172
- TypeFlags.Integer |
173
- TypeFlags.Boolean |
174
- TypeFlags.Date)) >
175
- 0);
176
- }
177
- //# sourceMappingURL=primitives.js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AACnE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAM7B,IAAQ;IACR,OAAO,CACL,MAAM,CAAC,IAAI,CAAC;QACZ,CAAC,IAAI,CAAC,KAAK;YACT,CAAC,SAAS,CAAC,MAAM;gBACf,SAAS,CAAC,MAAM;gBAChB,SAAS,CAAC,OAAO;gBACjB,SAAS,CAAC,OAAO;gBACjB,SAAS,CAAC,IAAI,CAAC,CAAC;YAClB,CAAC,CACJ,CAAA;AACH,CAAC","sourcesContent":["import { SimpleType } from \"../core/type/type.ts\"\nimport {\n TypeFlags,\n createScalarNode,\n fail,\n identity,\n isFinite,\n isFloat,\n isInteger,\n isPrimitive,\n isType,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../internal.ts\"\n\nimport type {\n AnyNode,\n AnyObjectNode,\n ISimpleType,\n IType,\n IValidationContext,\n IValidationResult\n} from \"../internal.ts\"\n\n// TODO: implement CoreType using types.custom ?\n/**\n * @internal\n * @hidden\n */\nexport class CoreType<C, S, T> extends SimpleType<C, S, T> {\n constructor(\n name: string,\n readonly flags: TypeFlags,\n private readonly checker: (value: C) => boolean,\n private readonly initializer: (v: C) => T = identity\n ) {\n super(name)\n this.flags = flags\n }\n\n describe() {\n return this.name\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: C\n ): this[\"N\"] {\n return createScalarNode(this, parent, subpath, environment, initialValue)\n }\n\n createNewInstance(snapshot: C) {\n return this.initializer(snapshot)\n }\n\n isValidSnapshot(value: C, context: IValidationContext): IValidationResult {\n if (isPrimitive(value) && this.checker(value as any)) {\n return typeCheckSuccess()\n }\n const typeName =\n this.name === \"Date\" ? \"Date or a unix milliseconds timestamp\" : this.name\n return typeCheckFailure(context, value, `Value is not a ${typeName}`)\n }\n}\n\n/**\n * `types.string` - Creates a type that can only contain a string value.\n * This type is used for string values by default\n *\n * Example:\n * ```ts\n * const Person = types.model({\n * firstName: types.string,\n * lastName: \"Doe\"\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const string: ISimpleType<string> = new CoreType<string, string, string>(\n \"string\",\n TypeFlags.String,\n v => typeof v === \"string\"\n)\n\n/**\n * `types.number` - Creates a type that can only contain a numeric value.\n * This type is used for numeric values by default\n *\n * Example:\n * ```ts\n * const Vector = types.model({\n * x: types.number,\n * y: 1.5\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const number: ISimpleType<number> = new CoreType<number, number, number>(\n \"number\",\n TypeFlags.Number,\n v => typeof v === \"number\"\n)\n\n/**\n * `types.integer` - Creates a type that can only contain an integer value.\n *\n * Example:\n * ```ts\n * const Size = types.model({\n * width: types.integer,\n * height: 10\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const integer: ISimpleType<number> = new CoreType<\n number,\n number,\n number\n>(\"integer\", TypeFlags.Integer, v => isInteger(v))\n\n/**\n * `types.float` - Creates a type that can only contain an float value.\n *\n * Example:\n * ```ts\n * const Size = types.model({\n * width: types.float,\n * height: 10\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const float: ISimpleType<number> = new CoreType<number, number, number>(\n \"float\",\n TypeFlags.Float,\n v => isFloat(v)\n)\n\n/**\n * `types.finite` - Creates a type that can only contain an finite value.\n *\n * Example:\n * ```ts\n * const Size = types.model({\n * width: types.finite,\n * height: 10\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const finite: ISimpleType<number> = new CoreType<number, number, number>(\n \"finite\",\n TypeFlags.Finite,\n v => isFinite(v)\n)\n\n/**\n * `types.boolean` - Creates a type that can only contain a boolean value.\n * This type is used for boolean values by default\n *\n * Example:\n * ```ts\n * const Thing = types.model({\n * isCool: types.boolean,\n * isAwesome: false\n * })\n * ```\n */\n// tslint:disable-next-line:variable-name\nexport const boolean: ISimpleType<boolean> = new CoreType<\n boolean,\n boolean,\n boolean\n>(\"boolean\", TypeFlags.Boolean, v => typeof v === \"boolean\")\n\n/**\n * `types.null` - The type of the value `null`\n */\nexport const nullType: ISimpleType<null> = new CoreType<null, null, null>(\n \"null\",\n TypeFlags.Null,\n v => v === null\n)\n\n/**\n * `types.undefined` - The type of the value `undefined`\n */\nexport const undefinedType: ISimpleType<undefined> = new CoreType<\n undefined,\n undefined,\n undefined\n>(\"undefined\", TypeFlags.Undefined, v => v === undefined)\n\nconst _DatePrimitive = new CoreType<number | Date, number, Date>(\n \"Date\",\n TypeFlags.Date,\n v => typeof v === \"number\" || v instanceof Date,\n v => (v instanceof Date ? v : new Date(v))\n)\n_DatePrimitive.getSnapshot = function (node: AnyNode) {\n return node.storedValue.getTime()\n}\n\n/**\n * `types.Date` - Creates a type that can only contain a javascript Date value.\n *\n * Example:\n * ```ts\n * const LogLine = types.model({\n * timestamp: types.Date,\n * })\n *\n * LogLine.create({ timestamp: new Date() })\n * ```\n */\nexport const DatePrimitive: IType<number | Date, number, Date> = _DatePrimitive\n\n/**\n * @internal\n * @hidden\n */\nexport function getPrimitiveFactoryFromValue(value: any): ISimpleType<any> {\n switch (typeof value) {\n case \"string\":\n return string\n case \"number\":\n return number // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now\n case \"boolean\":\n return boolean\n case \"object\":\n if (value instanceof Date) {\n return DatePrimitive\n }\n }\n throw fail(\"Cannot determine primitive type from value \" + value)\n}\n\n/**\n * Returns if a given value represents a primitive type.\n *\n * @param type\n * @returns\n */\nexport function isPrimitiveType<\n IT extends\n | ISimpleType<string>\n | ISimpleType<number>\n | ISimpleType<boolean>\n | typeof DatePrimitive\n>(type: IT): type is IT {\n return (\n isType(type) &&\n (type.flags &\n (TypeFlags.String |\n TypeFlags.Number |\n TypeFlags.Integer |\n TypeFlags.Boolean |\n TypeFlags.Date)) >\n 0\n )\n}\n"]}
@@ -1,75 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type IType, type IValidationContext, type IValidationResult, TypeFlags } from "../../internal.ts";
3
- export interface CustomTypeOptions<S, T> {
4
- /** Friendly name */
5
- name: string;
6
- /** given a serialized value and environment, how to turn it into the target type */
7
- fromSnapshot(snapshot: S, env?: any): T;
8
- /** return the serialization of the current value */
9
- toSnapshot(value: T): S;
10
- /** if true, this is a converted value, if false, it's a snapshot */
11
- isTargetType(value: T | S): boolean;
12
- /** a non empty string is assumed to be a validation error */
13
- getValidationMessage(snapshot: S): string;
14
- }
15
- /**
16
- * `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.
17
- *
18
- * The signature of the options is:
19
- * ```ts
20
- * export interface CustomTypeOptions<S, T> {
21
- * // Friendly name
22
- * name: string
23
- * // given a serialized value and environment, how to turn it into the target type
24
- * fromSnapshot(snapshot: S, env: any): T
25
- * // return the serialization of the current value
26
- * toSnapshot(value: T): S
27
- * // if true, this is a converted value, if false, it's a snapshot
28
- * isTargetType(value: T | S): value is T
29
- * // a non empty string is assumed to be a validation error
30
- * getValidationMessage?(snapshot: S): string
31
- * }
32
- * ```
33
- *
34
- * Example:
35
- * ```ts
36
- * const DecimalPrimitive = types.custom<string, Decimal>({
37
- * name: "Decimal",
38
- * fromSnapshot(value: string) {
39
- * return new Decimal(value)
40
- * },
41
- * toSnapshot(value: Decimal) {
42
- * return value.toString()
43
- * },
44
- * isTargetType(value: string | Decimal): boolean {
45
- * return value instanceof Decimal
46
- * },
47
- * getValidationMessage(value: string): string {
48
- * if (/^-?\d+\.\d+$/.test(value)) return "" // OK
49
- * return `'${value}' doesn't look like a valid decimal number`
50
- * }
51
- * })
52
- *
53
- * const Wallet = types.model({
54
- * balance: DecimalPrimitive
55
- * })
56
- * ```
57
- *
58
- * @param options
59
- * @returns
60
- */
61
- export declare function custom<S, T>(options: CustomTypeOptions<S, T>): IType<S | T, S, T>;
62
- /**
63
- * @internal
64
- * @hidden
65
- */
66
- export declare class CustomType<S, T> extends SimpleType<S | T, S, T> {
67
- protected readonly options: CustomTypeOptions<S, T>;
68
- readonly flags = TypeFlags.Custom;
69
- constructor(options: CustomTypeOptions<S, T>);
70
- describe(): string;
71
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
72
- getSnapshot(node: this["N"]): S;
73
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: S | T): this["N"];
74
- reconcile(current: this["N"], value: S | T, parent: AnyObjectNode, subpath: string): this["N"];
75
- }
@@ -1,106 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.js";
2
- import { TypeFlags, createScalarNode, typeCheckFailure, typeCheckSuccess } from "../../internal.js";
3
- /**
4
- * `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.
5
- *
6
- * The signature of the options is:
7
- * ```ts
8
- * export interface CustomTypeOptions<S, T> {
9
- * // Friendly name
10
- * name: string
11
- * // given a serialized value and environment, how to turn it into the target type
12
- * fromSnapshot(snapshot: S, env: any): T
13
- * // return the serialization of the current value
14
- * toSnapshot(value: T): S
15
- * // if true, this is a converted value, if false, it's a snapshot
16
- * isTargetType(value: T | S): value is T
17
- * // a non empty string is assumed to be a validation error
18
- * getValidationMessage?(snapshot: S): string
19
- * }
20
- * ```
21
- *
22
- * Example:
23
- * ```ts
24
- * const DecimalPrimitive = types.custom<string, Decimal>({
25
- * name: "Decimal",
26
- * fromSnapshot(value: string) {
27
- * return new Decimal(value)
28
- * },
29
- * toSnapshot(value: Decimal) {
30
- * return value.toString()
31
- * },
32
- * isTargetType(value: string | Decimal): boolean {
33
- * return value instanceof Decimal
34
- * },
35
- * getValidationMessage(value: string): string {
36
- * if (/^-?\d+\.\d+$/.test(value)) return "" // OK
37
- * return `'${value}' doesn't look like a valid decimal number`
38
- * }
39
- * })
40
- *
41
- * const Wallet = types.model({
42
- * balance: DecimalPrimitive
43
- * })
44
- * ```
45
- *
46
- * @param options
47
- * @returns
48
- */
49
- export function custom(options) {
50
- return new CustomType(options);
51
- }
52
- /**
53
- * @internal
54
- * @hidden
55
- */
56
- export class CustomType extends SimpleType {
57
- options;
58
- flags = TypeFlags.Custom;
59
- constructor(options) {
60
- super(options.name);
61
- this.options = options;
62
- }
63
- describe() {
64
- return this.name;
65
- }
66
- isValidSnapshot(value, context) {
67
- if (this.options.isTargetType(value)) {
68
- return typeCheckSuccess();
69
- }
70
- const typeError = this.options.getValidationMessage(value);
71
- if (typeError) {
72
- return typeCheckFailure(context, value, `Invalid value for type '${this.name}': ${typeError}`);
73
- }
74
- return typeCheckSuccess();
75
- }
76
- getSnapshot(node) {
77
- return this.options.toSnapshot(node.storedValue);
78
- }
79
- instantiate(parent, subpath, environment, initialValue) {
80
- const valueToStore = this.options.isTargetType(initialValue)
81
- ? initialValue
82
- : this.options.fromSnapshot(initialValue, parent && parent.root.environment);
83
- return createScalarNode(this, parent, subpath, environment, valueToStore);
84
- }
85
- reconcile(current, value, parent, subpath) {
86
- const isSnapshot = !this.options.isTargetType(value);
87
- // in theory customs use scalar nodes which cannot be detached, but still...
88
- if (!current.isDetaching) {
89
- const unchanged = current.type === this &&
90
- (isSnapshot
91
- ? value === current.snapshot
92
- : value === current.storedValue);
93
- if (unchanged) {
94
- current.setParent(parent, subpath);
95
- return current;
96
- }
97
- }
98
- const valueToStore = isSnapshot
99
- ? this.options.fromSnapshot(value, parent.root.environment)
100
- : value;
101
- const newNode = this.instantiate(parent, subpath, undefined, valueToStore);
102
- current.die(); // noop if detaching
103
- return newNode;
104
- }
105
- }
106
- //# sourceMappingURL=custom.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"custom.js","sourceRoot":"","sources":["../../../src/types/utility-types/custom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAKL,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,mBAAmB,CAAA;AAiB1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,UAAU,MAAM,CACpB,OAAgC;IAEhC,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C,CAAA;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","sourcesContent":["import { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type IType,\n type IValidationContext,\n type IValidationResult,\n TypeFlags,\n createScalarNode,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\nexport interface CustomTypeOptions<S, T> {\n /** Friendly name */\n name: string\n /** given a serialized value and environment, how to turn it into the target type */\n fromSnapshot(snapshot: S, env?: any): T\n /** return the serialization of the current value */\n toSnapshot(value: T): S\n /** if true, this is a converted value, if false, it's a snapshot */\n isTargetType(value: T | S): boolean\n /** a non empty string is assumed to be a validation error */\n getValidationMessage(snapshot: S): string\n // TODO: isSnapshotEqual\n // TODO: isValueEqual\n}\n\n/**\n * `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.\n *\n * The signature of the options is:\n * ```ts\n * export interface CustomTypeOptions<S, T> {\n * // Friendly name\n * name: string\n * // given a serialized value and environment, how to turn it into the target type\n * fromSnapshot(snapshot: S, env: any): T\n * // return the serialization of the current value\n * toSnapshot(value: T): S\n * // if true, this is a converted value, if false, it's a snapshot\n * isTargetType(value: T | S): value is T\n * // a non empty string is assumed to be a validation error\n * getValidationMessage?(snapshot: S): string\n * }\n * ```\n *\n * Example:\n * ```ts\n * const DecimalPrimitive = types.custom<string, Decimal>({\n * name: \"Decimal\",\n * fromSnapshot(value: string) {\n * return new Decimal(value)\n * },\n * toSnapshot(value: Decimal) {\n * return value.toString()\n * },\n * isTargetType(value: string | Decimal): boolean {\n * return value instanceof Decimal\n * },\n * getValidationMessage(value: string): string {\n * if (/^-?\\d+\\.\\d+$/.test(value)) return \"\" // OK\n * return `'${value}' doesn't look like a valid decimal number`\n * }\n * })\n *\n * const Wallet = types.model({\n * balance: DecimalPrimitive\n * })\n * ```\n *\n * @param options\n * @returns\n */\nexport function custom<S, T>(\n options: CustomTypeOptions<S, T>\n): IType<S | T, S, T> {\n return new CustomType<S, T>(options)\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class CustomType<S, T> extends SimpleType<S | T, S, T> {\n readonly flags = TypeFlags.Custom\n\n constructor(protected readonly options: CustomTypeOptions<S, T>) {\n super(options.name)\n }\n\n describe() {\n return this.name\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (this.options.isTargetType(value)) {\n return typeCheckSuccess()\n }\n\n const typeError: string = this.options.getValidationMessage(value as S)\n if (typeError) {\n return typeCheckFailure(\n context,\n value,\n `Invalid value for type '${this.name}': ${typeError}`\n )\n }\n return typeCheckSuccess()\n }\n\n getSnapshot(node: this[\"N\"]): S {\n return this.options.toSnapshot(node.storedValue)\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: S | T\n ): this[\"N\"] {\n const valueToStore: T = this.options.isTargetType(initialValue)\n ? (initialValue as T)\n : this.options.fromSnapshot(\n initialValue as S,\n parent && parent.root.environment\n )\n return createScalarNode(this, parent, subpath, environment, valueToStore)\n }\n\n reconcile(\n current: this[\"N\"],\n value: S | T,\n parent: AnyObjectNode,\n subpath: string\n ): this[\"N\"] {\n const isSnapshot = !this.options.isTargetType(value)\n // in theory customs use scalar nodes which cannot be detached, but still...\n if (!current.isDetaching) {\n const unchanged =\n current.type === this &&\n (isSnapshot\n ? value === current.snapshot\n : value === current.storedValue)\n if (unchanged) {\n current.setParent(parent, subpath)\n return current\n }\n }\n const valueToStore: T = isSnapshot\n ? this.options.fromSnapshot(value as S, parent.root.environment)\n : (value as T)\n const newNode = this.instantiate(parent, subpath, undefined, valueToStore)\n current.die() // noop if detaching\n return newNode\n }\n}\n"]}
@@ -1,5 +0,0 @@
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[]>>;
@@ -1,31 +0,0 @@
1
- import { assertIsString, devMode, literal, union } from "../../internal.js";
2
- /**
3
- * `types.enumeration` - Can be used to create an string based enumeration.
4
- * (note: this methods is just sugar for a union of string literals)
5
- *
6
- * Example:
7
- * ```ts
8
- * const TrafficLight = types.model({
9
- * color: types.enumeration("Color", ["Red", "Orange", "Green"])
10
- * })
11
- * ```
12
- *
13
- * @param name descriptive name of the enumeration (optional)
14
- * @param options possible values this enumeration can have
15
- * @returns
16
- */
17
- export function enumeration(name, options) {
18
- const realOptions = typeof name === "string" ? options : name;
19
- // check all options
20
- if (devMode()) {
21
- realOptions.forEach((option, i) => {
22
- assertIsString(option, i + 1);
23
- });
24
- }
25
- const type = union(...realOptions.map(option => literal("" + option)));
26
- if (typeof name === "string") {
27
- type.name = name;
28
- }
29
- return type;
30
- }
31
- //# sourceMappingURL=enumeration.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"enumeration.js","sourceRoot":"","sources":["../../../src/types/utility-types/enumeration.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,EACd,OAAO,EACP,OAAO,EACP,KAAK,EACN,MAAM,mBAAmB,CAAA;AAkB1B;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,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,OAAO,EAAE,EAAE,CAAC;QACd,WAAW,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAChC,cAAc,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;QAC/B,CAAC,CAAC,CAAA;IACJ,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,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","sourcesContent":["import {\n type ISimpleType,\n assertIsString,\n devMode,\n literal,\n union\n} from \"../../internal.ts\"\n\n/** @hidden */\nexport type UnionStringArray<T extends readonly string[]> = T[number]\n\n// strongly typed enumeration forms for plain and readonly string arrays (when passed directly to the function).\n// with these overloads, we get correct typing for native TS string enums when we use Object.values(Enum) as Enum[] as options.\n// these overloads also allow both mutable and immutable arrays, making types.enumeration<Enum>(Object.values(Enum)) possible.\n// the only case where this doesn't work is when passing to the function an array variable with a mutable type constraint;\n// for these cases, it will just fallback and assume the type is a generic string.\nexport function enumeration<T extends readonly string[]>(\n options: T\n): ISimpleType<UnionStringArray<T>>\nexport function enumeration<T extends string>(\n name: string,\n options: T[]\n): ISimpleType<UnionStringArray<T[]>>\n\n/**\n * `types.enumeration` - Can be used to create an string based enumeration.\n * (note: this methods is just sugar for a union of string literals)\n *\n * Example:\n * ```ts\n * const TrafficLight = types.model({\n * color: types.enumeration(\"Color\", [\"Red\", \"Orange\", \"Green\"])\n * })\n * ```\n *\n * @param name descriptive name of the enumeration (optional)\n * @param options possible values this enumeration can have\n * @returns\n */\nexport function enumeration(\n name: string | string[],\n options?: any\n): ISimpleType<string> {\n const realOptions: string[] = typeof name === \"string\" ? options! : name\n // check all options\n if (devMode()) {\n realOptions.forEach((option, i) => {\n assertIsString(option, i + 1)\n })\n }\n const type = union(...realOptions.map(option => literal(\"\" + option)))\n if (typeof name === \"string\") {\n type.name = name\n }\n return type\n}\n"]}
@@ -1,24 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type IAnyType, type IType, type IValidationContext, type IValidationResult, TypeFlags } from "../../internal.ts";
3
- /**
4
- * @internal
5
- * @hidden
6
- */
7
- export declare class Frozen<T> extends SimpleType<T, T, T> {
8
- private subType?;
9
- flags: TypeFlags;
10
- constructor(subType?: IAnyType | undefined);
11
- describe(): string;
12
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, value: this["C"]): this["N"];
13
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
14
- }
15
- export declare function frozen<C>(subType: IType<C, any, any>): IType<C, C, C>;
16
- export declare function frozen<T>(defaultValue: T): IType<T | undefined | null, T, T>;
17
- export declare function frozen<T = any>(): IType<T, T, T>;
18
- /**
19
- * Returns if a given value represents a frozen type.
20
- *
21
- * @param type
22
- * @returns
23
- */
24
- export declare function isFrozenType<IT extends IType<T | any, T, T>, T = any>(type: IT): type is IT;