@es-joy/jsoe 0.2.0 → 0.4.0

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 (192) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.cjs +2 -0
  3. package/.ncurc.cjs +9 -0
  4. package/CHANGES.md +9 -0
  5. package/README.md +2 -2
  6. package/demo/index.html +8 -0
  7. package/demo/index.js +1 -1
  8. package/dist/formatAndTypeChoices.d.ts +56 -0
  9. package/dist/formatAndTypeChoices.d.ts.map +1 -0
  10. package/dist/formats/indexedDBKey.d.ts +4 -0
  11. package/dist/formats/indexedDBKey.d.ts.map +1 -0
  12. package/dist/formats/json.d.ts +4 -0
  13. package/dist/formats/json.d.ts.map +1 -0
  14. package/dist/formats/schemaAndArbitrary.d.ts +6 -0
  15. package/dist/formats/schemaAndArbitrary.d.ts.map +1 -0
  16. package/dist/formats/schemaOnly.d.ts +6 -0
  17. package/dist/formats/schemaOnly.d.ts.map +1 -0
  18. package/dist/formats/structuredCloning.d.ts +20 -0
  19. package/dist/formats/structuredCloning.d.ts.map +1 -0
  20. package/dist/formats.d.ts +26 -0
  21. package/dist/formats.d.ts.map +1 -0
  22. package/dist/fundamentalTypes/BooleanObjectType.d.ts +8 -0
  23. package/dist/fundamentalTypes/BooleanObjectType.d.ts.map +1 -0
  24. package/dist/fundamentalTypes/NumberObjectType.d.ts +6 -0
  25. package/dist/fundamentalTypes/NumberObjectType.d.ts.map +1 -0
  26. package/dist/fundamentalTypes/StringObjectType.d.ts +6 -0
  27. package/dist/fundamentalTypes/StringObjectType.d.ts.map +1 -0
  28. package/dist/fundamentalTypes/arrayReferenceType.d.ts +8 -0
  29. package/dist/fundamentalTypes/arrayReferenceType.d.ts.map +1 -0
  30. package/dist/fundamentalTypes/arrayType.d.ts +60 -0
  31. package/dist/fundamentalTypes/arrayType.d.ts.map +1 -0
  32. package/dist/fundamentalTypes/bigintType.d.ts +6 -0
  33. package/dist/fundamentalTypes/bigintType.d.ts.map +1 -0
  34. package/dist/fundamentalTypes/dateType.d.ts +26 -0
  35. package/dist/fundamentalTypes/dateType.d.ts.map +1 -0
  36. package/dist/fundamentalTypes/nullType.d.ts +6 -0
  37. package/dist/fundamentalTypes/nullType.d.ts.map +1 -0
  38. package/dist/fundamentalTypes/numberType.d.ts +6 -0
  39. package/dist/fundamentalTypes/numberType.d.ts.map +1 -0
  40. package/dist/fundamentalTypes/objectReferenceType.d.ts +8 -0
  41. package/dist/fundamentalTypes/objectReferenceType.d.ts.map +1 -0
  42. package/dist/fundamentalTypes/objectType.d.ts +6 -0
  43. package/dist/fundamentalTypes/objectType.d.ts.map +1 -0
  44. package/dist/fundamentalTypes/regexpType.d.ts +8 -0
  45. package/dist/fundamentalTypes/regexpType.d.ts.map +1 -0
  46. package/dist/fundamentalTypes/sparseUndefinedType.d.ts +6 -0
  47. package/dist/fundamentalTypes/sparseUndefinedType.d.ts.map +1 -0
  48. package/dist/fundamentalTypes/stringType.d.ts +6 -0
  49. package/dist/fundamentalTypes/stringType.d.ts.map +1 -0
  50. package/dist/fundamentalTypes/undefinedType.d.ts +6 -0
  51. package/dist/fundamentalTypes/undefinedType.d.ts.map +1 -0
  52. package/dist/index.d.ts +5 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/dist/src/deepEqual.d.ts +3 -0
  55. package/dist/src/deepEqual.d.ts.map +1 -0
  56. package/dist/src/formatAndTypeChoices.d.ts +47 -0
  57. package/dist/src/formatAndTypeChoices.d.ts.map +1 -0
  58. package/dist/src/formats/indexedDBKey.d.ts +9 -0
  59. package/dist/src/formats/indexedDBKey.d.ts.map +1 -0
  60. package/dist/src/formats/json.d.ts +9 -0
  61. package/dist/src/formats/json.d.ts.map +1 -0
  62. package/dist/src/formats/schemaAndArbitrary.d.ts +6 -0
  63. package/dist/src/formats/schemaAndArbitrary.d.ts.map +1 -0
  64. package/dist/src/formats/schemaOnly.d.ts +6 -0
  65. package/dist/src/formats/schemaOnly.d.ts.map +1 -0
  66. package/dist/src/formats/structuredCloning.d.ts +15 -0
  67. package/dist/src/formats/structuredCloning.d.ts.map +1 -0
  68. package/dist/src/formats.d.ts +18 -0
  69. package/dist/src/formats.d.ts.map +1 -0
  70. package/dist/src/fundamentalTypes/BooleanObjectType.d.ts +6 -0
  71. package/dist/src/fundamentalTypes/BooleanObjectType.d.ts.map +1 -0
  72. package/dist/src/fundamentalTypes/NumberObjectType.d.ts +6 -0
  73. package/dist/src/fundamentalTypes/NumberObjectType.d.ts.map +1 -0
  74. package/dist/src/fundamentalTypes/StringObjectType.d.ts +6 -0
  75. package/dist/src/fundamentalTypes/StringObjectType.d.ts.map +1 -0
  76. package/dist/src/fundamentalTypes/arrayReferenceType.d.ts +6 -0
  77. package/dist/src/fundamentalTypes/arrayReferenceType.d.ts.map +1 -0
  78. package/dist/src/fundamentalTypes/arrayType.d.ts +6 -0
  79. package/dist/src/fundamentalTypes/arrayType.d.ts.map +1 -0
  80. package/dist/src/fundamentalTypes/bigintType.d.ts +6 -0
  81. package/dist/src/fundamentalTypes/bigintType.d.ts.map +1 -0
  82. package/dist/src/fundamentalTypes/dateType.d.ts +6 -0
  83. package/dist/src/fundamentalTypes/dateType.d.ts.map +1 -0
  84. package/dist/src/fundamentalTypes/nullType.d.ts +6 -0
  85. package/dist/src/fundamentalTypes/nullType.d.ts.map +1 -0
  86. package/dist/src/fundamentalTypes/numberType.d.ts +6 -0
  87. package/dist/src/fundamentalTypes/numberType.d.ts.map +1 -0
  88. package/dist/src/fundamentalTypes/objectReferenceType.d.ts +6 -0
  89. package/dist/src/fundamentalTypes/objectReferenceType.d.ts.map +1 -0
  90. package/dist/src/fundamentalTypes/objectType.d.ts +6 -0
  91. package/dist/src/fundamentalTypes/objectType.d.ts.map +1 -0
  92. package/dist/src/fundamentalTypes/regexpType.d.ts +6 -0
  93. package/dist/src/fundamentalTypes/regexpType.d.ts.map +1 -0
  94. package/dist/src/fundamentalTypes/sparseUndefinedType.d.ts +6 -0
  95. package/dist/src/fundamentalTypes/sparseUndefinedType.d.ts.map +1 -0
  96. package/dist/src/fundamentalTypes/stringType.d.ts +6 -0
  97. package/dist/src/fundamentalTypes/stringType.d.ts.map +1 -0
  98. package/dist/src/fundamentalTypes/undefinedType.d.ts +6 -0
  99. package/dist/src/fundamentalTypes/undefinedType.d.ts.map +1 -0
  100. package/dist/src/index.d.ts +5 -0
  101. package/dist/src/index.d.ts.map +1 -0
  102. package/dist/src/subTypes/blobHTMLType.d.ts +26 -0
  103. package/dist/src/subTypes/blobHTMLType.d.ts.map +1 -0
  104. package/dist/src/subTypes/falseType.d.ts +6 -0
  105. package/dist/src/subTypes/falseType.d.ts.map +1 -0
  106. package/dist/src/subTypes/trueType.d.ts +6 -0
  107. package/dist/src/subTypes/trueType.d.ts.map +1 -0
  108. package/dist/src/superTypes/SpecialNumberSuperType.d.ts +11 -0
  109. package/dist/src/superTypes/SpecialNumberSuperType.d.ts.map +1 -0
  110. package/dist/src/superTypes/SpecialRealNumberSuperType.d.ts +6 -0
  111. package/dist/src/superTypes/SpecialRealNumberSuperType.d.ts.map +1 -0
  112. package/dist/src/typeChoices.d.ts +30 -0
  113. package/dist/src/typeChoices.d.ts.map +1 -0
  114. package/dist/src/types.d.ts +333 -0
  115. package/dist/src/types.d.ts.map +1 -0
  116. package/dist/src/utils/dialogs.d.ts +29 -0
  117. package/dist/src/utils/dialogs.d.ts.map +1 -0
  118. package/dist/src/utils/jsonPointer.d.ts +42 -0
  119. package/dist/src/utils/jsonPointer.d.ts.map +1 -0
  120. package/dist/src/utils/templateUtils.d.ts +59 -0
  121. package/dist/src/utils/templateUtils.d.ts.map +1 -0
  122. package/dist/src/utils/types.d.ts +2 -0
  123. package/dist/src/utils/types.d.ts.map +1 -0
  124. package/dist/subTypes/blobHTMLType.d.ts +14 -0
  125. package/dist/subTypes/blobHTMLType.d.ts.map +1 -0
  126. package/dist/subTypes/falseType.d.ts +8 -0
  127. package/dist/subTypes/falseType.d.ts.map +1 -0
  128. package/dist/subTypes/trueType.d.ts +8 -0
  129. package/dist/subTypes/trueType.d.ts.map +1 -0
  130. package/dist/superTypes/SpecialNumberSuperType.d.ts +14 -0
  131. package/dist/superTypes/SpecialNumberSuperType.d.ts.map +1 -0
  132. package/dist/superTypes/SpecialRealNumberSuperType.d.ts +6 -0
  133. package/dist/superTypes/SpecialRealNumberSuperType.d.ts.map +1 -0
  134. package/dist/typeChoices.d.ts +103 -0
  135. package/dist/typeChoices.d.ts.map +1 -0
  136. package/dist/types.d.ts +523 -0
  137. package/dist/types.d.ts.map +1 -0
  138. package/dist/utils/dialogs.d.ts +109 -0
  139. package/dist/utils/dialogs.d.ts.map +1 -0
  140. package/dist/utils/jsonPointer.d.ts +46 -0
  141. package/dist/utils/jsonPointer.d.ts.map +1 -0
  142. package/dist/utils/templateUtils.d.ts +59 -0
  143. package/dist/utils/templateUtils.d.ts.map +1 -0
  144. package/dist/utils/types.d.ts +2 -0
  145. package/dist/utils/types.d.ts.map +1 -0
  146. package/dist/vendor/jamilih/dist/jml-es.d.ts +436 -0
  147. package/dist/vendor/jamilih/dist/jml-es.d.ts.map +1 -0
  148. package/dist/vendor/typeson-registry/dist/index.d.ts +400 -0
  149. package/dist/vendor/typeson-registry/dist/index.d.ts.map +1 -0
  150. package/dist/vendor-imports.d.ts +3 -0
  151. package/dist/vendor-imports.d.ts.map +1 -0
  152. package/package.json +19 -14
  153. package/postorder-traversal.js +58 -0
  154. package/src/formatAndTypeChoices.js +95 -50
  155. package/src/formats/indexedDBKey.js +49 -56
  156. package/src/formats/json.js +47 -56
  157. package/src/formats/schemaOnly.js +12 -2
  158. package/src/formats/structuredCloning.js +162 -105
  159. package/src/formats.js +38 -10
  160. package/src/fundamentalTypes/BooleanObjectType.js +6 -4
  161. package/src/fundamentalTypes/NumberObjectType.js +5 -3
  162. package/src/fundamentalTypes/StringObjectType.js +1 -1
  163. package/src/fundamentalTypes/arrayReferenceType.js +29 -14
  164. package/src/fundamentalTypes/arrayType.js +789 -278
  165. package/src/fundamentalTypes/bigintType.js +2 -2
  166. package/src/fundamentalTypes/dateType.js +42 -14
  167. package/src/fundamentalTypes/nullType.js +1 -1
  168. package/src/fundamentalTypes/numberType.js +2 -2
  169. package/src/fundamentalTypes/objectReferenceType.js +7 -5
  170. package/src/fundamentalTypes/regexpType.js +45 -24
  171. package/src/fundamentalTypes/sparseUndefinedType.js +7 -1
  172. package/src/fundamentalTypes/stringType.js +1 -1
  173. package/src/fundamentalTypes/undefinedType.js +2 -2
  174. package/src/jsoe.css +1 -1
  175. package/src/subTypes/blobHTMLType.js +42 -22
  176. package/src/subTypes/falseType.js +2 -2
  177. package/src/subTypes/trueType.js +2 -2
  178. package/src/superTypes/SpecialNumberSuperType.js +30 -16
  179. package/src/superTypes/SpecialRealNumberSuperType.js +62 -0
  180. package/src/typeChoices.js +137 -55
  181. package/src/types.js +312 -148
  182. package/src/utils/dialogs.js +98 -29
  183. package/src/utils/jsonPointer.js +4 -0
  184. package/src/utils/templateUtils.js +27 -12
  185. package/src/vendor-imports.js +8 -0
  186. package/tsconfig.json +18 -0
  187. package/typings/sceditor.d.ts +20 -0
  188. package/vendor/jamilih/dist/jml-es.js +1019 -389
  189. package/vendor/jamilih/dist/jml.d.ts +436 -0
  190. package/vendor/jamilih/package.json +136 -0
  191. package/vendor/typeson-registry/dist/index.js +382 -84
  192. package/src/superTypes/InfinitiesSuperType.js +0 -49
@@ -1,4 +1,4 @@
1
- import {jml} from '../vendor/jamilih/dist/jml-es.js';
1
+ import {jml} from './vendor-imports.js';
2
2
  import {buildTypeChoices} from './typeChoices.js';
3
3
  import Types from './types.js';
4
4
  import {getControlsForFormatAndValue} from './formats.js';
@@ -16,7 +16,8 @@ import {$e, DOM} from './utils/templateUtils.js';
16
16
  */
17
17
  export const getFormatAndSchemaChoices = ({schema, hasKeyPath} = {}) => {
18
18
  const hasSchema = typeof schema === 'string';
19
- return [
19
+ // eslint-disable-next-line max-len -- Long
20
+ return /** @type {[optText: string, opts: {value: string, selected?: boolean}][]} */ ([
20
21
  ['JSON only', {value: 'json'}],
21
22
  ...(hasKeyPath
22
23
  ? []
@@ -48,14 +49,26 @@ export const getFormatAndSchemaChoices = ({schema, hasKeyPath} = {}) => {
48
49
  'for structured cloning'
49
50
  }]
50
51
  */
51
- ].map(([optText, optAtts]) => {
52
- return jml('option', optAtts, [optText]);
53
- }).reduce((frag, option) => {
52
+ ]).map(([optText, optAtts]) => {
53
+ return /** @type {HTMLOptionElement} */ (
54
+ jml('option', optAtts, [optText])
55
+ );
56
+ }).reduce((
57
+ frag,
58
+ option
59
+ ) => {
54
60
  frag.append(option);
55
61
  return frag;
56
62
  }, document.createDocumentFragment());
57
63
  };
58
64
 
65
+ /**
66
+ * @callback SetValue
67
+ * @param {import('./formats.js').StructuredCloneValue} value
68
+ * @param {import('./types.js').StateObject} stateObj
69
+ * @returns {Promise<void>}
70
+ */
71
+
59
72
  /**
60
73
  * Builds a selector and container for types.
61
74
  * @param {object} cfg
@@ -77,9 +90,10 @@ export const getFormatAndSchemaChoices = ({schema, hasKeyPath} = {}) => {
77
90
  * typesHolder: TypesHolder,
78
91
  * domArray: [formatChoices: FormatChoices, typesHolder: TypesHolder],
79
92
  * getValue: (stateObj: import('./types.js').StateObject,
80
- * currentPath: string) => StructuredCloneValue,
81
- * getType: () => string|undefined,
82
- * validValuesSet: () => boolean
93
+ * currentPath: string) => import('./formats.js').StructuredCloneValue,
94
+ * getType: () => string|null|undefined,
95
+ * validValuesSet: () => boolean,
96
+ * setValue: SetValue
83
97
  * }} The selector for types and the container for them. Both should be
84
98
  * added to the page.
85
99
  */
@@ -92,7 +106,7 @@ export function formatAndTypeChoices ({
92
106
  typeNamespace
93
107
  }) {
94
108
  const format = 'structuredCloning';
95
- const formatChoices = jml('select', {
109
+ const formatChoices = /** @type {HTMLSelectElement} */ (jml('select', {
96
110
  class: 'formatChoices',
97
111
  hidden: singleValue,
98
112
  // is: 'main-type-choices',
@@ -130,8 +144,10 @@ export function formatAndTypeChoices ({
130
144
  $buildTypeChoices () {
131
145
  DOM.removeChildren(typesHolder);
132
146
  jml({'#': buildTypeChoices({
133
- topRoot: $e(typesHolder, 'div[data-type]'),
134
- resultType: 'both',
147
+ topRoot: /** @type {HTMLDivElement} */ (
148
+ $e(typesHolder, 'div[data-type]')
149
+ ),
150
+ // resultType: 'both',
135
151
  format: this.value,
136
152
  typeNamespace,
137
153
  requireObject: hasKeyPath,
@@ -142,18 +158,20 @@ export function formatAndTypeChoices ({
142
158
  }
143
159
  },
144
160
  $on: {change () {
145
- this.$buildTypeChoices();
161
+ /** @type {Element & {$buildTypeChoices: TypeChoiceBuilder}} */ (
162
+ this
163
+ ).$buildTypeChoices();
146
164
  }}
147
- }, [getFormatAndSchemaChoices({schema, hasKeyPath})]);
165
+ }, [getFormatAndSchemaChoices({schema, hasKeyPath})]));
148
166
 
149
167
  /**
150
168
  * @callback TypeRootGetter
151
- * @returns {void}
169
+ * @returns {Element|null}
152
170
  */
153
171
 
154
172
  /**
155
173
  * @callback TypeSelectGetter
156
- * @returns {void}
174
+ * @returns {HTMLSelectElement|null}
157
175
  */
158
176
 
159
177
  /**
@@ -162,24 +180,30 @@ export function formatAndTypeChoices ({
162
180
  * @property {TypeSelectGetter} $getTypeSelect
163
181
  */
164
182
 
165
- const typesHolder = jml('div', {class: 'typesHolder', $custom: {
166
- /**
167
- * @type {TypeRootGetter}
168
- */
169
- $getTypeRoot () {
170
- return $e(this, 'div[data-type]');
171
- },
172
- /**
173
- * @type {TypeSelectGetter}
174
- */
175
- $getTypeSelect () {
176
- return $e(this, `.typeChoices-${typeNamespace}`);
177
- }
178
- }});
183
+ const typesHolder = /** @type {HTMLDivElement} */ (
184
+ jml('div', {class: 'typesHolder', $custom: {
185
+ /**
186
+ * @type {TypeRootGetter}
187
+ * @this {Element}
188
+ */
189
+ $getTypeRoot () {
190
+ return $e(this, 'div[data-type]');
191
+ },
192
+ /**
193
+ * @type {TypeSelectGetter}
194
+ * @this {HTMLSelectElement}
195
+ */
196
+ $getTypeSelect () {
197
+ return /** @type {HTMLSelectElement} */ (
198
+ $e(this, `.typeChoices-${typeNamespace}`)
199
+ );
200
+ }
201
+ }})
202
+ );
179
203
 
180
204
  jml({'#': buildTypeChoices({
181
- resultType: 'both',
182
- topRoot: $e(typesHolder, 'div[data-type]'),
205
+ // resultType: 'both',
206
+ topRoot: /** @type {HTMLDivElement} */ ($e(typesHolder, 'div[data-type]')),
183
207
  format,
184
208
  typeNamespace,
185
209
  requireObject: hasKeyPath,
@@ -200,46 +224,67 @@ export function formatAndTypeChoices ({
200
224
  * @param {import('./types.js').StateObject} [stateObj] Will
201
225
  * auto-set `typeNamespace` and `format`
202
226
  * @param {string} [currentPath]
203
- * @returns {StructuredCloneValue}
227
+ * @returns {import('./formats.js').StructuredCloneValue}
204
228
  */
205
229
  getValue (stateObj, currentPath) {
206
- const root = typesHolder.$getTypeRoot();
207
- return Types.getValueForRoot(root, {
230
+ // eslint-disable-next-line max-len -- Long
231
+ const root = /** @type {HTMLDivElement & {$getTypeRoot: TypeRootGetter}} */ (
232
+ typesHolder
233
+ ).$getTypeRoot();
234
+ return Types.getValueForRoot(/** @type {HTMLDivElement} */ (root), {
208
235
  typeNamespace,
209
- format: formatChoices.value,
236
+ format: /** @type {import('./formats.js').AvailableFormat} */ (
237
+ formatChoices.value
238
+ ),
210
239
  ...stateObj
211
240
  }, currentPath);
212
241
  },
213
242
 
214
243
  /**
215
- * @returns {string|undefined}
244
+ * @returns {string|null|undefined}
216
245
  */
217
246
  getType () {
218
- const root = typesHolder.$getTypeRoot();
219
- return Types.getTypeForRoot(root);
247
+ // eslint-disable-next-line max-len -- Long
248
+ const root = /** @type {HTMLDivElement & {$getTypeRoot: TypeRootGetter}} */ (
249
+ typesHolder
250
+ ).$getTypeRoot();
251
+ return Types.getTypeForRoot(/** @type {HTMLDivElement} */ (root));
220
252
  },
221
253
 
222
254
  /**
223
255
  * @returns {boolean}
224
256
  */
225
257
  validValuesSet () {
226
- const root = typesHolder.$getTypeRoot();
227
- const form = root.closest('form');
258
+ // eslint-disable-next-line max-len -- Long
259
+ const root = /** @type {HTMLDivElement & {$getTypeRoot: TypeRootGetter}} */ (
260
+ typesHolder
261
+ ).$getTypeRoot();
262
+ const form = /** @type {HTMLFormElement} */ (
263
+ /** @type {Element} */ (root).closest('form')
264
+ );
228
265
  return Types.validValuesSet({form, typeNamespace});
229
266
  },
230
267
 
231
- /**
232
- * @param {StructuredCloneValue} value
233
- * @param {import('./types.js').StateObject} stateObj
234
- * @returns {Promise<void>}
235
- */
268
+ /** @type {SetValue} */
236
269
  async setValue (value, stateObj) {
237
- const rootEditUI = await getControlsForFormatAndValue(
238
- formatChoices.value, value, stateObj
270
+ const rootEditUI = /** @type {HTMLDivElement} */ (
271
+ await getControlsForFormatAndValue(
272
+ /** @type {import('./formats.js').AvailableFormat} */ (
273
+ formatChoices.value
274
+ ),
275
+ value,
276
+ stateObj
277
+ )
239
278
  );
240
- const type = Types.getTypeForRoot(rootEditUI);
241
- const sel = typesHolder.$getTypeSelect();
242
- sel.$addTypeAndEditUI({type, editUI: rootEditUI});
279
+ const type = /** @type {string} */ (Types.getTypeForRoot(rootEditUI));
280
+ // eslint-disable-next-line max-len -- Long
281
+ const sel = /** @type {HTMLDivElement & {$getTypeSelect: TypeSelectGetter}} */ (
282
+ typesHolder
283
+ ).$getTypeSelect();
284
+ // eslint-disable-next-line max-len -- Long
285
+ /** @type {HTMLSelectElement & {$addTypeAndEditUI: import('./typeChoices.js').AddTypeAndEditUI}} */ (
286
+ sel
287
+ ).$addTypeAndEditUI({type, editUI: rootEditUI});
243
288
  }
244
289
  };
245
290
  }
@@ -1,66 +1,59 @@
1
- import * as structuredCloning from './structuredCloning.js';
1
+ import structuredCloning from './structuredCloning.js';
2
2
 
3
- // Todo (low): Support ArrayBuffer
4
- /**
5
- * @returns {string[]}
6
- */
7
- export const types = () => [
8
- 'number', 'Infinities', 'string', 'ValidDate', 'array'
9
- ];
10
-
11
- // A hack until we simply pass in our own types or do own parsing
12
-
13
- /**
14
- * @param {string} newType
15
- * @param {Date|GenericArray} value
16
- * @returns {boolean}
17
- */
18
- export const testInvalid = (newType, value) => {
19
- switch (newType) {
20
- /* istanbul ignore next -- Shouldn't occur as it is a preset */
21
- case 'Infinities':
3
+ /** @type {import('../formats.js').Format} */
4
+ const indexedDBKey = {
5
+ types: () => [
6
+ 'number', 'SpecialRealNumber', 'string', 'ValidDate', 'array'
7
+ ],
8
+ // A hack until we simply pass in our own types or do own parsing
9
+ testInvalid (newType, value) {
10
+ switch (newType) {
22
11
  /* istanbul ignore next -- Shouldn't occur as it is a preset */
23
- return Number.isNaN(value);
24
- case 'ValidDate':
25
- return Number.isNaN(value.getTime());
26
- case 'array':
27
- return Object.keys(value).length !== value.length;
28
- /* istanbul ignore next -- Shouldn't occur */
29
- default:
12
+ case 'SpecialRealNumber':
13
+ /* istanbul ignore next -- Shouldn't occur as it is a preset */
14
+ return Number.isNaN(value);
15
+ case 'ValidDate':
16
+ return Number.isNaN(/** @type {Date} */ (value).getTime());
17
+ case 'array':
18
+ return Object.keys(value).length !==
19
+ /** @type {Array<import('../formats.js').StructuredCloneValue>} */ (
20
+ value
21
+ ).length;
30
22
  /* istanbul ignore next -- Shouldn't occur */
23
+ default:
24
+ /* istanbul ignore next -- Shouldn't occur */
25
+ return false; // Todo: Should this throw?
26
+ }
27
+ },
28
+ convertFromTypeson (typesonType) {
29
+ return typesonToIndexedDBKey.get(typesonType);
30
+ },
31
+ iterate (records, stateObj) {
32
+ stateObj.format = 'indexedDBKey';
33
+ return structuredCloning.iterate(records, stateObj);
34
+ },
35
+ getTypesForState (state) {
36
+ if (!state || ['array'].includes(state)) {
37
+ return this.types();
38
+ }
39
+ /* istanbul ignore next -- Can't be object, so shouldn't reach here */
31
40
  return undefined; // Todo: Should this throw?
32
41
  }
33
42
  };
34
43
 
35
- /**
36
- * @param {string} typesonType
37
- * @returns {string}
38
- */
39
- export const convertFromTypeson = (typesonType) => {
40
- return {
41
- SpecialNumber: 'Infinities', // This shouldn't occur as it is a preset
42
- date: 'ValidDate',
43
- // sparseArrays: 'array',
44
- arrayNonindexKeys: 'array'
45
- }[typesonType];
46
- };
44
+ // Todo (low): Support ArrayBuffer
47
45
 
48
46
  /**
49
- * @type {import('./structuredCloning.js').FormatIterator}
50
- */
51
- export const iterate = (records, stateObj) => {
52
- stateObj.format = 'indexedDBKey';
53
- return structuredCloning.iterate(records, stateObj);
54
- };
47
+ * @type {Map<import('../types.js').AvailableType,
48
+ * import('../types.js').AvailableType>
49
+ * }
50
+ */
51
+ const typesonToIndexedDBKey = new Map([
52
+ // This shouldn't occur as it is a preset
53
+ ['SpecialNumber', 'SpecialRealNumber'],
54
+ ['date', 'ValidDate'],
55
+ // 'sparseArrays', 'array',
56
+ ['arrayNonindexKeys', 'array']
57
+ ]);
55
58
 
56
- /**
57
- * @param {"array"|undefined} state
58
- * @returns {undefined|string[]}
59
- */
60
- export const getTypesForState = function (state) {
61
- if (!state || ['array'].includes(state)) {
62
- return this.types();
63
- }
64
- /* istanbul ignore next -- Can't be object, so shouldn't reach here */
65
- return undefined; // Todo: Should this throw?
66
- };
59
+ export default indexedDBKey;
@@ -1,64 +1,55 @@
1
- import * as structuredCloning from './structuredCloning.js';
1
+ import structuredCloning from './structuredCloning.js';
2
2
 
3
3
  /**
4
- * @returns {string[]}
4
+ * @type {Map<import('../types.js').AvailableType,
5
+ * import('../types.js').AvailableType>
6
+ * }
5
7
  */
6
- export const types = () => [
7
- 'null', 'true', 'false', 'number', 'string', 'array', 'object'
8
- ];
8
+ const typesonToJson = new Map([
9
+ ['arrayNonindexKeys', 'array']
10
+ ]);
9
11
 
10
- /**
11
- * @type {import('./structuredCloning.js').FormatIterator}
12
- */
13
- export const iterate = (records, stateObj) => {
14
- // Todo (low): Add a more optimal (`JSON.stringify`-based iterator)
15
- const recs = records;
16
- // I believe this escaping should be by Typeson itself
17
- // if (records && typeof records === 'object' && records.$types) {
18
- // recs = {$: records, $types: true};
19
- // }
20
- stateObj.format = 'json';
21
- return structuredCloning.iterate(recs, stateObj);
22
- };
23
-
24
- // A hack until we simply pass in our own types or do own parsing
25
- /**
26
- * @param {string} typesonType
27
- * @returns {string}
28
- */
29
- export const convertFromTypeson = (typesonType) => {
30
- return {
31
- // sparseArrays: 'array',
32
- arrayNonindexKeys: 'array'
33
- }[typesonType];
34
- };
35
-
36
- /**
37
- * @param {string} newType
38
- * @param {GenericArray} value
39
- * @returns {boolean|undefined}
40
- */
41
- export const testInvalid = (newType, value) => {
42
- switch (newType) {
43
- case 'array':
44
- return Object.keys(value).length !== value.length;
45
- /* istanbul ignore next -- Shouldn't occur */
46
- default:
12
+ /** @type {import('../formats.js').Format} */
13
+ const json = {
14
+ types: () => [
15
+ 'null', 'true', 'false', 'number', 'string', 'array', 'object'
16
+ ],
17
+ iterate (records, stateObj) {
18
+ // Todo (low): Add a more optimal (`JSON.stringify`-based iterator)
19
+ const recs = records;
20
+ // I believe this escaping should be by Typeson itself
21
+ // if (records && typeof records === 'object' && records.$types) {
22
+ // recs = {$: records, $types: true};
23
+ // }
24
+ stateObj.format = 'json';
25
+ return structuredCloning.iterate(recs, stateObj);
26
+ },
27
+ // A hack until we simply pass in our own types or do own parsing
28
+ convertFromTypeson (typesonType) {
29
+ return typesonToJson.get(typesonType);
30
+ },
31
+ testInvalid (newType, value) {
32
+ switch (newType) {
33
+ case 'array':
34
+ return Object.keys(value).length !==
35
+ /** @type {Array<import('../formats.js').StructuredCloneValue>} */ (
36
+ value
37
+ ).length;
47
38
  /* istanbul ignore next -- Shouldn't occur */
48
- return undefined; // Todo: Fix?
39
+ default:
40
+ /* istanbul ignore next -- Shouldn't occur */
41
+ return undefined; // Todo: Fix?
42
+ }
43
+ },
44
+ getTypesForState (state) {
45
+ /* istanbul ignore else -- No other states apparently */
46
+ if (!state || ['array', 'object'].includes(state)) {
47
+ return this.types();
48
+ }
49
+ // Todo: Should this throw?
50
+ /* istanbul ignore next -- No other states apparently */
51
+ return undefined;
49
52
  }
50
53
  };
51
54
 
52
- /**
53
- * @param {string} state
54
- * @returns {string[]|undefined}
55
- */
56
- export const getTypesForState = function (state) {
57
- /* istanbul ignore else -- No other states apparently */
58
- if (!state || ['array', 'object'].includes(state)) {
59
- return this.types();
60
- }
61
- // Todo: Should this throw?
62
- /* istanbul ignore next -- No other states apparently */
63
- return undefined;
64
- };
55
+ export default json;
@@ -3,10 +3,13 @@
3
3
  sonarjs/no-empty-collection,
4
4
  sonarjs/no-identical-conditions,
5
5
  no-constant-binary-expression -- Just debugging */
6
- import Formats from '../formats.js';
6
+ import {getTypeForFormatStateAndValue} from '../formats.js';
7
7
  import * as structuredCloning from './structuredCloning.js';
8
8
  import deepEqual from '../deepEqual.js';
9
9
 
10
+ /**
11
+ * @typedef {import('./formats.js').StructuredCloneValue} StructuredCloneValue
12
+ */
10
13
  /**
11
14
  * @type {import('./structuredCloning.js').FormatIterator}
12
15
  */
@@ -31,11 +34,18 @@ export const iterate = (records, stateObj) => {
31
34
  const resolveRef = (/* {schema, possibleSchemaObject} */) => {
32
35
  // Todo?
33
36
  };
37
+
38
+ /**
39
+ * @param {object} cfg
40
+ * @param {StructuredCloneValue[]} cfg.list
41
+ * @param {StructuredCloneValue} cfg.value
42
+ * @returns {boolean}
43
+ */
34
44
  const valueInEnumList = ({list, value}) => {
35
45
  return list.some((en) => deepEqual(en, value));
36
46
  };
37
47
  const getTypesForValues = ({enumList}) => enumList.map(
38
- (value) => Formats.getTypeForFormatStateAndValue({
48
+ (value) => getTypeForFormatStateAndValue({
39
49
  value,
40
50
  format: 'structuredCloning',
41
51
  state: 'arrayNonindexKeys'