@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.
- package/.eslintrc.cjs +31 -0
- package/.out/index.d.ts +20 -0
- package/.out/index.js +20 -0
- package/.out/transformers/copies/copy.d.ts +4 -0
- package/.out/transformers/copies/copy.js +7 -0
- package/.out/transformers/copies/copy_to.d.ts +6 -0
- package/.out/transformers/copies/copy_to.js +91 -0
- package/.out/transformers/copies/mobx_copy.d.ts +5 -0
- package/.out/transformers/copies/mobx_copy.js +42 -0
- package/.out/transformers/copies/specs/copy_to.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/copy_to.tests.js +97 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.js +19 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +10 -0
- package/.out/transformers/flatteners/flatten_type_def_to.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_type_def_to.js +49 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.js +7 -0
- package/.out/transformers/flatteners/flatten_value_type_to.d.ts +11 -0
- package/.out/transformers/flatteners/flatten_value_type_to.js +79 -0
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_value_types_of.js +7 -0
- package/.out/transformers/flatteners/json_path.d.ts +2 -0
- package/.out/transformers/flatteners/json_path.js +14 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.js +90 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +94 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.js +110 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +17 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.js +297 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.js +37 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +167 -0
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +2 -0
- package/.out/transformers/flatteners/value_path_to_type_path.js +96 -0
- package/.out/tsconfig.json +15 -0
- package/.out/tsconfig.tsbuildinfo +1 -0
- package/.out/tsup.config.d.ts +3 -0
- package/.out/tsup.config.js +12 -0
- package/.out/types/builders.d.ts +62 -0
- package/.out/types/builders.js +148 -0
- package/.out/types/definitions.d.ts +41 -0
- package/.out/types/definitions.js +26 -0
- package/.out/types/flattened.d.ts +2 -0
- package/.out/types/flattened.js +1 -0
- package/.out/types/flattened_accessors_of.d.ts +9 -0
- package/.out/types/flattened_accessors_of.js +1 -0
- package/.out/types/flattened_type_defs_of.d.ts +21 -0
- package/.out/types/flattened_type_defs_of.js +1 -0
- package/.out/types/flattened_value_types_of.d.ts +6 -0
- package/.out/types/flattened_value_types_of.js +1 -0
- package/.out/types/json_path_of.d.ts +1 -0
- package/.out/types/json_path_of.js +1 -0
- package/.out/types/json_paths_of.d.ts +16 -0
- package/.out/types/json_paths_of.js +1 -0
- package/.out/types/mobx_value_type_of.d.ts +11 -0
- package/.out/types/mobx_value_type_of.js +2 -0
- package/.out/types/partial_type_def_of.d.ts +40 -0
- package/.out/types/partial_type_def_of.js +1 -0
- package/.out/types/readonly_type_def_of.d.ts +29 -0
- package/.out/types/readonly_type_def_of.js +1 -0
- package/.out/types/specs/builder.tests.d.ts +1 -0
- package/.out/types/specs/builder.tests.js +93 -0
- package/.out/types/specs/flattened_accessors_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_accessors_of.tests.js +11 -0
- package/.out/types/specs/flattened_type_defs_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_type_defs_of.tests.js +71 -0
- package/.out/types/specs/flattened_value_types_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_value_types_of.tests.js +11 -0
- package/.out/types/specs/json_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/json_paths_of.tests.js +199 -0
- package/.out/types/specs/partial_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/partial_type_def_of.tests.js +50 -0
- package/.out/types/specs/readonly_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/readonly_type_def_of.tests.js +55 -0
- package/.out/types/specs/strict_definitions.tests.d.ts +1 -0
- package/.out/types/specs/strict_definitions.tests.js +42 -0
- package/.out/types/specs/value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/value_to_type_paths_of.tests.js +90 -0
- package/.out/types/specs/value_type_of.tests.d.ts +1 -0
- package/.out/types/specs/value_type_of.tests.js +91 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.d.ts +1 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +9 -0
- package/.out/types/strict_definitions.d.ts +44 -0
- package/.out/types/strict_definitions.js +1 -0
- package/.out/types/value_to_type_paths_of.d.ts +22 -0
- package/.out/types/value_to_type_paths_of.js +1 -0
- package/.out/types/value_type_of.d.ts +24 -0
- package/.out/types/value_type_of.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +9 -0
- package/.out/types/value_types_of_discriminated_union.js +1 -0
- package/.out/vitest.workspace.d.ts +2 -0
- package/.out/vitest.workspace.js +7 -0
- package/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-check-types.log +3 -0
- package/.turbo/turbo-release$colon$exports.log +3 -0
- package/README.md +12 -0
- package/dist/index.cjs +798 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +775 -0
- package/index.ts +20 -0
- package/package.exports.json +18 -0
- package/package.json +45 -0
- package/transformers/copies/copy.ts +18 -0
- package/transformers/copies/copy_to.ts +226 -0
- package/transformers/copies/mobx_copy.ts +81 -0
- package/transformers/copies/specs/copy_to.tests.ts +143 -0
- package/transformers/copies/specs/mobx_copy.tests.ts +26 -0
- package/transformers/flatteners/flatten_accessors_of.ts +43 -0
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +39 -0
- package/transformers/flatteners/flatten_type_def_to.ts +127 -0
- package/transformers/flatteners/flatten_type_defs_of.ts +16 -0
- package/transformers/flatteners/flatten_value_type_to.ts +227 -0
- package/transformers/flatteners/flatten_value_types_of.ts +23 -0
- package/transformers/flatteners/json_path.ts +15 -0
- package/transformers/flatteners/specs/flatten_accessors_of.tests.ts +113 -0
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +115 -0
- package/transformers/flatteners/specs/flatten_type_def_to.tests.ts +146 -0
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +26 -0
- package/transformers/flatteners/specs/flatten_value_type_to.tests.ts +452 -0
- package/transformers/flatteners/specs/flatten_value_types_of.tests.ts +46 -0
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +240 -0
- package/transformers/flatteners/value_path_to_type_path.ts +164 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +16 -0
- package/types/builders.ts +284 -0
- package/types/definitions.ts +106 -0
- package/types/flattened.ts +5 -0
- package/types/flattened_accessors_of.ts +15 -0
- package/types/flattened_type_defs_of.ts +130 -0
- package/types/flattened_value_types_of.ts +11 -0
- package/types/json_path_of.ts +7 -0
- package/types/json_paths_of.ts +129 -0
- package/types/mobx_value_type_of.ts +16 -0
- package/types/partial_type_def_of.ts +64 -0
- package/types/readonly_type_def_of.ts +53 -0
- package/types/specs/builder.tests.ts +295 -0
- package/types/specs/flattened_accessors_of.tests.ts +27 -0
- package/types/specs/flattened_type_defs_of.tests.ts +212 -0
- package/types/specs/flattened_value_types_of.tests.ts +21 -0
- package/types/specs/json_paths_of.tests.ts +304 -0
- package/types/specs/partial_type_def_of.tests.ts +251 -0
- package/types/specs/readonly_type_def_of.tests.ts +158 -0
- package/types/specs/strict_definitions.tests.ts +184 -0
- package/types/specs/value_to_type_paths_of.tests.ts +181 -0
- package/types/specs/value_type_of.tests.ts +329 -0
- package/types/specs/value_types_of_discriminated_union.tests.ts +30 -0
- package/types/strict_definitions.ts +107 -0
- package/types/value_to_type_paths_of.ts +184 -0
- package/types/value_type_of.ts +84 -0
- package/types/value_types_of_discriminated_union.ts +14 -0
- package/vitest.workspace.ts +11 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { expectDefinedAndReturn } from '@strictly/base';
|
|
2
|
+
import { flattenAccessorsOf } from 'transformers/flatteners/flatten_accessors_of';
|
|
3
|
+
import { booleanType, list, numberType, object, } from 'types/builders';
|
|
4
|
+
import { vi, } from 'vitest';
|
|
5
|
+
describe('flattenAccessorsOf', function () {
|
|
6
|
+
let setter;
|
|
7
|
+
const builder = object()
|
|
8
|
+
.set('a', list(numberType))
|
|
9
|
+
.set('b', booleanType)
|
|
10
|
+
.narrow;
|
|
11
|
+
let flattened;
|
|
12
|
+
let value;
|
|
13
|
+
beforeEach(function () {
|
|
14
|
+
setter = vi.fn();
|
|
15
|
+
value = {
|
|
16
|
+
a: [
|
|
17
|
+
1,
|
|
18
|
+
2,
|
|
19
|
+
4,
|
|
20
|
+
],
|
|
21
|
+
b: false,
|
|
22
|
+
};
|
|
23
|
+
flattened = flattenAccessorsOf(builder, value, setter);
|
|
24
|
+
});
|
|
25
|
+
// note that we already have tests for the type and the function that this calls, so
|
|
26
|
+
// this is only a sanity check
|
|
27
|
+
it('flattens to expected type', function () {
|
|
28
|
+
expect(flattened).toEqual({
|
|
29
|
+
$: {
|
|
30
|
+
value: {
|
|
31
|
+
a: [
|
|
32
|
+
1,
|
|
33
|
+
2,
|
|
34
|
+
4,
|
|
35
|
+
],
|
|
36
|
+
b: false,
|
|
37
|
+
},
|
|
38
|
+
set: setter,
|
|
39
|
+
},
|
|
40
|
+
'$.a': expect.objectContaining({
|
|
41
|
+
value: [
|
|
42
|
+
1,
|
|
43
|
+
2,
|
|
44
|
+
4,
|
|
45
|
+
],
|
|
46
|
+
}),
|
|
47
|
+
'$.a.0': expect.objectContaining({
|
|
48
|
+
value: 1,
|
|
49
|
+
}),
|
|
50
|
+
'$.a.1': expect.objectContaining({
|
|
51
|
+
value: 2,
|
|
52
|
+
}),
|
|
53
|
+
'$.a.2': expect.objectContaining({
|
|
54
|
+
value: 4,
|
|
55
|
+
}),
|
|
56
|
+
'$.b': expect.objectContaining({
|
|
57
|
+
value: false,
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
it('sets an internal value of a struct', function () {
|
|
62
|
+
expectDefinedAndReturn(flattened['$.a']).set([5]);
|
|
63
|
+
expect(value).toEqual({
|
|
64
|
+
a: [5],
|
|
65
|
+
b: false,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
it('sets an internal value of an array', function () {
|
|
69
|
+
expectDefinedAndReturn(flattened['$.a.1']).set(99);
|
|
70
|
+
expect(value).toEqual({
|
|
71
|
+
a: [
|
|
72
|
+
1,
|
|
73
|
+
99,
|
|
74
|
+
4,
|
|
75
|
+
],
|
|
76
|
+
b: false,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
it('sets the top level value', function () {
|
|
80
|
+
const newValue = {
|
|
81
|
+
a: [
|
|
82
|
+
-1,
|
|
83
|
+
5,
|
|
84
|
+
],
|
|
85
|
+
b: true,
|
|
86
|
+
};
|
|
87
|
+
expectDefinedAndReturn(flattened.$).set(newValue);
|
|
88
|
+
expect(setter).toHaveBeenCalledWith(newValue);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { flattenJsonValueToTypePathsOf } from 'transformers/flatteners/flatten_json_value_to_type_paths_of';
|
|
2
|
+
import { booleanType, list, nullType, numberType, object, record, stringType, union, } from 'types/builders';
|
|
3
|
+
describe('flattenJsonValueToTypePathsOf', function () {
|
|
4
|
+
describe('literal', function () {
|
|
5
|
+
const typeDef = numberType;
|
|
6
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, 1);
|
|
7
|
+
it('equals expected value', function () {
|
|
8
|
+
expect(flattened).toEqual({
|
|
9
|
+
$: '$',
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
describe('list', function () {
|
|
14
|
+
const typeDef = list(numberType);
|
|
15
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, [
|
|
16
|
+
1,
|
|
17
|
+
2,
|
|
18
|
+
]);
|
|
19
|
+
it('equals expected value', function () {
|
|
20
|
+
expect(flattened).toEqual({
|
|
21
|
+
$: '$',
|
|
22
|
+
'$.0': '$.*',
|
|
23
|
+
'$.1': '$.*',
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe('record', function () {
|
|
28
|
+
const typeDef = record(numberType);
|
|
29
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, {
|
|
30
|
+
a: 1,
|
|
31
|
+
b: 3,
|
|
32
|
+
});
|
|
33
|
+
it('equals expected value', function () {
|
|
34
|
+
expect(flattened).toEqual({
|
|
35
|
+
$: '$',
|
|
36
|
+
'$.a': '$.*',
|
|
37
|
+
'$.b': '$.*',
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe('object', function () {
|
|
42
|
+
const typeDef = object()
|
|
43
|
+
.set('a', numberType)
|
|
44
|
+
.set('b', booleanType);
|
|
45
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, {
|
|
46
|
+
a: 1,
|
|
47
|
+
b: true,
|
|
48
|
+
});
|
|
49
|
+
it('equals expected value', function () {
|
|
50
|
+
expect(flattened).toEqual({
|
|
51
|
+
$: '$',
|
|
52
|
+
'$.a': '$.a',
|
|
53
|
+
'$.b': '$.b',
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('union', function () {
|
|
58
|
+
describe('non-discriminated', function () {
|
|
59
|
+
const typeDef = union()
|
|
60
|
+
.add('a', list(numberType))
|
|
61
|
+
.add('b', nullType);
|
|
62
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, [
|
|
63
|
+
1,
|
|
64
|
+
2,
|
|
65
|
+
3,
|
|
66
|
+
]);
|
|
67
|
+
it('equals expected value', function () {
|
|
68
|
+
expect(flattened).toEqual({
|
|
69
|
+
$: '$',
|
|
70
|
+
'$.0': '$.*',
|
|
71
|
+
'$.1': '$.*',
|
|
72
|
+
'$.2': '$.*',
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('discriminated', function () {
|
|
77
|
+
const typeDef = union('d')
|
|
78
|
+
.add('x', object().set('a', numberType).set('b', booleanType))
|
|
79
|
+
.add('y', object().set('c', stringType).set('d', booleanType));
|
|
80
|
+
const flattened = flattenJsonValueToTypePathsOf(typeDef, {
|
|
81
|
+
d: 'x',
|
|
82
|
+
a: 1,
|
|
83
|
+
b: true,
|
|
84
|
+
});
|
|
85
|
+
it('equals expected value', function () {
|
|
86
|
+
expect(flattened).toEqual({
|
|
87
|
+
$: '$',
|
|
88
|
+
'$.x:a': '$.x:a',
|
|
89
|
+
'$.x:b': '$.x:b',
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { flattenTypeDefTo } from 'transformers/flatteners/flatten_type_def_to';
|
|
2
|
+
import { booleanType, list, nullType, numberType, object, record, union, } from 'types/builders';
|
|
3
|
+
import { TypeDefType, } from 'types/definitions';
|
|
4
|
+
import { vi, } from 'vitest';
|
|
5
|
+
describe('flattenTypeDefTo', function () {
|
|
6
|
+
let toTypeDefType;
|
|
7
|
+
let flattened;
|
|
8
|
+
beforeEach(function () {
|
|
9
|
+
toTypeDefType = vi.fn(function (typeDef) {
|
|
10
|
+
return typeDef.type;
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
describe('literal', function () {
|
|
14
|
+
beforeEach(function () {
|
|
15
|
+
flattened = flattenTypeDefTo(numberType, toTypeDefType);
|
|
16
|
+
});
|
|
17
|
+
it('equals expected type', function () {
|
|
18
|
+
expect(flattened).toEqual({
|
|
19
|
+
$: TypeDefType.Literal,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
it('calls the mapper function', function () {
|
|
23
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(1);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('list', function () {
|
|
27
|
+
const type = list(numberType);
|
|
28
|
+
beforeEach(function () {
|
|
29
|
+
flattened = flattenTypeDefTo(type, toTypeDefType);
|
|
30
|
+
});
|
|
31
|
+
it('equals expected type', function () {
|
|
32
|
+
expect(flattened).toEqual({
|
|
33
|
+
$: TypeDefType.List,
|
|
34
|
+
'$.*': TypeDefType.Literal,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
it('calls the mapper function', function () {
|
|
38
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(2);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe('record', function () {
|
|
42
|
+
const type = record(numberType);
|
|
43
|
+
beforeEach(function () {
|
|
44
|
+
flattened = flattenTypeDefTo(type, toTypeDefType);
|
|
45
|
+
});
|
|
46
|
+
it('equals expected type', function () {
|
|
47
|
+
expect(flattened).toEqual({
|
|
48
|
+
$: TypeDefType.Record,
|
|
49
|
+
'$.*': TypeDefType.Literal,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
it('calls the mapper function', function () {
|
|
53
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(2);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('object', function () {
|
|
57
|
+
const type = object().set('a', numberType).set('b', list(booleanType));
|
|
58
|
+
beforeEach(function () {
|
|
59
|
+
flattened = flattenTypeDefTo(type, toTypeDefType);
|
|
60
|
+
});
|
|
61
|
+
it('equals expected type', function () {
|
|
62
|
+
expect(flattened).toEqual({
|
|
63
|
+
$: TypeDefType.Object,
|
|
64
|
+
'$.a': TypeDefType.Literal,
|
|
65
|
+
'$.b': TypeDefType.List,
|
|
66
|
+
'$.b.*': TypeDefType.Literal,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
it('calls the mapper function', function () {
|
|
70
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(4);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe('union', function () {
|
|
74
|
+
describe('non-discriminated', function () {
|
|
75
|
+
const type = union()
|
|
76
|
+
.add('a', nullType)
|
|
77
|
+
.add('b', booleanType)
|
|
78
|
+
.add('c', numberType);
|
|
79
|
+
beforeEach(function () {
|
|
80
|
+
flattened = flattenTypeDefTo(type, toTypeDefType);
|
|
81
|
+
});
|
|
82
|
+
it('equals expected type', function () {
|
|
83
|
+
expect(flattened).toEqual({
|
|
84
|
+
$: TypeDefType.Union,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
it('calls the mapper function', function () {
|
|
88
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(1);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
describe('discriminated', function () {
|
|
92
|
+
const type = union('d')
|
|
93
|
+
.add('a', object().set('a', booleanType))
|
|
94
|
+
.add('b', object().set('b', numberType));
|
|
95
|
+
beforeEach(function () {
|
|
96
|
+
flattened = flattenTypeDefTo(type, toTypeDefType);
|
|
97
|
+
});
|
|
98
|
+
it('equals expected type', function () {
|
|
99
|
+
expect(flattened).toEqual({
|
|
100
|
+
$: TypeDefType.Union,
|
|
101
|
+
'$.a:a': TypeDefType.Literal,
|
|
102
|
+
'$.b:b': TypeDefType.Literal,
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
it('calls the mapper function', function () {
|
|
106
|
+
expect(toTypeDefType).toHaveBeenCalledTimes(3);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { flattenTypeDefsOf } from 'transformers/flatteners/flatten_type_defs_of';
|
|
2
|
+
import { booleanType, list, numberType, object, } from 'types/builders';
|
|
3
|
+
describe('flattenTypeDefsOf', function () {
|
|
4
|
+
it('flattens', function () {
|
|
5
|
+
const listTypeDef = list(numberType);
|
|
6
|
+
const structTypeDef = object()
|
|
7
|
+
.set('a', listTypeDef)
|
|
8
|
+
.set('b', booleanType);
|
|
9
|
+
const flattened = flattenTypeDefsOf(structTypeDef);
|
|
10
|
+
expect(flattened).toEqual({
|
|
11
|
+
$: structTypeDef.narrow,
|
|
12
|
+
'$.a': listTypeDef.narrow,
|
|
13
|
+
'$.a.*': numberType.narrow,
|
|
14
|
+
'$.b': booleanType.narrow,
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { flattenValueTypeTo, } from 'transformers/flatteners/flatten_value_type_to';
|
|
2
|
+
import { booleanType, list, literal, nullType, numberType, object, record, union, } from 'types/builders';
|
|
3
|
+
import { vi, } from 'vitest';
|
|
4
|
+
describe('flattenValueTypeTo', function () {
|
|
5
|
+
let toStringMapper;
|
|
6
|
+
let setMapper;
|
|
7
|
+
let setter;
|
|
8
|
+
beforeEach(function () {
|
|
9
|
+
toStringMapper = vi.fn(function (_t, v) {
|
|
10
|
+
return JSON.stringify(v);
|
|
11
|
+
});
|
|
12
|
+
setMapper = vi.fn(function (_t, _v, setter) {
|
|
13
|
+
return setter;
|
|
14
|
+
});
|
|
15
|
+
setter = vi.fn();
|
|
16
|
+
});
|
|
17
|
+
describe('literal', function () {
|
|
18
|
+
const typeDef = numberType;
|
|
19
|
+
describe('toString', function () {
|
|
20
|
+
let flattened;
|
|
21
|
+
beforeEach(function () {
|
|
22
|
+
flattened = flattenValueTypeTo(typeDef, 1, setter, toStringMapper);
|
|
23
|
+
});
|
|
24
|
+
it('equals expected type', function () {
|
|
25
|
+
expect(flattened).toEqual({
|
|
26
|
+
$: '1',
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it('calls the mapping function', function () {
|
|
30
|
+
expect(toStringMapper).toHaveBeenCalledTimes(1);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('setter', function () {
|
|
34
|
+
let flattened;
|
|
35
|
+
beforeEach(function () {
|
|
36
|
+
flattened = flattenValueTypeTo(typeDef, 1, setter, setMapper);
|
|
37
|
+
});
|
|
38
|
+
it('calls the top level setter', function () {
|
|
39
|
+
const value = 3;
|
|
40
|
+
flattened.$(value);
|
|
41
|
+
expect(setter).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(setter).toHaveBeenCalledWith(value);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
describe('list', function () {
|
|
47
|
+
const typeDef = list(numberType);
|
|
48
|
+
let l;
|
|
49
|
+
beforeEach(function () {
|
|
50
|
+
l = [
|
|
51
|
+
1,
|
|
52
|
+
2,
|
|
53
|
+
3,
|
|
54
|
+
];
|
|
55
|
+
});
|
|
56
|
+
describe('toString', function () {
|
|
57
|
+
let flattened;
|
|
58
|
+
beforeEach(function () {
|
|
59
|
+
flattened = flattenValueTypeTo(typeDef, l, setter, toStringMapper);
|
|
60
|
+
});
|
|
61
|
+
it('equals expected type', function () {
|
|
62
|
+
expect(flattened).toEqual({
|
|
63
|
+
$: '[1,2,3]',
|
|
64
|
+
['$.0']: '1',
|
|
65
|
+
['$.1']: '2',
|
|
66
|
+
['$.2']: '3',
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
it('calls the mapping function', function () {
|
|
70
|
+
expect(toStringMapper).toHaveBeenCalledTimes(4);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe('setter', function () {
|
|
74
|
+
let flattened;
|
|
75
|
+
beforeEach(function () {
|
|
76
|
+
flattened = flattenValueTypeTo(typeDef, l, setter, setMapper);
|
|
77
|
+
});
|
|
78
|
+
it('sets a value in the list', function () {
|
|
79
|
+
flattened['$.1'](4);
|
|
80
|
+
expect(l).toEqual([
|
|
81
|
+
1,
|
|
82
|
+
4,
|
|
83
|
+
3,
|
|
84
|
+
]);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe('record', function () {
|
|
89
|
+
const typeDef = record(numberType);
|
|
90
|
+
let m;
|
|
91
|
+
beforeEach(function () {
|
|
92
|
+
m = {
|
|
93
|
+
a: 1,
|
|
94
|
+
b: 3,
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
describe('toString', function () {
|
|
98
|
+
let flattened;
|
|
99
|
+
beforeEach(function () {
|
|
100
|
+
flattened = flattenValueTypeTo(typeDef, m, setter, toStringMapper);
|
|
101
|
+
});
|
|
102
|
+
it('equals expected type', function () {
|
|
103
|
+
expect(flattened).toEqual({
|
|
104
|
+
$: '{"a":1,"b":3}',
|
|
105
|
+
['$.a']: '1',
|
|
106
|
+
['$.b']: '3',
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('setter', function () {
|
|
111
|
+
let flattened;
|
|
112
|
+
beforeEach(function () {
|
|
113
|
+
flattened = flattenValueTypeTo(typeDef, m, setter, setMapper);
|
|
114
|
+
});
|
|
115
|
+
it('sets a value in the record', function () {
|
|
116
|
+
flattened['$.a'](4);
|
|
117
|
+
expect(m).toEqual({
|
|
118
|
+
a: 4,
|
|
119
|
+
b: 3,
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('object', function () {
|
|
125
|
+
const typeDef = object()
|
|
126
|
+
.set('a', numberType)
|
|
127
|
+
.set('b', booleanType);
|
|
128
|
+
let s;
|
|
129
|
+
beforeEach(function () {
|
|
130
|
+
s = {
|
|
131
|
+
a: 1,
|
|
132
|
+
b: false,
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
describe('toString', function () {
|
|
136
|
+
let flattened;
|
|
137
|
+
beforeEach(function () {
|
|
138
|
+
flattened = flattenValueTypeTo(typeDef, s, setter, toStringMapper);
|
|
139
|
+
});
|
|
140
|
+
it('equals expected type', function () {
|
|
141
|
+
expect(flattened).toEqual({
|
|
142
|
+
$: '{"a":1,"b":false}',
|
|
143
|
+
['$.a']: '1',
|
|
144
|
+
['$.b']: 'false',
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe('setter', function () {
|
|
149
|
+
let flattened;
|
|
150
|
+
beforeEach(function () {
|
|
151
|
+
flattened = flattenValueTypeTo(typeDef, s, setter, setMapper);
|
|
152
|
+
});
|
|
153
|
+
it('sets "a" in the object', function () {
|
|
154
|
+
flattened['$.a'](2);
|
|
155
|
+
expect(s).toEqual({
|
|
156
|
+
a: 2,
|
|
157
|
+
b: false,
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
it('sets "b" in the object', function () {
|
|
161
|
+
flattened['$.b'](true);
|
|
162
|
+
expect(s).toEqual({
|
|
163
|
+
a: 1,
|
|
164
|
+
b: true,
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
describe('union', function () {
|
|
170
|
+
describe('discriminated', function () {
|
|
171
|
+
const typeDef = union('d')
|
|
172
|
+
.add('1', object().set('a', numberType))
|
|
173
|
+
.add('2', object().set('b', booleanType));
|
|
174
|
+
let u;
|
|
175
|
+
beforeEach(function () {
|
|
176
|
+
u = {
|
|
177
|
+
d: '1',
|
|
178
|
+
a: 2,
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
describe('toString', function () {
|
|
182
|
+
let flattened;
|
|
183
|
+
beforeEach(function () {
|
|
184
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, toStringMapper);
|
|
185
|
+
});
|
|
186
|
+
it('equals expected type', function () {
|
|
187
|
+
expect(flattened).toEqual({
|
|
188
|
+
$: '{"d":"1","a":2}',
|
|
189
|
+
['$.1:a']: '2',
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
describe('setter', function () {
|
|
194
|
+
let flattened;
|
|
195
|
+
beforeEach(function () {
|
|
196
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, setMapper);
|
|
197
|
+
});
|
|
198
|
+
it('sets a value', function () {
|
|
199
|
+
const value = {
|
|
200
|
+
d: '2',
|
|
201
|
+
b: false,
|
|
202
|
+
};
|
|
203
|
+
flattened.$(value);
|
|
204
|
+
expect(setter).toHaveBeenCalledWith(value);
|
|
205
|
+
});
|
|
206
|
+
it('sets an internal value', function () {
|
|
207
|
+
flattened['$.1:a'](1);
|
|
208
|
+
expect(u).toEqual({
|
|
209
|
+
d: '1',
|
|
210
|
+
a: 1,
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
describe('non-discriminated', function () {
|
|
216
|
+
const typeDef = union()
|
|
217
|
+
.add('0', numberType)
|
|
218
|
+
.add('1', nullType);
|
|
219
|
+
let u;
|
|
220
|
+
beforeEach(function () {
|
|
221
|
+
u = null;
|
|
222
|
+
});
|
|
223
|
+
describe('toString', function () {
|
|
224
|
+
let flattened;
|
|
225
|
+
beforeEach(function () {
|
|
226
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, toStringMapper);
|
|
227
|
+
});
|
|
228
|
+
it('equals expected type', function () {
|
|
229
|
+
expect(flattened).toEqual({
|
|
230
|
+
$: 'null',
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
describe('setter', function () {
|
|
235
|
+
let flattened;
|
|
236
|
+
beforeEach(function () {
|
|
237
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, setMapper);
|
|
238
|
+
});
|
|
239
|
+
it('sets a value', function () {
|
|
240
|
+
const value = 2;
|
|
241
|
+
flattened.$(value);
|
|
242
|
+
expect(setter).toHaveBeenCalledWith(value);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
describe('complex non-discriminated', function () {
|
|
247
|
+
const typeDef = union()
|
|
248
|
+
.add('z', list(numberType))
|
|
249
|
+
.add('x', nullType)
|
|
250
|
+
.add('y', literal([false]));
|
|
251
|
+
let u;
|
|
252
|
+
beforeEach(function () {
|
|
253
|
+
u = [
|
|
254
|
+
1,
|
|
255
|
+
2,
|
|
256
|
+
3,
|
|
257
|
+
];
|
|
258
|
+
});
|
|
259
|
+
describe('toString', function () {
|
|
260
|
+
let flattened;
|
|
261
|
+
beforeEach(function () {
|
|
262
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, toStringMapper);
|
|
263
|
+
});
|
|
264
|
+
it('equals expected type', function () {
|
|
265
|
+
expect(flattened).toEqual({
|
|
266
|
+
$: '[1,2,3]',
|
|
267
|
+
['$.0']: '1',
|
|
268
|
+
['$.1']: '2',
|
|
269
|
+
['$.2']: '3',
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
describe('setter', function () {
|
|
274
|
+
let flattened;
|
|
275
|
+
beforeEach(function () {
|
|
276
|
+
flattened = flattenValueTypeTo(typeDef, u, setter, setMapper);
|
|
277
|
+
});
|
|
278
|
+
it('sets the top level value', function () {
|
|
279
|
+
const value = [100];
|
|
280
|
+
flattened.$(value);
|
|
281
|
+
expect(setter).toHaveBeenCalledWith(value);
|
|
282
|
+
});
|
|
283
|
+
it('sets a subordinate value', function () {
|
|
284
|
+
// need to cast to any as TS cannot guarantee that '$.a' is present
|
|
285
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
|
286
|
+
;
|
|
287
|
+
flattened['$.1'](4);
|
|
288
|
+
expect(u).toEqual([
|
|
289
|
+
1,
|
|
290
|
+
4,
|
|
291
|
+
3,
|
|
292
|
+
]);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { flattenValueTypesOf } from 'transformers/flatteners/flatten_value_types_of';
|
|
2
|
+
import { booleanType, list, numberType, object, } from 'types/builders';
|
|
3
|
+
describe('flattenValueTypesOf', function () {
|
|
4
|
+
// note that we already have tests for the type and the function that this calls, so
|
|
5
|
+
// this is only a sanity check
|
|
6
|
+
it('flattens', function () {
|
|
7
|
+
const flattened = flattenValueTypesOf(object()
|
|
8
|
+
.set('a', list(numberType))
|
|
9
|
+
.set('b', booleanType), {
|
|
10
|
+
a: [
|
|
11
|
+
1,
|
|
12
|
+
2,
|
|
13
|
+
4,
|
|
14
|
+
],
|
|
15
|
+
b: false,
|
|
16
|
+
});
|
|
17
|
+
expect(flattened).toEqual({
|
|
18
|
+
$: {
|
|
19
|
+
a: [
|
|
20
|
+
1,
|
|
21
|
+
2,
|
|
22
|
+
4,
|
|
23
|
+
],
|
|
24
|
+
b: false,
|
|
25
|
+
},
|
|
26
|
+
'$.a': [
|
|
27
|
+
1,
|
|
28
|
+
2,
|
|
29
|
+
4,
|
|
30
|
+
],
|
|
31
|
+
'$.a.0': 1,
|
|
32
|
+
'$.a.1': 2,
|
|
33
|
+
'$.a.2': 4,
|
|
34
|
+
'$.b': false,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|