@strictly/define 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/.eslintrc.cjs +31 -0
  2. package/.out/index.d.ts +20 -0
  3. package/.out/index.js +20 -0
  4. package/.out/transformers/copies/copy.d.ts +4 -0
  5. package/.out/transformers/copies/copy.js +7 -0
  6. package/.out/transformers/copies/copy_to.d.ts +6 -0
  7. package/.out/transformers/copies/copy_to.js +91 -0
  8. package/.out/transformers/copies/mobx_copy.d.ts +5 -0
  9. package/.out/transformers/copies/mobx_copy.js +42 -0
  10. package/.out/transformers/copies/specs/copy_to.tests.d.ts +1 -0
  11. package/.out/transformers/copies/specs/copy_to.tests.js +97 -0
  12. package/.out/transformers/copies/specs/mobx_copy.tests.d.ts +1 -0
  13. package/.out/transformers/copies/specs/mobx_copy.tests.js +19 -0
  14. package/.out/transformers/flatteners/flatten_accessors_of.d.ts +5 -0
  15. package/.out/transformers/flatteners/flatten_accessors_of.js +11 -0
  16. package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +3 -0
  17. package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +10 -0
  18. package/.out/transformers/flatteners/flatten_type_def_to.d.ts +4 -0
  19. package/.out/transformers/flatteners/flatten_type_def_to.js +49 -0
  20. package/.out/transformers/flatteners/flatten_type_defs_of.d.ts +3 -0
  21. package/.out/transformers/flatteners/flatten_type_defs_of.js +7 -0
  22. package/.out/transformers/flatteners/flatten_value_type_to.d.ts +11 -0
  23. package/.out/transformers/flatteners/flatten_value_type_to.js +79 -0
  24. package/.out/transformers/flatteners/flatten_value_types_of.d.ts +3 -0
  25. package/.out/transformers/flatteners/flatten_value_types_of.js +7 -0
  26. package/.out/transformers/flatteners/json_path.d.ts +2 -0
  27. package/.out/transformers/flatteners/json_path.js +14 -0
  28. package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.d.ts +1 -0
  29. package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.js +90 -0
  30. package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.d.ts +1 -0
  31. package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +94 -0
  32. package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.d.ts +1 -0
  33. package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.js +110 -0
  34. package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.d.ts +1 -0
  35. package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +17 -0
  36. package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.d.ts +1 -0
  37. package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.js +297 -0
  38. package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.d.ts +1 -0
  39. package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.js +37 -0
  40. package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.d.ts +1 -0
  41. package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +167 -0
  42. package/.out/transformers/flatteners/value_path_to_type_path.d.ts +2 -0
  43. package/.out/transformers/flatteners/value_path_to_type_path.js +96 -0
  44. package/.out/tsconfig.json +15 -0
  45. package/.out/tsconfig.tsbuildinfo +1 -0
  46. package/.out/tsup.config.d.ts +3 -0
  47. package/.out/tsup.config.js +12 -0
  48. package/.out/types/builders.d.ts +62 -0
  49. package/.out/types/builders.js +148 -0
  50. package/.out/types/definitions.d.ts +41 -0
  51. package/.out/types/definitions.js +26 -0
  52. package/.out/types/flattened.d.ts +2 -0
  53. package/.out/types/flattened.js +1 -0
  54. package/.out/types/flattened_accessors_of.d.ts +9 -0
  55. package/.out/types/flattened_accessors_of.js +1 -0
  56. package/.out/types/flattened_type_defs_of.d.ts +21 -0
  57. package/.out/types/flattened_type_defs_of.js +1 -0
  58. package/.out/types/flattened_value_types_of.d.ts +6 -0
  59. package/.out/types/flattened_value_types_of.js +1 -0
  60. package/.out/types/json_path_of.d.ts +1 -0
  61. package/.out/types/json_path_of.js +1 -0
  62. package/.out/types/json_paths_of.d.ts +16 -0
  63. package/.out/types/json_paths_of.js +1 -0
  64. package/.out/types/mobx_value_type_of.d.ts +11 -0
  65. package/.out/types/mobx_value_type_of.js +2 -0
  66. package/.out/types/partial_type_def_of.d.ts +40 -0
  67. package/.out/types/partial_type_def_of.js +1 -0
  68. package/.out/types/readonly_type_def_of.d.ts +29 -0
  69. package/.out/types/readonly_type_def_of.js +1 -0
  70. package/.out/types/specs/builder.tests.d.ts +1 -0
  71. package/.out/types/specs/builder.tests.js +93 -0
  72. package/.out/types/specs/flattened_accessors_of.tests.d.ts +1 -0
  73. package/.out/types/specs/flattened_accessors_of.tests.js +11 -0
  74. package/.out/types/specs/flattened_type_defs_of.tests.d.ts +1 -0
  75. package/.out/types/specs/flattened_type_defs_of.tests.js +71 -0
  76. package/.out/types/specs/flattened_value_types_of.tests.d.ts +1 -0
  77. package/.out/types/specs/flattened_value_types_of.tests.js +11 -0
  78. package/.out/types/specs/json_paths_of.tests.d.ts +1 -0
  79. package/.out/types/specs/json_paths_of.tests.js +199 -0
  80. package/.out/types/specs/partial_type_def_of.tests.d.ts +1 -0
  81. package/.out/types/specs/partial_type_def_of.tests.js +50 -0
  82. package/.out/types/specs/readonly_type_def_of.tests.d.ts +1 -0
  83. package/.out/types/specs/readonly_type_def_of.tests.js +55 -0
  84. package/.out/types/specs/strict_definitions.tests.d.ts +1 -0
  85. package/.out/types/specs/strict_definitions.tests.js +42 -0
  86. package/.out/types/specs/value_to_type_paths_of.tests.d.ts +1 -0
  87. package/.out/types/specs/value_to_type_paths_of.tests.js +90 -0
  88. package/.out/types/specs/value_type_of.tests.d.ts +1 -0
  89. package/.out/types/specs/value_type_of.tests.js +91 -0
  90. package/.out/types/specs/value_types_of_discriminated_union.tests.d.ts +1 -0
  91. package/.out/types/specs/value_types_of_discriminated_union.tests.js +9 -0
  92. package/.out/types/strict_definitions.d.ts +44 -0
  93. package/.out/types/strict_definitions.js +1 -0
  94. package/.out/types/value_to_type_paths_of.d.ts +22 -0
  95. package/.out/types/value_to_type_paths_of.js +1 -0
  96. package/.out/types/value_type_of.d.ts +24 -0
  97. package/.out/types/value_type_of.js +1 -0
  98. package/.out/types/value_types_of_discriminated_union.d.ts +9 -0
  99. package/.out/types/value_types_of_discriminated_union.js +1 -0
  100. package/.out/vitest.workspace.d.ts +2 -0
  101. package/.out/vitest.workspace.js +7 -0
  102. package/.turbo/turbo-build.log +18 -0
  103. package/.turbo/turbo-check-types.log +3 -0
  104. package/.turbo/turbo-release$colon$exports.log +3 -0
  105. package/README.md +12 -0
  106. package/dist/index.cjs +798 -0
  107. package/dist/index.d.cts +301 -0
  108. package/dist/index.d.ts +301 -0
  109. package/dist/index.js +775 -0
  110. package/index.ts +20 -0
  111. package/package.exports.json +18 -0
  112. package/package.json +45 -0
  113. package/transformers/copies/copy.ts +18 -0
  114. package/transformers/copies/copy_to.ts +226 -0
  115. package/transformers/copies/mobx_copy.ts +81 -0
  116. package/transformers/copies/specs/copy_to.tests.ts +143 -0
  117. package/transformers/copies/specs/mobx_copy.tests.ts +26 -0
  118. package/transformers/flatteners/flatten_accessors_of.ts +43 -0
  119. package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +39 -0
  120. package/transformers/flatteners/flatten_type_def_to.ts +127 -0
  121. package/transformers/flatteners/flatten_type_defs_of.ts +16 -0
  122. package/transformers/flatteners/flatten_value_type_to.ts +227 -0
  123. package/transformers/flatteners/flatten_value_types_of.ts +23 -0
  124. package/transformers/flatteners/json_path.ts +15 -0
  125. package/transformers/flatteners/specs/flatten_accessors_of.tests.ts +113 -0
  126. package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +115 -0
  127. package/transformers/flatteners/specs/flatten_type_def_to.tests.ts +146 -0
  128. package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +26 -0
  129. package/transformers/flatteners/specs/flatten_value_type_to.tests.ts +452 -0
  130. package/transformers/flatteners/specs/flatten_value_types_of.tests.ts +46 -0
  131. package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +240 -0
  132. package/transformers/flatteners/value_path_to_type_path.ts +164 -0
  133. package/tsconfig.build.json +11 -0
  134. package/tsconfig.json +15 -0
  135. package/tsup.config.ts +16 -0
  136. package/types/builders.ts +284 -0
  137. package/types/definitions.ts +106 -0
  138. package/types/flattened.ts +5 -0
  139. package/types/flattened_accessors_of.ts +15 -0
  140. package/types/flattened_type_defs_of.ts +130 -0
  141. package/types/flattened_value_types_of.ts +11 -0
  142. package/types/json_path_of.ts +7 -0
  143. package/types/json_paths_of.ts +129 -0
  144. package/types/mobx_value_type_of.ts +16 -0
  145. package/types/partial_type_def_of.ts +64 -0
  146. package/types/readonly_type_def_of.ts +53 -0
  147. package/types/specs/builder.tests.ts +295 -0
  148. package/types/specs/flattened_accessors_of.tests.ts +27 -0
  149. package/types/specs/flattened_type_defs_of.tests.ts +212 -0
  150. package/types/specs/flattened_value_types_of.tests.ts +21 -0
  151. package/types/specs/json_paths_of.tests.ts +304 -0
  152. package/types/specs/partial_type_def_of.tests.ts +251 -0
  153. package/types/specs/readonly_type_def_of.tests.ts +158 -0
  154. package/types/specs/strict_definitions.tests.ts +184 -0
  155. package/types/specs/value_to_type_paths_of.tests.ts +181 -0
  156. package/types/specs/value_type_of.tests.ts +329 -0
  157. package/types/specs/value_types_of_discriminated_union.tests.ts +30 -0
  158. package/types/strict_definitions.ts +107 -0
  159. package/types/value_to_type_paths_of.ts +184 -0
  160. package/types/value_type_of.ts +84 -0
  161. package/types/value_types_of_discriminated_union.ts +14 -0
  162. package/vitest.workspace.ts +11 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,798 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ TypeDefType: () => TypeDefType,
