Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

util.json.test.ts 8.1 KB

You have to be logged in to leave a comment. Sign In
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  1. import dedent from 'dedent';
  2. import { extractJsonObjects, isValidJson, safeJsonStringify, orderKeys } from '../src/util/json';
  3. describe('json utilities', () => {
  4. describe('isValidJson', () => {
  5. it('returns true for valid JSON', () => {
  6. expect(isValidJson('{"key": "value"}')).toBe(true);
  7. expect(isValidJson('[1, 2, 3]')).toBe(true);
  8. expect(isValidJson('"string"')).toBe(true);
  9. expect(isValidJson('123')).toBe(true);
  10. expect(isValidJson('true')).toBe(true);
  11. expect(isValidJson('null')).toBe(true);
  12. });
  13. it('returns false for invalid JSON', () => {
  14. expect(isValidJson('{')).toBe(false);
  15. expect(isValidJson('["unclosed array"')).toBe(false);
  16. expect(isValidJson('{"key": value}')).toBe(false);
  17. expect(isValidJson('undefined')).toBe(false);
  18. });
  19. });
  20. describe('safeJsonStringify', () => {
  21. it('stringifies simple objects', () => {
  22. const obj = { key: 'value', number: 123 };
  23. expect(safeJsonStringify(obj)).toBe('{"key":"value","number":123}');
  24. });
  25. it('handles circular references', () => {
  26. const obj: any = { key: 'value' };
  27. obj.circular = obj;
  28. expect(safeJsonStringify(obj)).toBe('{"key":"value"}');
  29. });
  30. it('pretty prints when specified', () => {
  31. const obj = { key: 'value', nested: { inner: 'content' } };
  32. const expected = dedent`
  33. {
  34. "key": "value",
  35. "nested": {
  36. "inner": "content"
  37. }
  38. }`;
  39. expect(safeJsonStringify(obj, true)).toBe(expected);
  40. });
  41. it('handles arrays with circular references', () => {
  42. const arr: any[] = [1, 2, 3];
  43. arr.push(arr);
  44. expect(safeJsonStringify(arr)).toBe('[1,2,3,null]');
  45. });
  46. it('handles null values', () => {
  47. expect(safeJsonStringify(null)).toBe('null');
  48. });
  49. it('handles undefined values', () => {
  50. expect(safeJsonStringify(undefined)).toBeUndefined();
  51. });
  52. it('handles complex nested structures', () => {
  53. const complex = {
  54. string: 'value',
  55. number: 123,
  56. boolean: true,
  57. null: null,
  58. array: [1, 'two', { three: 3 }],
  59. nested: {
  60. a: 1,
  61. b: [2, 3],
  62. },
  63. };
  64. const result = safeJsonStringify(complex);
  65. expect(JSON.parse(result)).toEqual(complex);
  66. });
  67. it('handles nested circular references', () => {
  68. const obj: any = { a: { b: {} } };
  69. obj.a.b.c = obj.a;
  70. expect(safeJsonStringify(obj)).toBe('{"a":{"b":{}}}');
  71. });
  72. it('preserves non-circular nested structures', () => {
  73. const nested = { a: { b: { c: 1 } }, d: [1, 2, { e: 3 }] };
  74. expect(JSON.parse(safeJsonStringify(nested))).toEqual(nested);
  75. });
  76. });
  77. describe('extractJsonObjects', () => {
  78. it('should extract a single JSON object from a string', () => {
  79. const input = '{"key": "value"}';
  80. const expectedOutput = [{ key: 'value' }];
  81. expect(extractJsonObjects(input)).toEqual(expectedOutput);
  82. });
  83. it('should extract multiple JSON objects from a string', () => {
  84. const input = 'yolo {"key1": "value1"} some text {"key2": "value2"} fomo';
  85. const expectedOutput = [{ key1: 'value1' }, { key2: 'value2' }];
  86. expect(extractJsonObjects(input)).toEqual(expectedOutput);
  87. });
  88. it('should return an empty array if no JSON objects are found', () => {
  89. const input = 'no json here';
  90. const expectedOutput: any[] = [];
  91. expect(extractJsonObjects(input)).toEqual(expectedOutput);
  92. });
  93. it('should handle nested JSON objects', () => {
  94. const input = 'wassup {"outer": {"inner": "value"}, "foo": [1,2,3,4]}';
  95. const expectedOutput = [{ outer: { inner: 'value' }, foo: [1, 2, 3, 4] }];
  96. expect(extractJsonObjects(input)).toEqual(expectedOutput);
  97. });
  98. it('should handle invalid JSON gracefully', () => {
  99. const input = '{"key": "value" some text {"key2": "value2"}';
  100. const expectedOutput = [{ key2: 'value2' }];
  101. expect(extractJsonObjects(input)).toEqual(expectedOutput);
  102. });
  103. it('should handle incomplete JSON', () => {
  104. const input = `{
  105. "incomplete": "object"`;
  106. expect(extractJsonObjects(input)).toEqual([]);
  107. });
  108. it('should handle string containing incomplete JSON', () => {
  109. const input = `{
  110. "key1": "value1",
  111. "key2": {
  112. "nested": "value2"
  113. },
  114. "key3": "value3"
  115. }
  116. {
  117. "incomplete": "object"`;
  118. expect(extractJsonObjects(input)).toEqual([
  119. {
  120. key1: 'value1',
  121. key2: {
  122. nested: 'value2',
  123. },
  124. key3: 'value3',
  125. },
  126. ]);
  127. });
  128. it('should handle this case', () => {
  129. const obj = {
  130. vars: [
  131. {
  132. language: 'Klingon',
  133. body: 'Live long and prosper',
  134. },
  135. {
  136. language: 'Elvish',
  137. body: 'Good morning',
  138. },
  139. {
  140. language: 'Esperanto',
  141. body: 'I love learning languages',
  142. },
  143. {
  144. language: 'Morse Code',
  145. body: 'Help',
  146. },
  147. {
  148. language: 'Emoji',
  149. body: 'I am feeling happy 😊',
  150. },
  151. {
  152. language: 'Binary',
  153. body: 'Yes',
  154. },
  155. {
  156. language: 'Javascript',
  157. body: 'Hello, World!',
  158. },
  159. {
  160. language: 'Shakespearean',
  161. body: 'To be or not to be',
  162. },
  163. {
  164. language: 'Leet Speak',
  165. body: 'You are amazing',
  166. },
  167. {
  168. language: 'Old English',
  169. body: 'What is thy name?',
  170. },
  171. {
  172. language: 'Yoda Speak',
  173. body: 'Strong with the force, you are',
  174. },
  175. ],
  176. };
  177. const input = JSON.stringify(obj);
  178. expect(extractJsonObjects(input)).toEqual([obj]);
  179. });
  180. });
  181. describe('orderKeys', () => {
  182. it('orders keys according to specified order', () => {
  183. const obj = { c: 3, a: 1, b: 2 };
  184. const order: (keyof typeof obj)[] = ['a', 'b', 'c'];
  185. const result = orderKeys(obj, order);
  186. expect(Object.keys(result)).toEqual(['a', 'b', 'c']);
  187. });
  188. it('places unspecified keys at the end', () => {
  189. const obj = { d: 4, b: 2, a: 1, c: 3 };
  190. const order: (keyof typeof obj)[] = ['a', 'b'];
  191. const result = orderKeys(obj, order);
  192. expect(Object.keys(result)).toEqual(['a', 'b', 'd', 'c']);
  193. });
  194. it('ignores specified keys that do not exist in the object', () => {
  195. const obj = { a: 1, c: 3 };
  196. const order = ['a', 'b', 'c', 'd'] as (keyof typeof obj)[];
  197. const result = orderKeys(obj, order);
  198. expect(Object.keys(result)).toEqual(['a', 'c']);
  199. });
  200. it('returns an empty object when input is empty', () => {
  201. const obj = {};
  202. const order = ['a', 'b', 'c'] as (keyof typeof obj)[];
  203. const result = orderKeys(obj, order);
  204. expect(result).toEqual({});
  205. });
  206. it('returns the original object when order is empty', () => {
  207. const obj = { c: 3, a: 1, b: 2 };
  208. const order: (keyof typeof obj)[] = [];
  209. const result = orderKeys(obj, order);
  210. expect(result).toEqual(obj);
  211. });
  212. it('preserves nested object structures', () => {
  213. const obj = { c: { x: 1 }, a: [1, 2], b: 2 };
  214. const order: (keyof typeof obj)[] = ['a', 'b', 'c'];
  215. const result = orderKeys(obj, order);
  216. expect(result).toEqual({ a: [1, 2], b: 2, c: { x: 1 } });
  217. });
  218. it('handles objects with symbol keys', () => {
  219. const sym1 = Symbol('sym1');
  220. const sym2 = Symbol('sym2');
  221. const obj = { [sym1]: 1, b: 2, [sym2]: 3, a: 4 };
  222. const order: (keyof typeof obj)[] = ['a', 'b'];
  223. const result = orderKeys(obj, order);
  224. expect(Object.getOwnPropertySymbols(result)).toEqual([sym1, sym2]);
  225. expect(Object.keys(result)).toEqual(['a', 'b']);
  226. });
  227. it('maintains the correct types for keys and values', () => {
  228. const obj = { a: 'string', b: 42, c: true, d: null, e: undefined };
  229. const order: (keyof typeof obj)[] = ['b', 'a', 'c', 'd', 'e'];
  230. const result = orderKeys(obj, order);
  231. expect(result).toEqual({ b: 42, a: 'string', c: true, d: null, e: undefined });
  232. });
  233. });
  234. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...