shinkansen-transmission 2.3.5 → 2.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 (34) hide show
  1. package/index.d.mts +22 -22
  2. package/package.json +2 -2
  3. package/src/transmission/common/index.d.mts +30 -38
  4. package/src/transmission/common/index.mjs +76 -196
  5. package/src/transmission/common/transform/index.d.mts +18 -0
  6. package/src/transmission/common/transform/index.mjs +128 -0
  7. package/src/transmission/from-document-to-hash/array/index.d.mts +1 -0
  8. package/src/transmission/from-document-to-hash/array/index.mjs +1 -46
  9. package/src/transmission/from-document-to-hash/boolean/index.d.mts +5 -0
  10. package/src/transmission/from-document-to-hash/boolean/index.mjs +7 -7
  11. package/src/transmission/from-document-to-hash/index.d.mts +10 -3
  12. package/src/transmission/from-document-to-hash/index.mjs +121 -60
  13. package/src/transmission/from-document-to-hash/null/index.d.mts +5 -0
  14. package/src/transmission/from-document-to-hash/null/index.mjs +7 -7
  15. package/src/transmission/from-document-to-hash/number/index.d.mts +5 -0
  16. package/src/transmission/from-document-to-hash/number/index.mjs +7 -7
  17. package/src/transmission/from-document-to-hash/object/index.d.mts +1 -0
  18. package/src/transmission/from-document-to-hash/object/index.mjs +1 -46
  19. package/src/transmission/from-document-to-hash/string/index.d.mts +5 -0
  20. package/src/transmission/from-document-to-hash/string/index.mjs +8 -8
  21. package/src/transmission/from-hash-to-document/array/index.d.mts +4 -0
  22. package/src/transmission/from-hash-to-document/array/index.mjs +4 -42
  23. package/src/transmission/from-hash-to-document/boolean/index.d.mts +8 -0
  24. package/src/transmission/from-hash-to-document/boolean/index.mjs +84 -3
  25. package/src/transmission/from-hash-to-document/index.d.mts +28 -21
  26. package/src/transmission/from-hash-to-document/index.mjs +184 -352
  27. package/src/transmission/from-hash-to-document/null/index.d.mts +8 -0
  28. package/src/transmission/from-hash-to-document/null/index.mjs +84 -3
  29. package/src/transmission/from-hash-to-document/number/index.d.mts +8 -0
  30. package/src/transmission/from-hash-to-document/number/index.mjs +84 -3
  31. package/src/transmission/from-hash-to-document/object/index.d.mts +4 -0
  32. package/src/transmission/from-hash-to-document/object/index.mjs +4 -42
  33. package/src/transmission/from-hash-to-document/string/index.d.mts +8 -0
  34. package/src/transmission/from-hash-to-document/string/index.mjs +85 -4
@@ -1,52 +1,7 @@
1
- /**
2
- * @typedef {TransmissionTypes.DocumentType} DocumentType
3
- * @typedef {TransmissionTypes.SchemaType} SchemaType
4
- * @typedef {TransmissionTypes.HashType} HashType
5
- */
6
-
7
1
  import debug from 'debug'
8
2
 
9
- import {
10
- toString,
11
- isObject,
12
- getUri
13
- } from '#transmission/transmission/common'
14
-
15
- import {
16
- transformObject
17
- } from '#transmission/transmission/from-document-to-hash'
18
-
19
3
  const log = debug('shinkansen-transmission/from-document-to-hash/object')
20
4
 
21
5
  log('`shinkansen` is awake')
22
6
 
