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

objectUtils.test.ts 1.6 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
  1. import { removeEmpty } from '../../src/util/objectUtils';
  2. describe('objectUtils', () => {
  3. describe('removeEmpty', () => {
  4. it('removes empty arrays from the top level', () => {
  5. const input = {
  6. name: 'test',
  7. emptyArray: [],
  8. nonEmptyArray: [1, 2, 3],
  9. };
  10. const result = removeEmpty(input);
  11. expect(result).toEqual({
  12. name: 'test',
  13. nonEmptyArray: [1, 2, 3],
  14. });
  15. expect(result.emptyArray).toBeUndefined();
  16. });
  17. it('removes empty objects from the top level', () => {
  18. const input = {
  19. name: 'test',
  20. emptyObj: {},
  21. nonEmptyObj: { key: 'value' },
  22. };
  23. const result = removeEmpty(input);
  24. expect(result).toEqual({
  25. name: 'test',
  26. nonEmptyObj: { key: 'value' },
  27. });
  28. expect(result.emptyObj).toBeUndefined();
  29. });
  30. it('does not remove empty arrays or objects in nested objects', () => {
  31. const input = {
  32. level1: {
  33. emptyArr: [],
  34. emptyObj: {},
  35. validKey: 'value',
  36. },
  37. };
  38. const result = removeEmpty(input);
  39. expect(result).toEqual({
  40. level1: {
  41. emptyArr: [],
  42. emptyObj: {},
  43. validKey: 'value',
  44. },
  45. });
  46. });
  47. it('does not modify the original object', () => {
  48. const original = {
  49. name: 'test',
  50. emptyArray: [],
  51. nested: { empty: {} },
  52. };
  53. const originalCopy = JSON.parse(JSON.stringify(original));
  54. removeEmpty(original);
  55. expect(original).toEqual(originalCopy);
  56. });
  57. });
  58. });
Tip!

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

Comments

Loading...