@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
@@ -0,0 +1,146 @@
1
+ import { flattenTypeDefTo } from 'transformers/flatteners/flatten_type_def_to'
2
+ import {
3
+ booleanType,
4
+ list,
5
+ nullType,
6
+ numberType,
7
+ object,
8
+ record,
9
+ union,
10
+ } from 'types/builders'
11
+ import {
12
+ type TypeDef,
13
+ TypeDefType,
14
+ } from 'types/definitions'
15
+ import {
16
+ type Mock,
17
+ vi,
18
+ } from 'vitest'
19
+
20
+ describe('flattenTypeDefTo', function () {
21
+ let toTypeDefType: Mock<(typeDef: TypeDef) => number>
22
+ let flattened: Record<string, TypeDefType>
23
+
24
+ beforeEach(function () {
25
+ toTypeDefType = vi.fn(function (typeDef: TypeDef) {
26
+ return typeDef.type
27
+ })
28
+ })
29
+
30
+ describe('literal', function () {
31
+ beforeEach(function () {
32
+ flattened = flattenTypeDefTo(numberType, toTypeDefType)
33
+ })
34
+
35
+ it('equals expected type', function () {
36
+ expect(flattened).toEqual({
37
+ $: TypeDefType.Literal,
38
+ })
39
+ })
40
+
41
+ it('calls the mapper function', function () {
42
+ expect(toTypeDefType).toHaveBeenCalledTimes(1)
43
+ })
44
+ })
45
+
46
+ describe('list', function () {
47
+ const type = list(numberType)
48
+ beforeEach(function () {
49
+ flattened = flattenTypeDefTo(type, toTypeDefType)
50
+ })
51
+
52
+ it('equals expected type', function () {
53
+ expect(flattened).toEqual({
54
+ $: TypeDefType.List,
55
+ '$.*': TypeDefType.Literal,
56
+ })
57
+ })
58
+
59
+ it('calls the mapper function', function () {
60
+ expect(toTypeDefType).toHaveBeenCalledTimes(2)
61
+ })
62
+ })
63
+
64
+ describe('record', function () {
65
+ const type = record<typeof numberType, 'a' | 'b'>(numberType)
66
+ beforeEach(function () {
67
+ flattened = flattenTypeDefTo(type, toTypeDefType)
68
+ })
69
+
70
+ it('equals expected type', function () {
71
+ expect(
72
+ flattened,
73
+ ).toEqual({
74
+ $: TypeDefType.Record,
75
+ '$.*': TypeDefType.Literal,
76
+ })
77
+ })
78
+
79
+ it('calls the mapper function', function () {
80
+ expect(toTypeDefType).toHaveBeenCalledTimes(2)
81
+ })
82
+ })
83
+
84
+ describe('object', function () {
85
+ const type = object().set('a', numberType).set('b', list(booleanType))
86
+ beforeEach(function () {
87
+ flattened = flattenTypeDefTo(type, toTypeDefType)
88
+ })
89
+
90
+ it('equals expected type', function () {
91
+ expect(flattened).toEqual({
92
+ $: TypeDefType.Object,
93
+ '$.a': TypeDefType.Literal,
94
+ '$.b': TypeDefType.List,
95
+ '$.b.*': TypeDefType.Literal,
96
+ })
97
+ })
98
+
99
+ it('calls the mapper function', function () {
100
+ expect(toTypeDefType).toHaveBeenCalledTimes(4)
101
+ })
102
+ })
103
+
104
+ describe('union', function () {
105
+ describe('non-discriminated', function () {
106
+ const type = union()
107
+ .add('a', nullType)
108
+ .add('b', booleanType)
109
+ .add('c', numberType)
110
+ beforeEach(function () {
111
+ flattened = flattenTypeDefTo(type, toTypeDefType)
112
+ })
113
+
114
+ it('equals expected type', function () {
115
+ expect(flattened).toEqual({
116
+ $: TypeDefType.Union,
117
+ })
118
+ })
119
+
120
+ it('calls the mapper function', function () {
121
+ expect(toTypeDefType).toHaveBeenCalledTimes(1)
122
+ })
123
+ })
124
+
125
+ describe('discriminated', function () {
126
+ const type = union('d')
127
+ .add('a', object().set('a', booleanType))
128
+ .add('b', object().set('b', numberType))
129
+ beforeEach(function () {
130
+ flattened = flattenTypeDefTo(type, toTypeDefType)
131
+ })
132
+
133
+ it('equals expected type', function () {
134
+ expect(flattened).toEqual({
135
+ $: TypeDefType.Union,
136
+ '$.a:a': TypeDefType.Literal,
137
+ '$.b:b': TypeDefType.Literal,
138
+ })
139
+ })
140
+
141
+ it('calls the mapper function', function () {
142
+ expect(toTypeDefType).toHaveBeenCalledTimes(3)
143
+ })
144
+ })
145
+ })
146
+ })
@@ -0,0 +1,26 @@
1
+ import { flattenTypeDefsOf } from 'transformers/flatteners/flatten_type_defs_of'
2
+ import {
3
+ booleanType,
4
+ list,
5
+ numberType,
6
+ object,
7
+ } from 'types/builders'
8
+
9
+ describe('flattenTypeDefsOf', function () {
10
+ it('flattens', function () {
11
+ const listTypeDef = list(numberType)
12
+ const structTypeDef = object()
13
+ .set('a', listTypeDef)
14
+ .set('b', booleanType)
15
+ const flattened = flattenTypeDefsOf(
16
+ structTypeDef,
17
+ )
18
+
19
+ expect(flattened).toEqual({
20
+ $: structTypeDef.narrow,
21
+ '$.a': listTypeDef.narrow,
22
+ '$.a.*': numberType.narrow,
23
+ '$.b': booleanType.narrow,
24
+ })
25
+ })
26
+ })
@@ -0,0 +1,452 @@
1
+ import {
2
+ type AnyValueType,
3
+ flattenValueTypeTo,
4
+ type Mapper,
5
+ type Setter,
6
+ } from 'transformers/flatteners/flatten_value_type_to'
7
+ import { type SimplifyDeep } from 'type-fest'
8
+ import {
9
+ booleanType,
10
+ list,
11
+ literal,
12
+ nullType,
13
+ numberType,
14
+ object,
15
+ record,
16
+ union,
17
+ } from 'types/builders'
18
+ import {
19
+ type Type,
20
+ type TypeDef,
21
+ } from 'types/definitions'
22
+ import { type FlattenedTypeDefsOf } from 'types/flattened_type_defs_of'
23
+ import { type ValueTypeOf } from 'types/value_type_of'
24
+ import {
25
+ type Mock,
26
+ vi,
27
+ } from 'vitest'
28
+
29
+ type FlattenedSetters<R extends Record<string, Type>> = {
30
+ [K in keyof R]: Setter<ValueTypeOf<R[K]>>
31
+ }
32
+
33
+ type FlattenedToStrings<R extends Record<string, Type>> = {
34
+ [K in keyof R]: string
35
+ }
36
+
37
+ describe('flattenValueTypeTo', function () {
38
+ let toStringMapper: Mock<Mapper<string>>
39
+ let setMapper: Mock<Mapper<(v: AnyValueType) => void>>
40
+ let setter: Mock<(v: AnyValueType) => void>
41
+ beforeEach(function () {
42
+ toStringMapper = vi.fn(function (_t: TypeDef, v: AnyValueType) {
43
+ return JSON.stringify(v)
44
+ })
45
+ setMapper = vi.fn(function (_t: TypeDef, _v: AnyValueType, setter: Setter<AnyValueType>) {
46
+ return setter
47
+ })
48
+ setter = vi.fn()
49
+ })
50
+
51
+ describe('literal', function () {
52
+ const typeDef = numberType
53
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
54
+ describe('toString', function () {
55
+ let flattened: FlattenedToStrings<F>
56
+ beforeEach(function () {
57
+ flattened = flattenValueTypeTo(
58
+ typeDef,
59
+ 1,
60
+ setter,
61
+ toStringMapper,
62
+ )
63
+ })
64
+
65
+ it('equals expected type', function () {
66
+ expect(flattened).toEqual({
67
+ $: '1',
68
+ })
69
+ })
70
+
71
+ it('calls the mapping function', function () {
72
+ expect(toStringMapper).toHaveBeenCalledTimes(1)
73
+ })
74
+ })
75
+
76
+ describe('setter', function () {
77
+ let flattened: FlattenedSetters<F>
78
+ beforeEach(function () {
79
+ flattened = flattenValueTypeTo(
80
+ typeDef,
81
+ 1,
82
+ setter,
83
+ setMapper,
84
+ )
85
+ })
86
+
87
+ it('calls the top level setter', function () {
88
+ const value = 3
89
+ flattened.$(value)
90
+ expect(setter).toHaveBeenCalledTimes(1)
91
+ expect(setter).toHaveBeenCalledWith(value)
92
+ })
93
+ })
94
+ })
95
+
96
+ describe('list', function () {
97
+ const typeDef = list(numberType)
98
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
99
+ let l: ValueTypeOf<typeof typeDef>
100
+ beforeEach(function () {
101
+ l = [
102
+ 1,
103
+ 2,
104
+ 3,
105
+ ]
106
+ })
107
+
108
+ describe('toString', function () {
109
+ let flattened: FlattenedToStrings<F>
110
+ beforeEach(function () {
111
+ flattened = flattenValueTypeTo(
112
+ typeDef,
113
+ l,
114
+ setter,
115
+ toStringMapper,
116
+ )
117
+ })
118
+
119
+ it('equals expected type', function () {
120
+ expect(flattened).toEqual({
121
+ $: '[1,2,3]',
122
+ ['$.0']: '1',
123
+ ['$.1']: '2',
124
+ ['$.2']: '3',
125
+ })
126
+ })
127
+
128
+ it('calls the mapping function', function () {
129
+ expect(toStringMapper).toHaveBeenCalledTimes(4)
130
+ })
131
+ })
132
+
133
+ describe('setter', function () {
134
+ let flattened: FlattenedSetters<F>
135
+ beforeEach(function () {
136
+ flattened = flattenValueTypeTo(
137
+ typeDef,
138
+ l,
139
+ setter,
140
+ setMapper,
141
+ )
142
+ })
143
+
144
+ it('sets a value in the list', function () {
145
+ flattened['$.1'](4)
146
+ expect(l).toEqual([
147
+ 1,
148
+ 4,
149
+ 3,
150
+ ])
151
+ })
152
+ })
153
+ })
154
+
155
+ describe('record', function () {
156
+ const typeDef = record<typeof numberType, 'a' | 'b'>(numberType)
157
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
158
+
159
+ let m: ValueTypeOf<typeof typeDef>
160
+ beforeEach(function () {
161
+ m = {
162
+ a: 1,
163
+ b: 3,
164
+ }
165
+ })
166
+
167
+ describe('toString', function () {
168
+ let flattened: FlattenedToStrings<F>
169
+ beforeEach(function () {
170
+ flattened = flattenValueTypeTo(
171
+ typeDef,
172
+ m,
173
+ setter,
174
+ toStringMapper,
175
+ )
176
+ })
177
+
178
+ it('equals expected type', function () {
179
+ expect(flattened).toEqual({
180
+ $: '{"a":1,"b":3}',
181
+ ['$.a']: '1',
182
+ ['$.b']: '3',
183
+ })
184
+ })
185
+ })
186
+
187
+ describe('setter', function () {
188
+ let flattened: FlattenedSetters<F>
189
+ beforeEach(function () {
190
+ flattened = flattenValueTypeTo(
191
+ typeDef,
192
+ m,
193
+ setter,
194
+ setMapper,
195
+ )
196
+ })
197
+
198
+ it('sets a value in the record', function () {
199
+ flattened['$.a'](4)
200
+ expect(m).toEqual({
201
+ a: 4,
202
+ b: 3,
203
+ })
204
+ })
205
+ })
206
+ })
207
+
208
+ describe('object', function () {
209
+ const typeDef = object()
210
+ .set('a', numberType)
211
+ .set('b', booleanType)
212
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
213
+
214
+ let s: ValueTypeOf<typeof typeDef>
215
+ beforeEach(function () {
216
+ s = {
217
+ a: 1,
218
+ b: false,
219
+ }
220
+ })
221
+
222
+ describe('toString', function () {
223
+ let flattened: FlattenedToStrings<F>
224
+ beforeEach(function () {
225
+ flattened = flattenValueTypeTo(
226
+ typeDef,
227
+ s,
228
+ setter,
229
+ toStringMapper,
230
+ )
231
+ })
232
+
233
+ it('equals expected type', function () {
234
+ expect(flattened).toEqual({
235
+ $: '{"a":1,"b":false}',
236
+ ['$.a']: '1',
237
+ ['$.b']: 'false',
238
+ })
239
+ })
240
+ })
241
+
242
+ describe('setter', function () {
243
+ let flattened: FlattenedSetters<F>
244
+ beforeEach(function () {
245
+ flattened = flattenValueTypeTo(
246
+ typeDef,
247
+ s,
248
+ setter,
249
+ setMapper,
250
+ )
251
+ })
252
+
253
+ it('sets "a" in the object', function () {
254
+ flattened['$.a'](2)
255
+ expect(s).toEqual({
256
+ a: 2,
257
+ b: false,
258
+ })
259
+ })
260
+
261
+ it('sets "b" in the object', function () {
262
+ flattened['$.b'](true)
263
+ expect(s).toEqual({
264
+ a: 1,
265
+ b: true,
266
+ })
267
+ })
268
+ })
269
+ })
270
+
271
+ describe('union', function () {
272
+ describe('discriminated', function () {
273
+ const typeDef = union('d')
274
+ .add('1', object().set('a', numberType))
275
+ .add('2', object().set('b', booleanType))
276
+ type F = SimplifyDeep<FlattenedTypeDefsOf<typeof typeDef, null>>
277
+ let u: ValueTypeOf<typeof typeDef>
278
+ beforeEach(function () {
279
+ u = {
280
+ d: '1',
281
+ a: 2,
282
+ }
283
+ })
284
+
285
+ describe('toString', function () {
286
+ let flattened: FlattenedToStrings<F>
287
+ beforeEach(function () {
288
+ flattened = flattenValueTypeTo(
289
+ typeDef,
290
+ u,
291
+ setter,
292
+ toStringMapper,
293
+ )
294
+ })
295
+
296
+ it('equals expected type', function () {
297
+ expect(flattened).toEqual({
298
+ $: '{"d":"1","a":2}',
299
+ ['$.1:a']: '2',
300
+ })
301
+ })
302
+ })
303
+
304
+ describe('setter', function () {
305
+ type G = SimplifyDeep<FlattenedSetters<F>>
306
+ let flattened: G
307
+ beforeEach(function () {
308
+ flattened = flattenValueTypeTo(
309
+ typeDef,
310
+ u,
311
+ setter,
312
+ setMapper,
313
+ )
314
+ })
315
+
316
+ it('sets a value', function () {
317
+ const value = {
318
+ d: '2',
319
+ b: false,
320
+ } as const
321
+ flattened.$(value)
322
+
323
+ expect(setter).toHaveBeenCalledWith(value)
324
+ })
325
+
326
+ it('sets an internal value', function () {
327
+ flattened['$.1:a'](1)
328
+
329
+ expect(u).toEqual({
330
+ d: '1',
331
+ a: 1,
332
+ })
333
+ })
334
+ })
335
+ })
336
+ describe('non-discriminated', function () {
337
+ const typeDef = union()
338
+ .add('0', numberType)
339
+ .add('1', nullType)
340
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
341
+ let u: ValueTypeOf<typeof typeDef>
342
+
343
+ beforeEach(function () {
344
+ u = null
345
+ })
346
+
347
+ describe('toString', function () {
348
+ let flattened: FlattenedToStrings<F>
349
+ beforeEach(function () {
350
+ flattened = flattenValueTypeTo(
351
+ typeDef,
352
+ u,
353
+ setter,
354
+ toStringMapper,
355
+ )
356
+ })
357
+
358
+ it('equals expected type', function () {
359
+ expect(flattened).toEqual({
360
+ $: 'null',
361
+ })
362
+ })
363
+ })
364
+
365
+ describe('setter', function () {
366
+ let flattened: FlattenedSetters<F>
367
+ beforeEach(function () {
368
+ flattened = flattenValueTypeTo(
369
+ typeDef,
370
+ u,
371
+ setter,
372
+ setMapper,
373
+ )
374
+ })
375
+
376
+ it('sets a value', function () {
377
+ const value = 2
378
+ flattened.$(value)
379
+
380
+ expect(setter).toHaveBeenCalledWith(value)
381
+ })
382
+ })
383
+ })
384
+
385
+ describe('complex non-discriminated', function () {
386
+ const typeDef = union()
387
+ .add('z', list(numberType))
388
+ .add('x', nullType)
389
+ .add('y', literal([false]))
390
+ type F = FlattenedTypeDefsOf<typeof typeDef, null>
391
+
392
+ let u: ValueTypeOf<typeof typeDef>
393
+ beforeEach(function () {
394
+ u = [
395
+ 1,
396
+ 2,
397
+ 3,
398
+ ]
399
+ })
400
+
401
+ describe('toString', function () {
402
+ let flattened: FlattenedToStrings<F>
403
+ beforeEach(function () {
404
+ flattened = flattenValueTypeTo(
405
+ typeDef,
406
+ u,
407
+ setter,
408
+ toStringMapper,
409
+ )
410
+ })
411
+
412
+ it('equals expected type', function () {
413
+ expect(flattened).toEqual({
414
+ $: '[1,2,3]',
415
+ ['$.0']: '1',
416
+ ['$.1']: '2',
417
+ ['$.2']: '3',
418
+ })
419
+ })
420
+ })
421
+
422
+ describe('setter', function () {
423
+ let flattened: FlattenedSetters<F>
424
+ beforeEach(function () {
425
+ flattened = flattenValueTypeTo(
426
+ typeDef,
427
+ u,
428
+ setter,
429
+ setMapper,
430
+ )
431
+ })
432
+
433
+ it('sets the top level value', function () {
434
+ const value = [100]
435
+ flattened.$(value)
436
+ expect(setter).toHaveBeenCalledWith(value)
437
+ })
438
+
439
+ it('sets a subordinate value', function () {
440
+ // need to cast to any as TS cannot guarantee that '$.a' is present
441
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
442
+ ;(flattened as any)['$.1'](4)
443
+ expect(u).toEqual([
444
+ 1,
445
+ 4,
446
+ 3,
447
+ ])
448
+ })
449
+ })
450
+ })
451
+ })
452
+ })
@@ -0,0 +1,46 @@
1
+ import { flattenValueTypesOf } from 'transformers/flatteners/flatten_value_types_of'
2
+ import {
3
+ booleanType,
4
+ list,
5
+ numberType,
6
+ object,
7
+ } from 'types/builders'
8
+
9
+ describe('flattenValueTypesOf', function () {
10
+ // note that we already have tests for the type and the function that this calls, so
11
+ // this is only a sanity check
12
+ it('flattens', function () {
13
+ const flattened = flattenValueTypesOf(
14
+ object()
15
+ .set('a', list(numberType))
16
+ .set('b', booleanType),
17
+ {
18
+ a: [
19
+ 1,
20
+ 2,
21
+ 4,
22
+ ],
23
+ b: false,
24
+ },
25
+ )
26
+ expect(flattened).toEqual({
27
+ $: {
28
+ a: [
29
+ 1,
30
+ 2,
31
+ 4,
32
+ ],
33
+ b: false,
34
+ },
35
+ '$.a': [
36
+ 1,
37
+ 2,
38
+ 4,
39
+ ],
40
+ '$.a.0': 1,
41
+ '$.a.1': 2,
42
+ '$.a.2': 4,
43
+ '$.b': false,
44
+ })
45
+ })
46
+ })