@jbrowse/mobx-state-tree 5.7.1 → 5.8.7

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,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.types = void 0;
4
- // we import the types to re-export them inside types.
5
- const internal_ts_1 = require("../internal.js");
6
- exports.types = {
7
- enumeration: internal_ts_1.enumeration,
8
- model: internal_ts_1.model,
9
- compose: internal_ts_1.compose,
10
- custom: internal_ts_1.custom,
11
- reference: internal_ts_1.reference,
12
- safeReference: internal_ts_1.safeReference,
13
- union: internal_ts_1.union,
14
- optional: internal_ts_1.optional,
15
- literal: internal_ts_1.literal,
16
- maybe: internal_ts_1.maybe,
17
- maybeNull: internal_ts_1.maybeNull,
18
- refinement: internal_ts_1.refinement,
19
- string: internal_ts_1.string,
20
- boolean: internal_ts_1.boolean,
21
- number: internal_ts_1.number,
22
- integer: internal_ts_1.integer,
23
- float: internal_ts_1.float,
24
- finite: internal_ts_1.finite,
25
- Date: internal_ts_1.DatePrimitive,
26
- map: internal_ts_1.map,
27
- array: internal_ts_1.array,
28
- frozen: internal_ts_1.frozen,
29
- identifier: internal_ts_1.identifier,
30
- identifierNumber: internal_ts_1.identifierNumber,
31
- late: internal_ts_1.late,
32
- lazy: internal_ts_1.lazy,
33
- undefined: internal_ts_1.undefinedType,
34
- null: internal_ts_1.nullType,
35
- snapshotProcessor: internal_ts_1.snapshotProcessor,
36
- resilient: internal_ts_1.resilient
37
- };
38
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;AAAA,sDAAsD;AACtD,gDA+BuB;AAEV,QAAA,KAAK,GAAG;IACnB,WAAW,EAAX,yBAAW;IACX,KAAK,EAAL,mBAAK;IACL,OAAO,EAAP,qBAAO;IACP,MAAM,EAAN,oBAAM;IACN,SAAS,EAAT,uBAAS;IACT,aAAa,EAAb,2BAAa;IACb,KAAK,EAAL,mBAAK;IACL,QAAQ,EAAR,sBAAQ;IACR,OAAO,EAAP,qBAAO;IACP,KAAK,EAAL,mBAAK;IACL,SAAS,EAAT,uBAAS;IACT,UAAU,EAAV,wBAAU;IACV,MAAM,EAAN,oBAAM;IACN,OAAO,EAAP,qBAAO;IACP,MAAM,EAAN,oBAAM;IACN,OAAO,EAAP,qBAAO;IACP,KAAK,EAAL,mBAAK;IACL,MAAM,EAAN,oBAAM;IACN,IAAI,EAAE,2BAAa;IACnB,GAAG,EAAH,iBAAG;IACH,KAAK,EAAL,mBAAK;IACL,MAAM,EAAN,oBAAM;IACN,UAAU,EAAV,wBAAU;IACV,gBAAgB,EAAhB,8BAAgB;IAChB,IAAI,EAAJ,kBAAI;IACJ,IAAI,EAAJ,kBAAI;IACJ,SAAS,EAAE,2BAAa;IACxB,IAAI,EAAE,sBAAQ;IACd,iBAAiB,EAAjB,+BAAiB;IACjB,SAAS,EAAT,uBAAS;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,183 +0,0 @@
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 type_ts_1 = require("../core/type/type.js");
7
- const internal_ts_1 = require("../internal.js");
8
- // TODO: implement CoreType using types.custom ?
9
- /**
10
- * @internal
11
- * @hidden
12
- */
13
- class CoreType extends type_ts_1.SimpleType {
14
- flags;
15
- checker;
16
- initializer;
17
- constructor(name, flags, checker, initializer = internal_ts_1.identity) {
18
- super(name);
19
- this.flags = flags;
20
- this.checker = checker;
21
- this.initializer = initializer;
22
- this.flags = flags;
23
- }
24
- describe() {
25
- return this.name;
26
- }
27
- instantiate(parent, subpath, environment, initialValue) {
28
- return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, initialValue);
29
- }
30
- createNewInstance(snapshot) {
31
- return this.initializer(snapshot);
32
- }
33
- isValidSnapshot(value, context) {
34
- if ((0, internal_ts_1.isPrimitive)(value) && this.checker(value)) {
35
- return (0, internal_ts_1.typeCheckSuccess)();
36
- }
37
- const typeName = this.name === "Date" ? "Date or a unix milliseconds timestamp" : this.name;
38
- return (0, internal_ts_1.typeCheckFailure)(context, value, `Value is not a ${typeName}`);
39
- }
40
- }
41
- exports.CoreType = CoreType;
42
- /**
43
- * `types.string` - Creates a type that can only contain a string value.
44
- * This type is used for string values by default
45
- *
46
- * Example:
47
- * ```ts
48
- * const Person = types.model({
49
- * firstName: types.string,
50
- * lastName: "Doe"
51
- * })
52
- * ```
53
- */
54
- // tslint:disable-next-line:variable-name
55
- exports.string = new CoreType("string", internal_ts_1.TypeFlags.String, v => typeof v === "string");
56
- /**
57
- * `types.number` - Creates a type that can only contain a numeric value.
58
- * This type is used for numeric values by default
59
- *
60
- * Example:
61
- * ```ts
62
- * const Vector = types.model({
63
- * x: types.number,
64
- * y: 1.5
65
- * })
66
- * ```
67
- */
68
- // tslint:disable-next-line:variable-name
69
- exports.number = new CoreType("number", internal_ts_1.TypeFlags.Number, v => typeof v === "number");
70
- /**
71
- * `types.integer` - Creates a type that can only contain an integer value.
72
- *
73
- * Example:
74
- * ```ts
75
- * const Size = types.model({
76
- * width: types.integer,
77
- * height: 10
78
- * })
79
- * ```
80
- */
81
- // tslint:disable-next-line:variable-name
82
- exports.integer = new CoreType("integer", internal_ts_1.TypeFlags.Integer, v => (0, internal_ts_1.isInteger)(v));
83
- /**
84
- * `types.float` - Creates a type that can only contain an float value.
85
- *
86
- * Example:
87
- * ```ts
88
- * const Size = types.model({
89
- * width: types.float,
90
- * height: 10
91
- * })
92
- * ```
93
- */
94
- // tslint:disable-next-line:variable-name
95
- exports.float = new CoreType("float", internal_ts_1.TypeFlags.Float, v => (0, internal_ts_1.isFloat)(v));
96
- /**
97
- * `types.finite` - Creates a type that can only contain an finite value.
98
- *
99
- * Example:
100
- * ```ts
101
- * const Size = types.model({
102
- * width: types.finite,
103
- * height: 10
104
- * })
105
- * ```
106
- */
107
- // tslint:disable-next-line:variable-name
108
- exports.finite = new CoreType("finite", internal_ts_1.TypeFlags.Finite, v => (0, internal_ts_1.isFinite)(v));
109
- /**
110
- * `types.boolean` - Creates a type that can only contain a boolean value.
111
- * This type is used for boolean values by default
112
- *
113
- * Example:
114
- * ```ts
115
- * const Thing = types.model({
116
- * isCool: types.boolean,
117
- * isAwesome: false
118
- * })
119
- * ```
120
- */
121
- // tslint:disable-next-line:variable-name
122
- exports.boolean = new CoreType("boolean", internal_ts_1.TypeFlags.Boolean, v => typeof v === "boolean");
123
- /**
124
- * `types.null` - The type of the value `null`
125
- */
126
- exports.nullType = new CoreType("null", internal_ts_1.TypeFlags.Null, v => v === null);
127
- /**
128
- * `types.undefined` - The type of the value `undefined`
129
- */
130
- exports.undefinedType = new CoreType("undefined", internal_ts_1.TypeFlags.Undefined, v => v === undefined);
131
- 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)));
132
- _DatePrimitive.getSnapshot = function (node) {
133
- return node.storedValue.getTime();
134
- };
135
- /**
136
- * `types.Date` - Creates a type that can only contain a javascript Date value.
137
- *
138
- * Example:
139
- * ```ts
140
- * const LogLine = types.model({
141
- * timestamp: types.Date,
142
- * })
143
- *
144
- * LogLine.create({ timestamp: new Date() })
145
- * ```
146
- */
147
- exports.DatePrimitive = _DatePrimitive;
148
- /**
149
- * @internal
150
- * @hidden
151
- */
152
- function getPrimitiveFactoryFromValue(value) {
153
- switch (typeof value) {
154
- case "string":
155
- return exports.string;
156
- case "number":
157
- return exports.number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
158
- case "boolean":
159
- return exports.boolean;
160
- case "object":
161
- if (value instanceof Date) {
162
- return exports.DatePrimitive;
163
- }
164
- }
165
- throw (0, internal_ts_1.fail)("Cannot determine primitive type from value " + value);
166
- }
167
- /**
168
- * Returns if a given value represents a primitive type.
169
- *
170
- * @param type
171
- * @returns
172
- */
173
- function isPrimitiveType(type) {
174
- return ((0, internal_ts_1.isType)(type) &&
175
- (type.flags &
176
- (internal_ts_1.TypeFlags.String |
177
- internal_ts_1.TypeFlags.Number |
178
- internal_ts_1.TypeFlags.Integer |
179
- internal_ts_1.TypeFlags.Boolean |
180
- internal_ts_1.TypeFlags.Date)) >
181
- 0);
182
- }
183
- //# sourceMappingURL=primitives.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":";;;AAgOA,oEAcC;AAQD,0CAiBC;AAvQD,kDAAiD;AACjD,gDAYuB;AAWvB,gDAAgD;AAChD;;;GAGG;AACH,MAAa,QAAkB,SAAQ,oBAAmB;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,CA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AAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAC3C,CAAA;AACD,cAAc,CAAC,WAAW,GAAG,UAAU,IAAa;IAClD,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAA;AACnC,CAAC,CAAA;AAED;;;;;;;;;;;GAWG;AACU,QAAA,aAAa,GAAuC,cAAc,CAAA;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","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,111 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomType = void 0;
4
- exports.custom = custom;
5
- const type_ts_1 = require("../../core/type/type.js");
6
- const internal_ts_1 = require("../../internal.js");
7
- /**
8
- * `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.
9
- *
10
- * The signature of the options is:
11
- * ```ts
12
- * export interface CustomTypeOptions<S, T> {
13
- * // Friendly name
14
- * name: string
15
- * // given a serialized value and environment, how to turn it into the target type
16
- * fromSnapshot(snapshot: S, env: any): T
17
- * // return the serialization of the current value
18
- * toSnapshot(value: T): S
19
- * // if true, this is a converted value, if false, it's a snapshot
20
- * isTargetType(value: T | S): value is T
21
- * // a non empty string is assumed to be a validation error
22
- * getValidationMessage?(snapshot: S): string
23
- * }
24
- * ```
25
- *
26
- * Example:
27
- * ```ts
28
- * const DecimalPrimitive = types.custom<string, Decimal>({
29
- * name: "Decimal",
30
- * fromSnapshot(value: string) {
31
- * return new Decimal(value)
32
- * },
33
- * toSnapshot(value: Decimal) {
34
- * return value.toString()
35
- * },
36
- * isTargetType(value: string | Decimal): boolean {
37
- * return value instanceof Decimal
38
- * },
39
- * getValidationMessage(value: string): string {
40
- * if (/^-?\d+\.\d+$/.test(value)) return "" // OK
41
- * return `'${value}' doesn't look like a valid decimal number`
42
- * }
43
- * })
44
- *
45
- * const Wallet = types.model({
46
- * balance: DecimalPrimitive
47
- * })
48
- * ```
49
- *
50
- * @param options
51
- * @returns
52
- */
53
- function custom(options) {
54
- return new CustomType(options);
55
- }
56
- /**
57
- * @internal
58
- * @hidden
59
- */
60
- class CustomType extends type_ts_1.SimpleType {
61
- options;
62
- flags = internal_ts_1.TypeFlags.Custom;
63
- constructor(options) {
64
- super(options.name);
65
- this.options = options;
66
- }
67
- describe() {
68
- return this.name;
69
- }
70
- isValidSnapshot(value, context) {
71
- if (this.options.isTargetType(value)) {
72
- return (0, internal_ts_1.typeCheckSuccess)();
73
- }
74
- const typeError = this.options.getValidationMessage(value);
75
- if (typeError) {
76
- return (0, internal_ts_1.typeCheckFailure)(context, value, `Invalid value for type '${this.name}': ${typeError}`);
77
- }
78
- return (0, internal_ts_1.typeCheckSuccess)();
79
- }
80
- getSnapshot(node) {
81
- return this.options.toSnapshot(node.storedValue);
82
- }
83
- instantiate(parent, subpath, environment, initialValue) {
84
- const valueToStore = this.options.isTargetType(initialValue)
85
- ? initialValue
86
- : this.options.fromSnapshot(initialValue, parent && parent.root.environment);
87
- return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, valueToStore);
88
- }
89
- reconcile(current, value, parent, subpath) {
90
- const isSnapshot = !this.options.isTargetType(value);
91
- // in theory customs use scalar nodes which cannot be detached, but still...
92
- if (!current.isDetaching) {
93
- const unchanged = current.type === this &&
94
- (isSnapshot
95
- ? value === current.snapshot
96
- : value === current.storedValue);
97
- if (unchanged) {
98
- current.setParent(parent, subpath);
99
- return current;
100
- }
101
- }
102
- const valueToStore = isSnapshot
103
- ? this.options.fromSnapshot(value, parent.root.environment)
104
- : value;
105
- const newNode = this.instantiate(parent, subpath, undefined, valueToStore);
106
- current.die(); // noop if detaching
107
- return newNode;
108
- }
109
- }
110
- exports.CustomType = CustomType;
111
- //# sourceMappingURL=custom.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"custom.js","sourceRoot":"","sources":["../../../src/types/utility-types/custom.ts"],"names":[],"mappings":";;;AAyEA,wBAIC;AA7ED,qDAAoD;AACpD,mDAS0B;AAiB1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,SAAgB,MAAM,CACpB,OAAgC;IAEhC,OAAO,IAAI,UAAU,CAAO,OAAO,CAAC,CAAA;AACtC,CAAC;AAED;;;GAGG;AACH,MAAa,UAAiB,SAAQ,oBAAuB;IAG5B;IAFtB,KAAK,GAAG,uBAAS,CAAC,MAAM,CAAA;IAEjC,YAA+B,OAAgC;QAC7D,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QADU,YAAO,GAAP,OAAO,CAAyB;IAE/D,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;IAED,eAAe,CACb,KAAgB,EAChB,OAA2B;QAE3B,IAAI,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;YACrC,OAAO,IAAA,8BAAgB,GAAE,CAAA;QAC3B,CAAC;QAED,MAAM,SAAS,GAAW,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,KAAU,CAAC,C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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","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,34 +0,0 @@
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
@@ -1 +0,0 @@
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","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;