forked from RossWilliams/ts-case-convert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaseConvert.bugs.test.ts
159 lines (127 loc) · 4.69 KB
/
caseConvert.bugs.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
ObjectToCamel,
ObjectToPascal,
ObjectToSnake,
objectToCamel,
objectToSnake,
} from '../src/caseConvert';
describe('bug fixes', () => {
it('#50 - Does not handle an array of objects correctly', () => {
interface MyObject {
index: number;
value?: string;
}
interface MyObjectWithArray {
object_array: MyObject[];
}
type CamelCaseConvertedObjectWithArray = ObjectToCamel<MyObjectWithArray>;
const camelRequest: CamelCaseConvertedObjectWithArray = {
objectArray: [
{
index: 0,
value: 'abc',
},
],
};
expect(camelRequest.objectArray?.length).toEqual(1);
camelRequest.objectArray?.forEach((value) => {
expect(value.index).toEqual(0);
});
type PascalCaseConvertedObjectWithArray = ObjectToPascal<MyObjectWithArray>;
const pascalRequest: PascalCaseConvertedObjectWithArray = {
ObjectArray: [
{
Index: 0,
Value: 'abc',
},
],
};
expect(pascalRequest.ObjectArray?.length).toEqual(1);
pascalRequest.ObjectArray?.forEach((value) => {
expect(value.Index).toEqual(0);
});
interface MyPascalObjectWithArray {
ObjectArray?: {
Index: number;
Value?: string;
}[];
}
type SnakeCaseConvertedObjectWithArray =
ObjectToSnake<MyPascalObjectWithArray>;
const snakeRequest: SnakeCaseConvertedObjectWithArray = {
object_array: [
{
index: 0,
value: 'abc',
},
],
};
expect(snakeRequest.object_array?.length).toEqual(1);
snakeRequest.object_array?.forEach((value) => {
expect(value.index).toEqual(0);
});
});
it('#52 - objectToCamel return type is a Pascal-cased array when the input is a snake-typed array', () => {
type SnakeTyped = { key: string; another_key: string };
type CamelType = { key: string; anotherKey: string };
const snakeObject: SnakeTyped[] = [{ key: 'a', another_key: 'b' }];
const camelObject: CamelType[] = objectToCamel(snakeObject);
expect(Object.keys(camelObject[0])).toEqual(['key', 'anotherKey']);
expect(camelObject[0].key).toEqual('a');
expect(camelObject[0].anotherKey).toEqual('b');
});
it('#55 - objectToCamel return type is an array when the input is a snake-typed array', () => {
type SnakeTyped = { key: string; another_key: string };
type CamelType = { key: string; anotherKey: string };
const snakeObject: SnakeTyped[] = [{ key: 'a', another_key: 'b' }];
const camelArray: CamelType[] = objectToCamel(snakeObject);
expect(Array.isArray(camelArray)).toBe(true);
const camelScalarArray = objectToCamel([1]);
expect(Array.isArray(camelScalarArray)).toBe(true);
});
it('#58 - does not handle Buffer objects correctly', () => {
const snakeObject = { buffer_key: Buffer.from('abc'), nested: { more_nested: Buffer.from('abc') }, array: [Buffer.from('abc')] };
const convertedSnakeObj = objectToCamel(snakeObject);
expect(Buffer.isBuffer(convertedSnakeObj.bufferKey)).toBeTruthy();
expect(Buffer.isBuffer(convertedSnakeObj.nested.moreNested)).toBeTruthy();
expect(Buffer.isBuffer(convertedSnakeObj.array[0])).toBeTruthy();
const camelObject = { bufferKey: Buffer.from('abc'), nested: { moreNested: Buffer.from('abc') }, array: [Buffer.from('abc')] };
const convertedCamelObject = objectToSnake(camelObject);
expect(Buffer.isBuffer(convertedCamelObject.buffer_key)).toBeTruthy();
expect(Buffer.isBuffer(convertedCamelObject.nested.more_nested)).toBeTruthy();
expect(Buffer.isBuffer(convertedCamelObject.array[0])).toBeTruthy();
});
});
// Bug #50
interface ArrayTypes {
arrayOfString: string[];
optionalArrayOfString?: string[];
arrayOfArrayOfString: string[][];
optionalArrayOfArrayofString1?: string[][];
arrayOfOptionalArrayOfString: Array<string[] | undefined>;
}
type SnakeArrayTypes = ObjectToSnake<ArrayTypes>;
const _snakeArrays1: SnakeArrayTypes = {
array_of_string: ['a'],
array_of_array_of_string: [['a']],
array_of_optional_array_of_string: [['a']],
optional_array_of_string: ['a'],
};
const _snakeArrays2: SnakeArrayTypes = {
array_of_string: ['a'],
array_of_array_of_string: [['a']],
array_of_optional_array_of_string: [undefined],
optional_array_of_string: undefined,
};
const _camelArrays1: ObjectToCamel<ArrayTypes> = {
arrayOfString: ['a'],
arrayOfArrayOfString: [['a']],
arrayOfOptionalArrayOfString: [['a']],
optionalArrayOfString: ['a'],
};
const _camelArrays2: ObjectToCamel<ArrayTypes> = {
arrayOfString: ['a'],
arrayOfArrayOfString: [['a']],
arrayOfOptionalArrayOfString: [undefined],
optionalArrayOfString: undefined,
};