Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. mocha.setup('bdd');
  2. const expect = chai.expect;
  3.  
  4. describe('flatten()', function () {
  5.  
  6.  
  7. // empty object
  8. // array not inside a root object
  9. //
  10.  
  11. it('should flatten a simple nested object', function () {
  12. const source = {
  13. a: 1,
  14. b: 2
  15. };
  16.  
  17. const expected = {
  18. 'a': 1,
  19. 'b': 2
  20. };
  21.  
  22. expect(flatten(source)).to.deep.equal(expected);
  23. });
  24.  
  25. it('should flatten a simple nested object', function () {
  26. const source = {
  27. foo: {
  28. bar: 1
  29. }
  30. };
  31.  
  32. const expected = {
  33. 'foo.bar': 1
  34. };
  35.  
  36. expect(flatten(source)).to.deep.equal(expected);
  37. });
  38.  
  39. it.only('should flatten an object with nested arrays', function () {
  40. const source = {
  41. foo: [{
  42. bar: 1
  43. }, {
  44. bar: 2
  45. }]
  46. };
  47.  
  48. const expected = {
  49. 'foo.0.bar': 1,
  50. 'foo.1.bar': 2
  51. };
  52.  
  53. expect(flatten(source)).to.deep.equal(expected);
  54. });
  55.  
  56. it('should flatten a complex nested object', function () {
  57. const source = {
  58. a: 1,
  59. b: {
  60. c: true,
  61. d: {
  62. e: 'foo'
  63. }
  64. },
  65. f: false,
  66. g: ['red', 'green', 'blue'],
  67. h: [{
  68. i: 2,
  69. j: 3
  70. }]
  71. };
  72.  
  73. const expected = {
  74. 'a': 1,
  75. 'b.c': true,
  76. 'b.d.e': 'foo',
  77. 'f': false,
  78. 'g.0': 'red',
  79. 'g.1': 'green',
  80. 'g.2': 'blue',
  81. 'h.0.i': 2,
  82. 'h.0.j': 3
  83. };
  84.  
  85. expect(flatten(source)).to.deep.equal(expected);
  86. });
  87.  
  88. it('should flatten an object with null values', function () {
  89. const source = {
  90. foo: {
  91. bar: null
  92. }
  93. };
  94.  
  95. const expected = {
  96. 'foo.bar': null
  97. };
  98.  
  99. expect(flatten(source)).to.deep.equal(expected);
  100. });
  101. });
  102.  
  103. mocha.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement