shinkansen-transmission 2.2.234 → 2.3.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.
- package/babel.config.cjs +1 -0
- package/eslint.config.mjs +28 -30
- package/index.d.mts +321 -16
- package/package.json +8 -8
- package/src/index.cjs +2 -0
- package/src/index.d.mts +10 -9
- package/src/transmission/common/index.d.mts +152 -87
- package/src/transmission/common/index.mjs +627 -193
- package/src/transmission/from-document-to-hash/array/index.mjs +20 -4
- package/src/transmission/from-document-to-hash/boolean/index.mjs +25 -9
- package/src/transmission/from-document-to-hash/index.d.mts +8 -19
- package/src/transmission/from-document-to-hash/index.mjs +102 -16
- package/src/transmission/from-document-to-hash/null/index.mjs +25 -9
- package/src/transmission/from-document-to-hash/number/index.mjs +25 -9
- package/src/transmission/from-document-to-hash/object/index.mjs +22 -6
- package/src/transmission/from-document-to-hash/string/index.mjs +25 -9
- package/src/transmission/from-hash-to-document/array/index.mjs +22 -5
- package/src/transmission/from-hash-to-document/boolean/index.mjs +21 -4
- package/src/transmission/from-hash-to-document/index.d.mts +24 -26
- package/src/transmission/from-hash-to-document/index.mjs +264 -142
- package/src/transmission/from-hash-to-document/null/index.mjs +21 -4
- package/src/transmission/from-hash-to-document/number/index.mjs +21 -4
- package/src/transmission/from-hash-to-document/object/index.mjs +22 -5
- package/src/transmission/from-hash-to-document/string/index.mjs +26 -4
- package/src/transmission/index.d.mts +4 -10
- package/src/transmission/to-zashiki/index.d.mts +5 -13
- package/src/transmission/to-zashiki/params/index.mjs +430 -0
- package/src/transmission/to-zashiki/render/array/index.mjs +737 -0
- package/src/transmission/to-zashiki/render/boolean/index.mjs +794 -0
- package/src/transmission/to-zashiki/render/index.mjs +157 -0
- package/src/transmission/to-zashiki/render/null/index.mjs +796 -0
- package/src/transmission/to-zashiki/render/number/index.mjs +940 -0
- package/src/transmission/to-zashiki/render/object/index.mjs +653 -0
- package/src/transmission/to-zashiki/render/string/index.mjs +910 -0
- package/src/transmission/to-zashiki/render-params/root-schema/index.mjs +212 -0
- package/src/transmission/to-zashiki/render-params/schema/index.mjs +731 -0
- package/src/transmission/to-zashiki/transform-root-schema.mjs +485 -421
- package/src/transmission/to-zashiki/transform-schema.mjs +1988 -4663
@@ -1,3 +1,16 @@
|
|
1
|
+
/**
|
2
|
+
* @typedef {TransmissionTypes.ObjectLiteralType} ObjectLiteralType
|
3
|
+
* @typedef {TransmissionTypes.ObjectType} ObjectType
|
4
|
+
* @typedef {TransmissionTypes.ArrayLiteralType} ArrayLiteralType
|
5
|
+
* @typedef {TransmissionTypes.ArrayType} ArrayType
|
6
|
+
*
|
7
|
+
* @typedef {TransmissionTypes.ItemsType} ItemsType
|
8
|
+
*
|
9
|
+
* @typedef {TransmissionTypes.DocumentType} DocumentType
|
10
|
+
* @typedef {TransmissionTypes.SchemaType} SchemaType
|
11
|
+
* @typedef {TransmissionTypes.HashType} HashType
|
12
|
+
*/
|
13
|
+
|
1
14
|
import debug from 'debug'
|
2
15
|
|
3
16
|
import {
|
@@ -11,19 +24,27 @@ import {
|
|
11
24
|
hasOneOf,
|
12
25
|
getOneOf,
|
13
26
|
getUri,
|
14
|
-
|
27
|
+
transformToValue
|
15
28
|
} from '#transmission/transmission/common'
|
16
29
|
|
17
30
|
const log = debug('shinkansen-transmission/from-hash-to-document')
|
18
31
|
|
19
32
|
log('`shinkansen` is awake')
|
20
33
|
|
34
|
+
/**
|
35
|
+
* @param {unknown} v
|
36
|
+
* @returns {null}
|
37
|
+
*/
|
21
38
|
export function toNull (v) {
|
22
39
|
if (v === null || v === 'null') return null
|
23
40
|
|
24
41
|
throw new Error('Invalid `null`')
|
25
42
|
}
|
26
43
|
|
44
|
+
/**
|
45
|
+
* @param {unknown} v
|
46
|
+
* @returns {boolean}
|
47
|
+
*/
|
27
48
|
export function toBoolean (v) {
|
28
49
|
if (typeof v === 'boolean') return v
|
29
50
|
if (v === 'true') return true
|
@@ -32,6 +53,10 @@ export function toBoolean (v) {
|
|
32
53
|
throw new Error('Invalid `boolean`')
|
33
54
|
}
|
34
55
|
|
56
|
+
/**
|
57
|
+
* @param {unknown} v
|
58
|
+
* @returns {string}
|
59
|
+
*/
|
35
60
|
export function toString (v) {
|
36
61
|
if (typeof v === 'string') return v
|
37
62
|
if (typeof v === 'number') return String(v)
|
@@ -39,6 +64,10 @@ export function toString (v) {
|
|
39
64
|
return JSON.stringify(v)
|
40
65
|
}
|
41
66
|
|
67
|
+
/**
|
68
|
+
* @param {unknown} v
|
69
|
+
* @returns {number}
|
70
|
+
*/
|
42
71
|
export function toNumber (v) {
|
43
72
|
if (typeof v === 'number') return v
|
44
73
|
if (typeof v === 'boolean') return Number(v)
|
@@ -56,45 +85,111 @@ export function toNumber (v) {
|
|
56
85
|
throw new Error('Invalid `number`')
|
57
86
|
}
|
58
87
|
|
59
|
-
|
88
|
+
export { transformToValue }
|
89
|
+
|
90
|
+
/**
|
91
|
+
* @param {HashType} hash
|
92
|
+
* @param {string} parentUri
|
93
|
+
* @returns {(document: ObjectLiteralType | ObjectType, entry: [key: string, schema: SchemaType]) => ObjectLiteralType | ObjectType}
|
94
|
+
*/
|
95
|
+
function getReducePropertiesEntriesFor (hash, parentUri) {
|
96
|
+
/**
|
97
|
+
* @param {ObjectLiteralType | ObjectType} document
|
98
|
+
* @param {[key: string, schema: SchemaType]} entry
|
99
|
+
* @returns {ObjectLiteralType | ObjectType}
|
100
|
+
*/
|
101
|
+
return function reduce (document, [key, schema]) {
|
102
|
+
const schemaUri = getUri(parentUri, key)
|
60
103
|
|
61
|
-
|
62
|
-
transformValue
|
63
|
-
}
|
104
|
+
document[key] = transform(hash, schema, schemaUri, schemaUri)
|
64
105
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
*/
|
106
|
+
return document
|
107
|
+
}
|
108
|
+
}
|
69
109
|
|
70
|
-
|
71
|
-
|
110
|
+
/**
|
111
|
+
* @param {HashType} hash
|
112
|
+
* @param {SchemaType[]} items
|
113
|
+
* @param {string} parentUri
|
114
|
+
* @param {string} uri
|
115
|
+
* @returns {(document: ArrayLiteralType | ArrayType, key: string) => ArrayLiteralType | ArrayType}
|
116
|
+
*/
|
117
|
+
function getReduceItemsArrayFor (hash, items, parentUri, uri) {
|
118
|
+
return function reduce (document, key) {
|
119
|
+
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
120
|
+
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
121
|
+
|
122
|
+
if (!isNaN(i)) {
|
123
|
+
const schemaUri = getUri(parentUri, i)
|
124
|
+
|
125
|
+
document[i] = transform(hash, items[i], schemaUri, schemaUri) // items
|
126
|
+
}
|
72
127
|
|
73
|
-
|
74
|
-
|
128
|
+
return document
|
129
|
+
}
|
130
|
+
}
|
75
131
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
132
|
+
/**
|
133
|
+
* @param {HashType} hash
|
134
|
+
* @param {SchemaType} items
|
135
|
+
* @param {string} parentUri
|
136
|
+
* @param {string} uri
|
137
|
+
* @returns {(document: ArrayLiteralType | ArrayType, key: string) => ArrayLiteralType | ArrayType}
|
138
|
+
*/
|
139
|
+
function getReduceItemsObjectFor (hash, items, parentUri, uri) {
|
140
|
+
return function reduce (document, key) {
|
141
|
+
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
142
|
+
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
143
|
+
|
144
|
+
if (!isNaN(i)) {
|
145
|
+
const schemaUri = getUri(parentUri, i)
|
146
|
+
|
147
|
+
document[i] = transform(hash, items, schemaUri, schemaUri) // items
|
80
148
|
}
|
81
|
-
|
82
|
-
|
149
|
+
|
150
|
+
return document
|
83
151
|
}
|
152
|
+
}
|
84
153
|
|
154
|
+
/**
|
155
|
+
* @param {DocumentType} document
|
156
|
+
* @param {ItemsType} [array]
|
157
|
+
* @returns {DocumentType | undefined}
|
158
|
+
*/
|
159
|
+
export function transformToValueFor (document, array = []) {
|
85
160
|
/*
|
86
|
-
*
|
161
|
+
* log('transformToValueFor')
|
87
162
|
*/
|
88
|
-
|
163
|
+
|
164
|
+
const i = toNumber(document)
|
165
|
+
|
166
|
+
if (i in array) { // @ts-ignore
|
167
|
+
const v = array[i]
|
168
|
+
|
169
|
+
/*
|
170
|
+
* Return the document according to the schema `v` or, if `v` is a primitive, return that
|
171
|
+
*/
|
172
|
+
return transformToValue(v)
|
173
|
+
}
|
174
|
+
|
175
|
+
throw new Error('Invalid `document`')
|
176
|
+
|
177
|
+
// return document
|
89
178
|
}
|
90
179
|
|
180
|
+
/**
|
181
|
+
* @param {HashType} hash
|
182
|
+
* @param {ItemsType} [array]
|
183
|
+
* @param {string} [uri]
|
184
|
+
* @returns {string | number | object | boolean | null | []}
|
185
|
+
*/
|
91
186
|
export function getArrayFor (hash, array = [], uri = '#') {
|
92
187
|
/*
|
93
188
|
* log('getArrayFor')
|
94
189
|
*/
|
95
190
|
|
96
|
-
if (
|
97
|
-
const v =
|
191
|
+
if (uri in hash) {
|
192
|
+
const v = hash[uri]
|
98
193
|
const i = Number(v)
|
99
194
|
|
100
195
|
if (!isNaN(i)) return array[i]
|
@@ -103,7 +198,14 @@ export function getArrayFor (hash, array = [], uri = '#') {
|
|
103
198
|
return []
|
104
199
|
}
|
105
200
|
|
106
|
-
|
201
|
+
/**
|
202
|
+
* @param {HashType} hash
|
203
|
+
* @param {SchemaType} [schema]
|
204
|
+
* @param {string} parentUri
|
205
|
+
* @param {string} uri
|
206
|
+
* @returns {DocumentType | undefined}
|
207
|
+
*/
|
208
|
+
export function transformArrayFor (hash, { items } = {}, parentUri = '#', uri = getUri(parentUri)) {
|
107
209
|
/*
|
108
210
|
* log('transformArrayFor')
|
109
211
|
*/
|
@@ -119,7 +221,13 @@ export function transformArrayFor (hash, { items = null } = {}, parentUri = '#',
|
|
119
221
|
return []
|
120
222
|
}
|
121
223
|
|
122
|
-
|
224
|
+
/**
|
225
|
+
* @param {HashType} hash
|
226
|
+
* @param {SchemaType} [schema]
|
227
|
+
* @param {string} [parentUri]
|
228
|
+
* @returns {DocumentType}
|
229
|
+
*/
|
230
|
+
export function transformObjectFor (hash, { properties } = {}, parentUri = '#') { // }, uri = getUri(parentUri)) {
|
123
231
|
/*
|
124
232
|
* log('transformObjectFor')
|
125
233
|
*/
|
@@ -128,29 +236,30 @@ export function transformObjectFor (hash, { properties = null } = {}, parentUri
|
|
128
236
|
return (
|
129
237
|
Object
|
130
238
|
.entries(properties)
|
131
|
-
.reduce((
|
132
|
-
const schemaUri = getUri(parentUri, key)
|
133
|
-
|
134
|
-
accumulator[key] = transform(hash, schema, schemaUri, schemaUri)
|
135
|
-
|
136
|
-
return accumulator
|
137
|
-
}, {})
|
239
|
+
.reduce(getReducePropertiesEntriesFor(hash, parentUri), {})
|
138
240
|
)
|
139
241
|
}
|
140
242
|
|
141
243
|
return {}
|
142
244
|
}
|
143
245
|
|
246
|
+
/**
|
247
|
+
* @param {HashType} hash
|
248
|
+
* @param {SchemaType[]} [items]
|
249
|
+
* @param {string} parentUri
|
250
|
+
* @param {string} uri
|
251
|
+
* @returns {DocumentType}
|
252
|
+
*/
|
144
253
|
export function transformItemsArrayFor (hash, items = [], parentUri = '#', uri = getUri(parentUri)) {
|
145
254
|
/*
|
146
255
|
* log('transformItemsArrayFor')
|
147
256
|
*/
|
148
257
|
|
149
|
-
if (Reflect.has(hash, uri)) {
|
150
|
-
const document = Reflect.get(hash, uri)
|
258
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
259
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
151
260
|
|
152
261
|
if (isArray(document)) {
|
153
|
-
return transform(hash,
|
262
|
+
return items.map((item, i) => transform(hash, item, uri, getUri(uri, i)))
|
154
263
|
}
|
155
264
|
}
|
156
265
|
|
@@ -158,28 +267,24 @@ export function transformItemsArrayFor (hash, items = [], parentUri = '#', uri =
|
|
158
267
|
Object
|
159
268
|
.keys(hash)
|
160
269
|
.filter((key) => key.startsWith(uri))
|
161
|
-
.reduce((
|
162
|
-
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
163
|
-
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
164
|
-
|
165
|
-
if (!isNaN(i)) {
|
166
|
-
const schemaUri = getUri(parentUri, i)
|
167
|
-
|
168
|
-
accumulator[i] = transform(hash, items[i], schemaUri, schemaUri) // items[i]
|
169
|
-
}
|
170
|
-
|
171
|
-
return accumulator
|
172
|
-
}, [])
|
270
|
+
.reduce(getReduceItemsArrayFor(hash, items, parentUri, uri), [])
|
173
271
|
)
|
174
272
|
}
|
175
273
|
|
274
|
+
/**
|
275
|
+
* @param {HashType} hash
|
276
|
+
* @param {SchemaType} [items]
|
277
|
+
* @param {string} parentUri
|
278
|
+
* @param {string} uri
|
279
|
+
* @returns {DocumentType | undefined}
|
280
|
+
*/
|
176
281
|
export function transformItemsObjectFor (hash, items = {}, parentUri = '#', uri = getUri(parentUri)) {
|
177
282
|
/*
|
178
283
|
* log('transformItemsObjectFor')
|
179
284
|
*/
|
180
285
|
|
181
|
-
if (Reflect.has(hash, uri)) {
|
182
|
-
const document = Reflect.get(hash, uri)
|
286
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
287
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
183
288
|
|
184
289
|
if (isArray(document)) {
|
185
290
|
return transform(hash, items, uri, uri)
|
@@ -190,101 +295,99 @@ export function transformItemsObjectFor (hash, items = {}, parentUri = '#', uri
|
|
190
295
|
Object
|
191
296
|
.keys(hash)
|
192
297
|
.filter((key) => key.startsWith(uri))
|
193
|
-
.reduce((
|
194
|
-
const v = uri.endsWith('/') ? key.slice(uri.length) : key.slice(uri.length + 1)
|
195
|
-
const i = v ? Number(v.includes('/') ? v.slice(0, v.indexOf('/')) : v) : NaN
|
196
|
-
|
197
|
-
if (!isNaN(i)) {
|
198
|
-
const schemaUri = getUri(parentUri, i)
|
199
|
-
|
200
|
-
accumulator[i] = transform(hash, items, schemaUri, schemaUri) // items
|
201
|
-
}
|
202
|
-
|
203
|
-
return accumulator
|
204
|
-
}, [])
|
298
|
+
.reduce(getReduceItemsObjectFor(hash, items, parentUri, uri), [])
|
205
299
|
)
|
206
300
|
}
|
207
301
|
|
302
|
+
/**
|
303
|
+
* @param {HashType} hash
|
304
|
+
* @param {SchemaType} schema
|
305
|
+
* @param {string} parentUri
|
306
|
+
* @param {string} uri
|
307
|
+
* @returns {DocumentType | undefined}
|
308
|
+
*/
|
208
309
|
export function transformNull (hash, schema, parentUri, uri) {
|
209
310
|
/*
|
210
311
|
* log('transformNull')
|
211
312
|
*/
|
212
313
|
|
213
|
-
if (Reflect.has(hash, uri)) {
|
214
|
-
const document = Reflect.get(hash, uri)
|
314
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
315
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
215
316
|
|
216
317
|
if (hasEnum(schema)) {
|
217
318
|
const array = getEnum(schema)
|
218
319
|
|
219
|
-
if (isArray(document)) return document.map((v) =>
|
220
|
-
return
|
320
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
321
|
+
return transformToValueFor(document, array)
|
221
322
|
} else {
|
222
323
|
if (hasAnyOf(schema)) {
|
223
324
|
const array = getAnyOf(schema)
|
224
325
|
|
225
|
-
if (isArray(document)) return document.map((v) =>
|
226
|
-
return
|
326
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
327
|
+
return transformToValueFor(document, array)
|
227
328
|
} else {
|
228
329
|
if (hasOneOf(schema)) {
|
229
330
|
const array = getOneOf(schema)
|
230
331
|
|
231
|
-
if (isArray(document)) return document.map((v) =>
|
232
|
-
return
|
332
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
333
|
+
return transformToValueFor(document, array)
|
233
334
|
}
|
234
335
|
}
|
235
336
|
}
|
236
337
|
|
237
|
-
|
238
|
-
|
239
|
-
return toNull(document)
|
240
|
-
} catch (e) {
|
241
|
-
handleError(e)
|
242
|
-
}
|
243
|
-
|
244
|
-
return document
|
338
|
+
if (isArray(document)) return document.map(toNull)
|
339
|
+
return toNull(document)
|
245
340
|
}
|
246
341
|
}
|
247
342
|
|
343
|
+
/**
|
344
|
+
* @param {HashType} hash
|
345
|
+
* @param {SchemaType} schema
|
346
|
+
* @param {string} parentUri
|
347
|
+
* @param {string} uri
|
348
|
+
* @returns {DocumentType | undefined}
|
349
|
+
*/
|
248
350
|
export function transformBoolean (hash, schema, parentUri, uri) {
|
249
351
|
/*
|
250
352
|
* log('transformBoolean')
|
251
353
|
*/
|
252
354
|
|
253
|
-
if (Reflect.has(hash, uri)) {
|
254
|
-
const document = Reflect.get(hash, uri)
|
355
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
356
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
255
357
|
|
256
358
|
if (hasEnum(schema)) {
|
257
359
|
const array = getEnum(schema)
|
258
360
|
|
259
|
-
if (isArray(document)) return document.map((v) =>
|
260
|
-
return
|
361
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
362
|
+
return transformToValueFor(document, array)
|
261
363
|
} else {
|
262
364
|
if (hasAnyOf(schema)) {
|
263
365
|
const array = getAnyOf(schema)
|
264
366
|
|
265
|
-
if (isArray(document)) return document.map((v) =>
|
266
|
-
return
|
367
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
368
|
+
return transformToValueFor(document, array)
|
267
369
|
} else {
|
268
370
|
if (hasOneOf(schema)) {
|
269
371
|
const array = getOneOf(schema)
|
270
372
|
|
271
|
-
if (isArray(document)) return document.map((v) =>
|
272
|
-
return
|
373
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
374
|
+
return transformToValueFor(document, array)
|
273
375
|
}
|
274
376
|
}
|
275
377
|
}
|
276
378
|
|
277
|
-
|
278
|
-
|
279
|
-
return toBoolean(document)
|
280
|
-
} catch (e) {
|
281
|
-
handleError(e)
|
282
|
-
}
|
283
|
-
|
284
|
-
return document
|
379
|
+
if (isArray(document)) return document.map(toBoolean)
|
380
|
+
return toBoolean(document)
|
285
381
|
}
|
286
382
|
}
|
287
383
|
|
384
|
+
/**
|
385
|
+
* @param {HashType} hash
|
386
|
+
* @param {SchemaType} schema
|
387
|
+
* @param {string} parentUri
|
388
|
+
* @param {string} uri
|
389
|
+
* @returns {DocumentType | undefined}
|
390
|
+
*/
|
288
391
|
export function transformObject (hash, schema, parentUri, uri) {
|
289
392
|
/*
|
290
393
|
* log('transformObject')
|
@@ -308,9 +411,16 @@ export function transformObject (hash, schema, parentUri, uri) {
|
|
308
411
|
}
|
309
412
|
}
|
310
413
|
|
311
|
-
return transformObjectFor(hash, schema, parentUri, uri)
|
414
|
+
return transformObjectFor(hash, schema, parentUri) // , uri)
|
312
415
|
}
|
313
416
|
|
417
|
+
/**
|
418
|
+
* @param {HashType} hash
|
419
|
+
* @param {SchemaType} schema
|
420
|
+
* @param {string} parentUri
|
421
|
+
* @param {string} uri
|
422
|
+
* @returns {DocumentType | undefined}
|
423
|
+
*/
|
314
424
|
export function transformArray (hash, schema, parentUri, uri) {
|
315
425
|
/*
|
316
426
|
* log('transformArray')
|
@@ -337,112 +447,124 @@ export function transformArray (hash, schema, parentUri, uri) {
|
|
337
447
|
return transformArrayFor(hash, schema, parentUri, uri)
|
338
448
|
}
|
339
449
|
|
450
|
+
/**
|
451
|
+
* @param {HashType} hash
|
452
|
+
* @param {SchemaType} schema
|
453
|
+
* @param {string} parentUri
|
454
|
+
* @param {string} uri
|
455
|
+
* @returns {DocumentType | undefined}
|
456
|
+
*/
|
340
457
|
export function transformNumber (hash, schema, parentUri, uri) {
|
341
458
|
/*
|
342
459
|
* log('transformNumber')
|
343
460
|
*/
|
344
461
|
|
345
|
-
if (Reflect.has(hash, uri)) {
|
346
|
-
const document = Reflect.get(hash, uri)
|
462
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
463
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
347
464
|
|
348
465
|
if (hasEnum(schema)) {
|
349
466
|
const array = getEnum(schema)
|
350
467
|
|
351
|
-
if (isArray(document)) return document.map((v) =>
|
352
|
-
return
|
468
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
469
|
+
return transformToValueFor(document, array)
|
353
470
|
} else {
|
354
471
|
if (hasAnyOf(schema)) {
|
355
472
|
const array = getAnyOf(schema)
|
356
473
|
|
357
|
-
if (isArray(document)) return document.map((v) =>
|
358
|
-
return
|
474
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
475
|
+
return transformToValueFor(document, array)
|
359
476
|
} else {
|
360
477
|
if (hasOneOf(schema)) {
|
361
478
|
const array = getOneOf(schema)
|
362
479
|
|
363
|
-
if (isArray(document)) return document.map((v) =>
|
364
|
-
return
|
480
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
481
|
+
return transformToValueFor(document, array)
|
365
482
|
}
|
366
483
|
}
|
367
484
|
}
|
368
485
|
|
369
|
-
|
370
|
-
|
371
|
-
return toNumber(document)
|
372
|
-
} catch (e) {
|
373
|
-
handleError(e)
|
374
|
-
}
|
375
|
-
|
376
|
-
return document
|
486
|
+
if (isArray(document)) return document.map(toNumber)
|
487
|
+
return toNumber(document)
|
377
488
|
}
|
378
489
|
}
|
379
490
|
|
491
|
+
/**
|
492
|
+
* @param {HashType} hash
|
493
|
+
* @param {SchemaType} schema
|
494
|
+
* @param {string} parentUri
|
495
|
+
* @param {string} uri
|
496
|
+
* @returns {DocumentType | undefined}
|
497
|
+
*/
|
380
498
|
export function transformString (hash, schema, parentUri, uri) {
|
381
499
|
/*
|
382
500
|
* log('transformString')
|
383
501
|
*/
|
384
502
|
|
385
|
-
if (Reflect.has(hash, uri)) {
|
386
|
-
const document = Reflect.get(hash, uri)
|
503
|
+
if (uri in hash) { // Reflect.has(hash, uri)) {
|
504
|
+
const document = hash[uri] // Reflect.get(hash, uri)
|
387
505
|
|
388
506
|
if (hasEnum(schema)) {
|
389
507
|
const array = getEnum(schema)
|
390
508
|
|
391
|
-
if (isArray(document)) return document.map((v) =>
|
392
|
-
return
|
509
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
510
|
+
return transformToValueFor(document, array)
|
393
511
|
} else {
|
394
512
|
if (hasAnyOf(schema)) {
|
395
513
|
const array = getAnyOf(schema)
|
396
514
|
|
397
|
-
if (isArray(document)) return document.map((v) =>
|
398
|
-
return
|
515
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
516
|
+
return transformToValueFor(document, array)
|
399
517
|
} else {
|
400
518
|
if (hasOneOf(schema)) {
|
401
519
|
const array = getOneOf(schema)
|
402
520
|
|
403
|
-
if (isArray(document)) return document.map((v) =>
|
404
|
-
return
|
521
|
+
if (isArray(document)) return document.map((v) => transformToValueFor(v, array))
|
522
|
+
return transformToValueFor(document, array)
|
405
523
|
}
|
406
524
|
}
|
407
525
|
}
|
408
526
|
|
409
|
-
|
410
|
-
|
411
|
-
return toString(document)
|
412
|
-
} catch (e) {
|
413
|
-
handleError(e)
|
414
|
-
}
|
415
|
-
|
416
|
-
return document
|
527
|
+
if (isArray(document)) return document.map(toString)
|
528
|
+
return toString(document)
|
417
529
|
}
|
418
530
|
}
|
419
531
|
|
420
|
-
|
532
|
+
/**
|
533
|
+
* Hash can be `undefined`
|
534
|
+
*
|
535
|
+
* @param {HashType} [hash]
|
536
|
+
* @param {SchemaType} [schema]
|
537
|
+
* @param {string} [parentUri]
|
538
|
+
* @param {string} [uri]
|
539
|
+
* @returns {DocumentType | undefined}
|
540
|
+
*/
|
541
|
+
export default function transform (hash = {}, schema = {}, parentUri = '#', uri = getUri(parentUri)) {
|
421
542
|
log('fromHashToDocument')
|
422
543
|
|
423
|
-
const {
|
544
|
+
const {
|
545
|
+
type
|
546
|
+
} = schema
|
424
547
|
|
425
548
|
// https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1
|
426
549
|
switch (type) {
|
427
|
-
case '
|
428
|
-
return
|
429
|
-
|
430
|
-
case 'boolean':
|
431
|
-
return transformBoolean(hash, rootSchema, parentUri, uri)
|
550
|
+
case 'string':
|
551
|
+
return transformString(hash, schema, parentUri, uri)
|
432
552
|
|
433
|
-
case '
|
434
|
-
return
|
553
|
+
case 'number':
|
554
|
+
return transformNumber(hash, schema, parentUri, uri)
|
435
555
|
|
436
556
|
case 'array':
|
437
|
-
return transformArray(hash,
|
557
|
+
return transformArray(hash, schema, parentUri, uri)
|
438
558
|
|
439
|
-
case '
|
440
|
-
return
|
559
|
+
case 'object':
|
560
|
+
return transformObject(hash, schema, parentUri, uri)
|
441
561
|
|
442
|
-
case '
|
443
|
-
return
|
562
|
+
case 'boolean':
|
563
|
+
return transformBoolean(hash, schema, parentUri, uri)
|
444
564
|
|
445
|
-
|
446
|
-
|
565
|
+
case 'null':
|
566
|
+
return transformNull(hash, schema, parentUri, uri)
|
447
567
|
}
|
568
|
+
|
569
|
+
throw new Error('Schema does not conform to Instance Data Model, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1')
|
448
570
|
}
|
@@ -1,3 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* @typedef {TransmissionTypes.HashType} HashType
|
3
|
+
* @typedef {TransmissionTypes.SchemaType} SchemaType
|
4
|
+
* @typedef {TransmissionTypes.ParamsType} ParamsType
|
5
|
+
* @typedef {TransmissionTypes.DocumentType} DocumentType
|
6
|
+
*/
|
7
|
+
|
1
8
|
import debug from 'debug'
|
2
9
|
|
3
10
|
import {
|
@@ -12,18 +19,28 @@ const log = debug('shinkansen-transmission/from-hash-to-document/null')
|
|
12
19
|
|
13
20
|
log('`shinkansen` is awake')
|
14
21
|
|
15
|
-
|
22
|
+
/**
|
23
|
+
* Hash can be `undefined`
|
24
|
+
*
|
25
|
+
* @param {HashType} [hash]
|
26
|
+
* @param {SchemaType} [schema]
|
27
|
+
* @param {ParamsType} [params]
|
28
|
+
* @returns {DocumentType | undefined}
|
29
|
+
*/
|
30
|
+
export default function transformNullSchema (hash = {}, schema = {}, params = {}) {
|
16
31
|
log('transformNullSchema')
|
17
32
|
|
18
|
-
const {
|
33
|
+
const {
|
34
|
+
type
|
35
|
+
} = schema
|
19
36
|
|
20
37
|
// https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1
|
21
38
|
if (type === 'null') {
|
22
39
|
const {
|
23
|
-
uri: parentUri
|
40
|
+
uri: parentUri = '#'
|
24
41
|
} = params
|
25
42
|
|
26
|
-
return transformNull(
|
43
|
+
return transformNull(hash, schema, parentUri, getUri(parentUri))
|
27
44
|
}
|
28
45
|
|
29
46
|
throw new Error('Schema does not conform to Instance Data Model, https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1')
|