23
- /**
24
- * Document can be `undefined`
25
- *
26
- * @param {DocumentType} [document]
27
- * @param {SchemaType} [schema]
28
- * @param {HashType} [hash]
29
- * @param {string} [parentUri]
30
- * @param {string} [uri]
31
- * @returns {HashType}
32
- */
33
- export default function transformObjectSchema (document, schema = {}, hash = {}, parentUri = '#', uri = getUri(parentUri)) {
34
- log('transformObjectSchema')
35
-
36
- /*
37
- * Is `document` an object?
38
- */
39
- if (isObject(document)) {
40
- /*
41
- * Yes, `document` is an object
42
- */
43
- return transformObject(document, schema, hash, parentUri, uri)
44
- }
45
-
46
- /*
47
- * The hash should contain only strings
48
- */
49
- hash[uri] = toString(document)
50
-
51
- return hash
52
- }
7
+ export { transformObjectSchema as default } from '#transmission/transmission/from-document-to-hash'
@@ -0,0 +1,5 @@
1
+ type DocumentType = TransmissionTypes.DocumentType
2
+ type SchemaType = TransmissionTypes.SchemaType
3
+ type HashType = TransmissionTypes.HashType
4
+
5
+ export default function transformStringSchema (document?: DocumentType, schema?: SchemaType, hash?: HashType, parentUri?: string, uri?: string): HashType
@@ -15,7 +15,7 @@ import {
15
15
  hasOneOf,
16
16
  getOneOf,
17
17
  getUri,
18
- getIndexByValue
18
+ transformIndexToValueByFindValue
19
19
  } from '#transmission/transmission/common'
20
20
 
21
21
  const log = debug('shinkansen-transmission/from-document-to-hash/string')