24
+ booleanType: () => booleanType,
25
+ copy: () => copy,
26
+ flattenAccessorsOf: () => flattenAccessorsOf,
27
+ flattenJsonValueToTypePathsOf: () => flattenJsonValueToTypePathsOf,
28
+ flattenTypeDefsOf: () => flattenTypeDefsOf,
29
+ flattenValueTypeTo: () => flattenValueTypeTo,
30
+ getUnionTypeDef: () => getUnionTypeDef,
31
+ jsonPath: () => jsonPath,
32
+ jsonPathPop: () => jsonPathPop,
33
+ list: () => list,
34
+ literal: () => literal,
35
+ mobxCopy: () => mobxCopy,
36
+ nullType: () => nullType,
37
+ nullable: () => nullable,
38
+ numberType: () => numberType,
39
+ object: () => object,
40
+ record: () => record,
41
+ stringType: () => stringType,
42
+ union: () => union,
43
+ valuePathToTypePath: () => valuePathToTypePath
44
+ });
45
+ module.exports = __toCommonJS(index_exports);
46
+
47
+ // transformers/copies/copy_to.ts
48
+ var import_base = require("@strictly/base");
49
+
50
+ // types/definitions.ts
51
+ var TypeDefType = /* @__PURE__ */ ((TypeDefType2) => {
52
+ TypeDefType2[TypeDefType2["Literal"] = 1] = "Literal";
53
+ TypeDefType2[TypeDefType2["List"] = 2] = "List";
54
+ TypeDefType2[TypeDefType2["Record"] = 3] = "Record";
55
+ TypeDefType2[TypeDefType2["Object"] = 4] = "Object";
56
+ TypeDefType2[TypeDefType2["Union"] = 5] = "Union";
57
+ return TypeDefType2;
58
+ })(TypeDefType || {});
59
+
60
+ // transformers/copies/copy_to.ts
61
+ function copyTo({ definition }, value, copier) {
62
+ return internalCopyTo(
63
+ definition,
64
+ value,
65
+ copier
66
+ );
67
+ }
68
+ function internalCopyTo(definition, value, copier) {
69
+ switch (definition.type) {
70
+ case 1 /* Literal */:
71
+ return copyLiteral(
72
+ definition,
73
+ value,
74
+ copier
75
+ );
76
+ case 2 /* List */:
77
+ return copyList(
78
+ definition,
79
+ value,
80
+ copier
81
+ );
82
+ case 3 /* Record */:
83
+ return copyRecord(
84
+ definition,
85
+ value,
86
+ copier
87
+ );
88
+ case 4 /* Object */:
89
+ return copyObject(
90
+ definition,
91
+ value,
92
+ copier
93
+ );
94
+ case 5 /* Union */:
95
+ return copyUnion(
96
+ definition,
97
+ value,
98
+ copier
99
+ );
100
+ default:
101
+ throw new import_base.UnreachableError(definition);
102
+ }
103
+ }
104
+ function copyLiteral(typeDef, value, copier) {
105
+ return copier(value, typeDef);
106
+ }
107
+ function copyList(typeDef, arr, copier) {
108
+ const {
109
+ elements
110
+ } = typeDef;
111
+ const list2 = arr.map(function(value) {
112
+ return internalCopyTo(elements, value, copier);
113
+ });
114
+ return copier(
115
+ list2,
116
+ typeDef
117
+ );
118
+ }
119
+ function copyRecord(typeDef, value, copier) {
120
+ const {
121
+ valueTypeDef
122
+ } = typeDef;
123
+ const record2 = (0, import_base.map)(
124
+ value,
125
+ function(_key, value2) {
126
+ return internalCopyTo(valueTypeDef, value2, copier);
127
+ }
128
+ );
129
+ return copier(
130
+ record2,
131
+ typeDef
132
+ );
133
+ }
134
+ function copyObjectFields(fields, value, copier, extra) {
135
+ const record2 = (0, import_base.reduce)(fields, function(acc, key, field) {
136
+ const fieldValue = value[key];
137
+ acc[key] = fieldValue != null ? internalCopyTo(field, fieldValue, copier) : fieldValue;
138
+ return acc;
139
+ }, extra);
140
+ return record2;
141
+ }
142
+ function copyObject(typeDef, value, copier) {
143
+ const {
144
+ fields
145
+ } = typeDef;
146
+ const record2 = copyObjectFields(fields, value, copier, {});
147
+ return copier(record2, typeDef);
148
+ }
149
+ function copyUnion(typeDef, value, copier) {
150
+ const {
151
+ discriminator,
152
+ unions
153
+ } = typeDef;
154
+ if (discriminator != null) {
155
+ const discriminatorValue = value[discriminator];
156
+ const discriminatingUnion = {
157
+ ...internalCopyTo(
158
+ unions[discriminatorValue],
159
+ value,
160
+ copier
161
+ ),
162
+ [discriminator]: discriminatorValue
163
+ };
164
+ return copier(discriminatingUnion, typeDef);
165
+ }
166
+ const allTypeDefs = Object.values(unions);
167
+ const variableTypeDefs = allTypeDefs.filter(function(typeDef2) {
168
+ return typeDef2.type !== 1 /* Literal */ || typeDef2.valuePrototype == null;
169
+ });
170
+ if (variableTypeDefs.length <= 1) {
171
+ const targetTypeDef = allTypeDefs.find(function(typeDef2) {
172
+ return typeDef2.type === 1 /* Literal */ && typeDef2.valuePrototype != null && typeDef2.valuePrototype.indexOf(value) >= 0;
173
+ }) || variableTypeDefs[0];
174
+ return internalCopyTo(targetTypeDef, value, copier);
175
+ }
176
+ throw new import_base.UnexpectedImplementationError(
177
+ "unions must be strict in order to be copied"
178
+ );
179
+ }
180
+
181
+ // transformers/copies/copy.ts
182
+ function identity(v) {
183
+ return v;
184
+ }
185
+ function copy(t, proto) {
186
+ return copyTo(t, proto, identity);
187
+ }
188
+
189
+ // transformers/copies/mobx_copy.ts
190
+ var import_base3 = require("@strictly/base");
191
+ var import_mobx = require("mobx");
192
+
193
+ // transformers/flatteners/flatten_value_type_to.ts
194
+ var import_base2 = require("@strictly/base");
195
+
196
+ // transformers/flatteners/json_path.ts
197
+ function jsonPath(prefix, segment, qualifier = "") {
198
+ const s = `.${qualifier}${segment}`;
199
+ return `${prefix}${s}`;
200
+ }
201
+ function jsonPathPop(path) {
202
+ const parts = path.split(".");
203
+ if (parts.length <= 1) {
204
+ return null;
205
+ }
206
+ return [
207
+ parts.slice(0, -1).join("."),
208
+ parts.pop()
209
+ ];
210
+ }
211
+
212
+ // transformers/flatteners/flatten_value_type_to.ts
213
+ function flattenValueTypeTo({ definition }, v, setter, mapper) {
214
+ const r = {};
215
+ internalFlattenValue(
216
+ "$",
217
+ "$",
218
+ definition,
219
+ v,
220
+ setter,
221
+ mapper,
222
+ r
223
+ );
224
+ return r;
225
+ }
226
+ function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper, r) {
227
+ r[valuePath] = mapper(typeDef, v, setter, typePath, valuePath);
228
+ return internalFlattenValueChildren(valuePath, typePath, "", typeDef, v, mapper, r);
229
+ }
230
+ function internalFlattenValueChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r) {
231
+ switch (typeDef.type) {
232
+ case 1 /* Literal */:
233
+ return r;
234
+ case 2 /* List */:
235
+ return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r);
236
+ case 3 /* Record */:
237
+ return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r);
238
+ case 4 /* Object */:
239
+ return internalFlattenObjectChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r);
240
+ case 5 /* Union */:
241
+ return internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r);
242
+ default:
243
+ throw new import_base2.UnreachableError(typeDef);
244
+ }
245
+ }
246
+ function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper, r) {
247
+ const newTypePath = jsonPath(typePath, "*");
248
+ return v.reduce(function(r2, e, i) {
249
+ return internalFlattenValue(
250
+ jsonPath(valuePath, i),
251
+ newTypePath,
252
+ elements,
253
+ e,
254
+ (e2) => {
255
+ v[i] = e2;
256
+ },
257
+ mapper,
258
+ r2
259
+ );
260
+ }, r);
261
+ }
262
+ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper, r) {
263
+ const newTypePath = jsonPath(typePath, "*");
264
+ return (0, import_base2.reduce)(
265
+ v,
266
+ function(r2, k, value) {
267
+ return internalFlattenValue(
268
+ jsonPath(valuePath, k),
269
+ newTypePath,
270
+ valueTypeDef,
271
+ value,
272
+ (value2) => {
273
+ v[k] = value2;
274
+ },
275
+ mapper,
276
+ r2
277
+ );
278
+ },
279
+ r
280
+ );
281
+ }
282
+ function internalFlattenObjectChildren(valuePath, typePath, qualifier, { fields }, v, mapper, r) {
283
+ return (0, import_base2.reduce)(
284
+ fields,
285
+ function(r2, k, fieldTypeDef) {
286
+ const fieldValue = v[k];
287
+ return internalFlattenValue(
288
+ jsonPath(valuePath, k, qualifier),
289
+ jsonPath(typePath, k, qualifier),
290
+ fieldTypeDef,
291
+ fieldValue,
292
+ (value) => {
293
+ v[k] = value;
294
+ },
295
+ mapper,
296
+ r2
297
+ );
298
+ },
299
+ r
300
+ );
301
+ }
302
+ function internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r) {
303
+ const childTypeDef = getUnionTypeDef(typeDef, v);
304
+ const newQualifier = typeDef.discriminator != null ? `${qualifier}${v[typeDef.discriminator]}:` : qualifier;
305
+ return internalFlattenValueChildren(
306
+ valuePath,
307
+ typePath,
308
+ newQualifier,
309
+ childTypeDef,
310
+ v,
311
+ mapper,
312
+ r
313
+ );
314
+ }
315
+ function getUnionTypeDef(typeDef, v) {
316
+ if (typeDef.discriminator == null) {
317
+ return (0, import_base2.reduce)(
318
+ typeDef.unions,
319
+ function(acc, _k, t) {
320
+ if (t.type === 1 /* Literal */ && t.valuePrototype) {
321
+ if (t.valuePrototype.indexOf(v) >= 0) {
322
+ return t;
323
+ }
324
+ } else {
325
+ if (acc == null) {
326
+ return t;
327
+ }
328
+ }
329
+ return acc;
330
+ },
331
+ null
332
+ );
333
+ }
334
+ return typeDef.unions[v[typeDef.discriminator]];
335
+ }
336
+
337
+ // transformers/copies/mobx_copy.ts
338
+ function observeValue(v, def) {
339
+ if (v == null) {
340
+ return v;
341
+ }
342
+ switch (def.type) {
343
+ case 1 /* Literal */:
344
+ return v;
345
+ case 2 /* List */:
346
+ return import_mobx.observable.array(v, { deep: false });
347
+ case 3 /* Record */:
348
+ return (0, import_mobx.observable)(
349
+ v,
350
+ {},
351
+ {
352
+ deep: false
353
+ }
354
+ );
355
+ case 4 /* Object */:
356
+ return (0, import_mobx.makeObservable)(
357
+ v,
358
+ (0, import_base3.reduce)(
359
+ def.fields,
360
+ function(acc, k) {
361
+ acc[k] = import_mobx.observable;
362
+ return acc;
363
+ },
364
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
365
+ {}
366
+ ),
367
+ {
368
+ deep: false
369
+ }
370
+ );
371
+ case 5 /* Union */:
372
+ return observeValue(v, getUnionTypeDef(def, v));
373
+ default:
374
+ throw new import_base3.UnreachableError(def);
375
+ }
376
+ }
377
+ function mobxCopy(t, proto) {
378
+ return copyTo(t, proto, observeValue);
379
+ }
380
+
381
+ // transformers/flatteners/flatten_accessors_of.ts
382
+ function mapAccessor(_t, value, set) {
383
+ return {
384
+ value,
385
+ set
386
+ };
387
+ }
388
+ function flattenAccessorsOf(t, value, setValue) {
389
+ return flattenValueTypeTo(
390
+ t,
391
+ value,
392
+ setValue,
393
+ mapAccessor
394
+ );
395
+ }
396
+
397
+ // transformers/flatteners/flatten_json_value_to_type_paths_of.ts
398
+ function mapTypePaths(_t, _value, _set, typePath) {
399
+ return typePath;
400
+ }
401
+ function flattenJsonValueToTypePathsOf(t, value) {
402
+ return flattenValueTypeTo(
403
+ t,
404
+ value,
405
+ function() {
406
+ },
407
+ mapTypePaths
408
+ );
409
+ }
410
+
411
+ // transformers/flatteners/flatten_type_def_to.ts
412
+ var import_base4 = require("@strictly/base");
413
+ function flattenTypeDefTo({ definition }, mapper) {
414
+ const typeDefs = internalFlattenTypeDef("$", definition, {});
415
+ return (0, import_base4.reduce)(
416
+ typeDefs,
417
+ function(acc, key, typeDef) {
418
+ acc[key] = mapper(typeDef);
419
+ return acc;
420
+ },
421
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
422
+ {}
423
+ );
424
+ }
425
+ function internalFlattenTypeDef(path, t, r) {
426
+ r[path] = t;
427
+ return internalFlattenTypeDefChildren(path, "", t, r);
428
+ }
429
+ function internalFlattenTypeDefChildren(path, qualifier, t, r) {
430
+ switch (t.type) {
431
+ case 1 /* Literal */:
432
+ return r;
433
+ case 2 /* List */:
434
+ return internalFlattenedListTypeDefChildren(path, t, r);
435
+ case 3 /* Record */:
436
+ return internalFlattenRecordTypeDefChildren(path, t, r);
437
+ case 4 /* Object */:
438
+ return internalFlattenObjectTypeDefChildren(path, qualifier, t, r);
439
+ case 5 /* Union */:
440
+ return internalFlattenUnionTypeDefChildren(path, qualifier, t, r);
441
+ default:
442
+ throw new import_base4.UnreachableError(t);
443
+ }
444
+ }
445
+ function internalFlattenedListTypeDefChildren(path, { elements }, r) {
446
+ return internalFlattenTypeDef(jsonPath(path, "*"), elements, r);
447
+ }
448
+ function internalFlattenRecordTypeDefChildren(path, { valueTypeDef }, r) {
449
+ return internalFlattenTypeDef(jsonPath(path, "*"), valueTypeDef, r);
450
+ }
451
+ function internalFlattenObjectTypeDefChildren(path, qualifier, { fields }, r) {
452
+ return (0, import_base4.reduce)(
453
+ fields,
454
+ function(acc, fieldName, fieldTypeDef) {
455
+ return internalFlattenTypeDef(
456
+ jsonPath(path, fieldName, qualifier),
457
+ fieldTypeDef,
458
+ acc
459
+ );
460
+ },
461
+ r
462
+ );
463
+ }
464
+ function internalFlattenUnionTypeDefChildren(path, qualifier, {
465
+ discriminator,
466
+ unions
467
+ }, r) {
468
+ return (0, import_base4.reduce)(
469
+ unions,
470
+ function(acc, key, typeDef) {
471
+ return internalFlattenTypeDefChildren(
472
+ path,
473
+ discriminator != null ? `${qualifier}${key}:` : qualifier,
474
+ typeDef,
475
+ acc
476
+ );
477
+ },
478
+ r
479
+ );
480
+ }
481
+
482
+ // transformers/flatteners/flatten_type_defs_of.ts
483
+ function flattenTypeDefsOf(t) {
484
+ return flattenTypeDefTo(
485
+ t,
486
+ function(definition) {
487
+ return { definition };
488
+ }
489
+ );
490
+ }
491
+
492
+ // transformers/flatteners/value_path_to_type_path.ts
493
+ var import_base5 = require("@strictly/base");
494
+ function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
495
+ const valueSteps = valuePath.split(/\.|\[/g);
496
+ (0, import_base5.assertEqual)(valueSteps[0], "$");
497
+ const typeSteps = internalJsonValuePathToTypePath(
498
+ typeDef,
499
+ valueSteps.slice(1),
500
+ allowMissingPaths,
501
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
502
+ valuePath
503
+ );
504
+ typeSteps.unshift("$");
505
+ return typeSteps.join(".");
506
+ }
507
+ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths, originalValuePath) {
508
+ if (valueSteps.length === 0) {
509
+ return [];
510
+ }
511
+ const [
512
+ valueStep,
513
+ ...remainingValueSteps
514
+ ] = valueSteps;
515
+ switch (typeDef.type) {
516
+ case 1 /* Literal */:
517
+ if (allowMissingPaths) {
518
+ return valueSteps;
519
+ } else {
520
+ throw new import_base5.PreconditionFailedError(
521
+ "literal should terminate path {} ({})",
522
+ originalValuePath,
523
+ valueStep
524
+ );
525
+ }
526
+ case 2 /* List */:
527
+ return [
528
+ "*",
529
+ ...internalJsonValuePathToTypePath(
530
+ typeDef.elements,
531
+ remainingValueSteps,
532
+ allowMissingPaths,
533
+ originalValuePath
534
+ )
535
+ ];
536
+ case 3 /* Record */:
537
+ return [
538
+ "*",
539
+ ...internalJsonValuePathToTypePath(
540
+ typeDef.valueTypeDef,
541
+ remainingValueSteps,
542
+ allowMissingPaths,
543
+ originalValuePath
544
+ )
545
+ ];
546
+ case 4 /* Object */:
547
+ if (allowMissingPaths) {
548
+ if (typeDef.fields[valueStep] == null) {
549
+ return valueSteps;
550
+ }
551
+ } else {
552
+ (0, import_base5.assertExists)(typeDef.fields[valueStep], "missing field in {} ({})", originalValuePath, valueStep);
553
+ }
554
+ return [
555
+ valueStep,
556
+ ...internalJsonValuePathToTypePath(
557
+ typeDef.fields[valueStep],
558
+ remainingValueSteps,
559
+ allowMissingPaths,
560
+ originalValuePath
561
+ )
562
+ ];
563
+ case 5 /* Union */:
564
+ if (typeDef.discriminator == null) {
565
+ if (remainingValueSteps.length > 0) {
566
+ const union2 = (0, import_base5.reduce)(
567
+ typeDef.unions,
568
+ function(acc, _k, v) {
569
+ if (v.type !== 1 /* Literal */ || v.type === 1 /* Literal */ && v.valuePrototype == null) {
570
+ return v;
571
+ }
572
+ return acc;
573
+ },
574
+ null
575
+ );
576
+ (0, import_base5.assertExists)(union2, "expected a complex union {}", originalValuePath);
577
+ return internalJsonValuePathToTypePath(
578
+ union2,
579
+ valueSteps,
580
+ allowMissingPaths,
581
+ originalValuePath
582
+ );
583
+ } else {
584
+ return [];
585
+ }
586
+ } else {
587
+ const qualifierIndex = valueStep.indexOf(":");
588
+ if (qualifierIndex < 0) {
589
+ if (allowMissingPaths) {
590
+ return valueSteps;
591
+ } else {
592
+ throw new import_base5.PreconditionFailedError(
593
+ "mismatched qualifiers in {} (at {})",
594
+ originalValuePath,
595
+ valueStep
596
+ );
597
+ }
598
+ }
599
+ const qualifier = valueStep.substring(0, qualifierIndex);
600
+ const remainder = valueStep.substring(qualifierIndex + 1);
601
+ const union2 = (0, import_base5.assertExistsAndReturn)(typeDef.unions[qualifier], "missing union {}", qualifier);
602
+ const [
603
+ typeStep,
604
+ ...remainingTypeSteps
605
+ ] = internalJsonValuePathToTypePath(
606
+ union2,
607
+ [
608
+ remainder,
609
+ ...remainingValueSteps
610
+ ],
611
+ allowMissingPaths,
612
+ originalValuePath
613
+ );
614
+ return [
615
+ `${qualifier}:${typeStep}`,
616
+ ...remainingTypeSteps
617
+ ];
618
+ }
619
+ default:
620
+ throw new import_base5.UnreachableError(typeDef);
621
+ }
622
+ }
623
+
624
+ // types/builders.ts
625
+ var TypeDefBuilder = class {
626
+ constructor(definition) {
627
+ this.definition = definition;
628
+ }
629
+ // returns just the relevant types, which can help typescript
630
+ // from complaining about infinitely deep data structures
631
+ get narrow() {
632
+ return {
633
+ definition: this.definition
634
+ };
635
+ }
636
+ };
637
+ var LiteralTypeDefBuilder = class extends TypeDefBuilder {
638
+ };
639
+ var ListTypeDefBuilder = class extends TypeDefBuilder {
640
+ readonly() {
641
+ return this;
642
+ }
643
+ };
644
+ var RecordTypeDefBuilder = class extends TypeDefBuilder {
645
+ partial() {
646
+ return this;
647
+ }
648
+ readonly() {
649
+ return this;
650
+ }
651
+ };
652
+ var ObjectTypeDefBuilder = class _ObjectTypeDefBuilder extends TypeDefBuilder {
653
+ set(name, { definition: typeDef }) {
654
+ const newFields = {
655
+ [name]: typeDef
656
+ };
657
+ return new _ObjectTypeDefBuilder({
658
+ type: 4 /* Object */,
659
+ fields: {
660
+ ...this.definition.fields,
661
+ ...newFields
662
+ }
663
+ });
664
+ }
665
+ setReadonly(name, { definition: typeDef }) {
666
+ const newFields = {
667
+ [name]: typeDef
668
+ };
669
+ return new _ObjectTypeDefBuilder({
670
+ type: 4 /* Object */,
671
+ fields: {
672
+ ...this.definition.fields,
673
+ ...newFields
674
+ }
675
+ });
676
+ }
677
+ setOptional(name, { definition: typeDef }) {
678
+ const newFields = {
679
+ [name]: typeDef
680
+ };
681
+ return new _ObjectTypeDefBuilder({
682
+ type: 4 /* Object */,
683
+ fields: {
684
+ ...this.definition.fields,
685
+ ...newFields
686
+ }
687
+ });
688
+ }
689
+ setReadonlyOptional(name, { definition: typeDef }) {
690
+ const newFields = {
691
+ [name]: typeDef
692
+ };
693
+ return new _ObjectTypeDefBuilder({
694
+ type: 4 /* Object */,
695
+ fields: {
696
+ ...this.definition.fields,
697
+ ...newFields
698
+ }
699
+ });
700
+ }
701
+ };
702
+ var UnionTypeDefBuilder = class _UnionTypeDefBuilder extends TypeDefBuilder {
703
+ add(k, {
704
+ definition: typeDef
705
+ }) {
706
+ const {
707
+ discriminator,
708
+ unions
709
+ } = this.definition;
710
+ return new _UnionTypeDefBuilder(
711
+ {
712
+ type: 5 /* Union */,
713
+ discriminator,
714
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
715
+ unions: {
716
+ ...unions,
717
+ [k]: typeDef
718
+ }
719
+ }
720
+ );
721
+ }
722
+ };
723
+ function literal(value) {
724
+ return new LiteralTypeDefBuilder({
725
+ type: 1 /* Literal */,
726
+ valuePrototype: value
727
+ });
728
+ }
729
+ var stringType = literal();
730
+ var numberType = literal();
731
+ var booleanType = literal();
732
+ var nullType = literal([null]);
733
+ function nullable(nonNullable) {
734
+ return new UnionTypeDefBuilder(
735
+ {
736
+ type: 5 /* Union */,
737
+ discriminator: null,
738
+ unions: {
739
+ ["0"]: nonNullable.definition,
740
+ ["1"]: nullType.definition
741
+ }
742
+ }
743
+ );
744
+ }
745
+ function list(elements) {
746
+ return new ListTypeDefBuilder({
747
+ type: 2 /* List */,
748
+ elements: elements.definition
749
+ });
750
+ }
751
+ function record({ definition: typeDef }) {
752
+ return new RecordTypeDefBuilder({
753
+ type: 3 /* Record */,
754
+ // eslint-disable-next-line no-undefined
755
+ keyPrototype: void 0,
756
+ valueTypeDef: typeDef
757
+ });
758
+ }
759
+ function object() {
760
+ return new ObjectTypeDefBuilder({
761
+ type: 4 /* Object */,
762
+ fields: {}
763
+ });
764
+ }
765
+ function union(discriminator) {
766
+ return new UnionTypeDefBuilder(
767
+ {
768
+ type: 5 /* Union */,
769
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
770
+ discriminator: discriminator ?? null,
771
+ unions: {}
772
+ }
773
+ );
774
+ }
775
+ // Annotate the CommonJS export names for ESM import in node:
776
+ 0 && (module.exports = {
777
+ TypeDefType,
778
+ booleanType,
779
+ copy,
780
+ flattenAccessorsOf,
781
+ flattenJsonValueToTypePathsOf,
782
+ flattenTypeDefsOf,
783
+ flattenValueTypeTo,
784
+ getUnionTypeDef,
785
+ jsonPath,
786
+ jsonPathPop,
787
+ list,
788
+ literal,
789
+ mobxCopy,
790
+ nullType,
791
+ nullable,
792
+ numberType,
793
+ object,
794
+ record,
795
+ stringType,
796
+ union,
797
+ valuePathToTypePath
798
+ });