@@ -40,28 +40,28 @@ export default function transformStringSchema (document, schema = {}, hash = {},
40
40
  * https://json-schema.org/draft/2019-09/json-schema-validation#rfc.section.6.1.2
41
41
  */
42
42
  const array = getEnum(schema)
43
- const index = getIndexByValue(array, document)
43
+ const value = transformIndexToValueByFindValue(array, document)
44
44
 
45
45
  /**
46
- * `document` is the string representation of an index to an item in the `enum`
46
+ * `document` is the string representation of an value to an item in the `enum`
47
47
  */
48
- hash[uri] = String(index)
48
+ hash[uri] = value
49
49
 
50
50
  return hash
51
51
  } else {
52
52
  if (hasAnyOf(schema)) {
53
53
  const array = getAnyOf(schema)
54
- const index = getIndexByValue(array, document)
54
+ const value = transformIndexToValueByFindValue(array, document)
55
55
 
56
- hash[uri] = String(index)
56
+ hash[uri] = value
57
57
 
58
58
  return hash
59
59
  } else {
60
60
  if (hasOneOf(schema)) {
61
61
  const array = getOneOf(schema)
62
- const index = getIndexByValue(array, document)
62
+ const value = transformIndexToValueByFindValue(array, document)
63
63
 
64
- hash[uri] = String(index)
64
+ hash[uri] = value
65
65
 
66
66
  return hash
67
67
  }
@@ -0,0 +1,4 @@
1
+ export {
2
+ transformArray,
3
+ transformArraySchema as default
4
+ } from '#transmission/transmission/from-hash-to-document'
@@ -1,48 +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
-
8
1
  import debug from 'debug'
9
2
 
10
- import {
11
- getUri
12
- } from '#transmission/transmission/common'
13
-
14
- import {
15
- transformArray
16
- } from '#transmission/transmission/from-hash-to-document'
17
-
18
3
  const log = debug('shinkansen-transmission/from-hash-to-document/array')
19
4
 
20
5
  log('`shinkansen` is awake')
21
6
 
22
- /**
23
- * Hash can be `undefined`
24
- *
25
- * @param {HashType} [hash]
26
- * @param {SchemaType} [schema]
27
- * @param {ParamsType} [params]
28
- * @returns {DocumentType}
29
- */
30
- export default function transformArraySchema (hash = {}, schema = {}, params = {}) {
31
- log('transformArraySchema')
32
-
33
- const {
34
- type
35
- } = schema
36
-
37
- // https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.4.2.1
38
- if (type === 'array') {
39
- const {
40
- uri: parentUri = '#',
41
- index: arrayIndex // number | undefined
42
- } = params
43
-
44
- return transformArray(hash, schema, parentUri, getUri(parentUri, arrayIndex))
45
- }
46
-
47
- 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')
48
- }
7
+ export {
8
+ transformArray,
9
+ transformArraySchema as default
10
+ } from '#transmission/transmission/from-hash-to-document'
@@ -0,0 +1,8 @@
1
+ export type SchemaType = TransmissionTypes.SchemaType
2
+ export type ParamsType = TransmissionTypes.ParamsType
3
+ export type DocumentType = TransmissionTypes.DocumentType
4
+ export type HashType = TransmissionTypes.HashType
5
+
6
+ export function transformBoolean (hash: HashType, schema: SchemaType, parentUri: string, uri: string): boolean | undefined
7
+
8
+ export default function transformBooleanSchema (hash?: HashType, schema?: SchemaType, params?: ParamsType): DocumentType | undefined
@@ -8,19 +8,100 @@
8
8
  import debug from 'debug'
9
9
 
10
10
  import {
11
+ isArray,
12
+ hasEnum,
13
+ getEnum,
14
+ hasAnyOf,
15
+ getAnyOf,
16
+ hasOneOf,
17
+ getOneOf,
18
+ transformToValue,
11
19
  getUri
12
20
  } from '#transmission/transmission/common'
13
21
 
14
22
  import {
15
- transformBoolean
16
- } from '#transmission/transmission/from-hash-to-document'
23
+ fromDocumentToArray,
24
+ mapToValue,
25
+ toBoolean
26
+ } from '#transmission/transmission/common/transform'
17
27
 
18
28
  const log = debug('shinkansen-transmission/from-hash-to-document/boolean')
19
29
 
20
30
  log('`shinkansen` is awake')
21
31
 
22
32
  /**
23
- * Hash can be `undefined`
33
+ * @overload
34
+ * @param {unknown} error
35
+ * @returns {void}
36
+ *
37
+ * @param {{ message?: string }} error
38
+ * @returns {void}
39
+ */
40
+ function handleError ({ message = 'No error message defined' }) { log(message) }
41
+
42
+ /**
43
+ * @param {HashType} hash
44
+ * @param {SchemaType} schema
45
+ * @param {string} parentUri
46
+ * @param {string} uri
47
+ * @returns {DocumentType | undefined}
48
+ */
49
+ export function transformBoolean (hash, schema, parentUri, uri) {
50
+ /*
51
+ * log('transformBoolean')
52
+ */
53
+
54
+ if (uri in hash) { // Reflect.has(hash, uri)) {
55
+ const document = hash[uri] // Reflect.get(hash, uri)
56
+
57
+ if (hasEnum(schema)) {
58
+ const array = getEnum(schema)
59
+
60
+ if (isArray(document)) return document.map(mapToValue(array))
61
+ return (
62
+ transformToValue(
63
+ fromDocumentToArray(document, array)
64
+ )
65
+ )
66
+ } else {
67
+ if (hasAnyOf(schema)) {
68
+ const array = getAnyOf(schema)
69
+
70
+ if (isArray(document)) return document.map(mapToValue(array))
71
+ return (
72
+ transformToValue(
73
+ fromDocumentToArray(document, array)
74
+ )
75
+ )
76
+ } else {
77
+ if (hasOneOf(schema)) {
78
+ const array = getOneOf(schema)
79
+
80
+ if (isArray(document)) return document.map(mapToValue(array))
81
+ return (
82
+ transformToValue(
83
+ fromDocumentToArray(document, array)
84
+ )
85
+ )
86
+ }
87
+ }
88
+ }
89
+
90
+ try {
91
+ if (isArray(document)) return document.map(toBoolean)
92
+ return (
93
+ toBoolean(document)
94
+ )
95
+ } catch (e) {
96
+ handleError(e)
97
+ }
98
+
99
+ return document
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Hash can be `undefined`
24
105
  *
25
106
  * @param {HashType} [hash]
26
107
  * @param {SchemaType} [schema]
@@ -4,29 +4,36 @@ export type ArrayLiteralType = TransmissionTypes.ArrayLiteralType
4
4
  export type ArrayType = TransmissionTypes.ArrayType
5
5
 
6
6
  export type SchemaType = TransmissionTypes.SchemaType
7
+ export type ParamsType = TransmissionTypes.ParamsType
7
8
  export type DocumentType = TransmissionTypes.DocumentType
8
9
  export type HashType = TransmissionTypes.HashType
9
10
 
10
- export function toNull (v: unknown): null
11
- export function toBoolean (v: unknown): boolean
12
- export function toString (v: unknown): string
13
- export function toNumber (v: unknown): number
14
-
15
- export function transformValueFor (document: DocumentType, array?: ArrayLiteralType | ArrayType): DocumentType
16
-
17
- export function getArrayFor (hash: HashType, array?: ArrayLiteralType | ArrayType, uri?: string): ArrayLiteralType | ArrayType
18
-
19
11
  export function transformArrayFor (hash: HashType, schema: SchemaType, parentUri?: string, uri?: string): ArrayLiteralType | ArrayType
20
- export function transformObjectFor (hash: HashType, schema: SchemaType, parentUri?: string, uri?: string): ObjectLiteralType | ObjectType
12
+ export function transformArray (hash: HashType, schema: SchemaType, parentUri: string, uri: string): DocumentType | undefined
13
+ export function transformArraySchema (hash?: HashType, schema?: SchemaType, params?: ParamsType): DocumentType | undefined
21
14
 
22
- export function transformItemsArrayFor (hash: HashType, items?: ArrayLiteralType | ArrayType, parentUri?: string, uri?: string): ArrayLiteralType | ArrayType | undefined
23
- export function transformItemsObjectFor (hash: HashType, items?: ObjectLiteralType | ObjectType, parentUri?: string, uri?: string): ArrayLiteralType | ArrayType | undefined
24
-
25
- export function transformNull (hash: HashType, schema: SchemaType, parentUri: string, uri: string): null | undefined
26
- export function transformBoolean (hash: HashType, schema: SchemaType, parentUri: string, uri: string): boolean | undefined
27
- export function transformObject (hash: HashType, schema: SchemaType, parentUri: string, uri: string): ObjectLiteralType | ObjectType
28
- export function transformArray (hash: HashType, schema: SchemaType, parentUri: string, uri: string): ArrayLiteralType | ArrayType
29
- export function transformNumber (hash: HashType, schema: SchemaType, parentUri: string, uri: string): number | undefined
30
- export function transformString (hash: HashType, schema: SchemaType, parentUri: string, uri: string): string | undefined
31
-
32
- export default function transform (hash?: HashType, rootSchema?: SchemaType, parentUri?: string, uri?: string): DocumentType | undefined
15
+ export function transformObjectFor (hash: HashType, schema: SchemaType, parentUri?: string, uri?: string): ObjectLiteralType | ObjectType
16
+ export function transformObject (hash: HashType, schema: SchemaType, parentUri: string, uri: string): DocumentType | undefined
17
+ export function transformObjectSchema (hash?: HashType, schema?: SchemaType, params?: ParamsType): DocumentType | undefined
18
+
19
+ export {
20
+ transformString,
21
+ default as transformStringSchema
22
+ } from './string/index.mjs'
23
+
24
+ export {
25
+ transformNumber,
26
+ default as transformNumberSchema
27
+ } from './number/index.mjs'
28
+
29
+ export {
30
+ transformBoolean,
31
+ default as transformBooleanSchema
32
+ } from './boolean/index.mjs'
33
+
34
+ export {
35
+ transformNull,
36
+ default as transformNullSchema
37
+ } from './null/index.mjs'
38
+
39
+ export default function fromHashToDocument (hash?: HashType, rootSchema?: SchemaType, parentUri?: string, uri?: string): DocumentType | undefined