Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. // running the function with `objectA` and `expectedKeys`
  2. // should return `true`
  3. const objectA = {
  4. id: 2,
  5. name: 'Jane Doe',
  6. age: 34,
  7. city: 'Chicago',
  8. };
  9.  
  10. // running the function with `objectB` and `expectedKeys`
  11. // should return `false`
  12. const objectB = {
  13. id: 3,
  14. age: 33,
  15. city: 'Peoria',
  16. };
  17.  
  18. const objectC = {
  19. id: 9,
  20. name: 'Billy Bear',
  21. age: 62,
  22. city: 'Milwaukee',
  23. status: 'paused',
  24. };
  25.  
  26. const expectedKeys = ['id', 'name', 'age', 'city'];
  27.  
  28. function validateKeys(object, expectedKeys){
  29. if (Object.keys(object).length !== expectedKeys.length){
  30. return false;}
  31.  
  32.  
  33. for (let i = 0; i < expectedKeys.length; i++) {
  34. if (!Object.keys(object).find(key => key === expectedKeys[i])) {
  35. return false;
  36. }
  37. }
  38.  
  39. return true;
  40. }
  41.  
  42. /* From here down, you are not expected to
  43. understand.... for now :)
  44.  
  45.  
  46. Nothing to see here!
  47.  
  48. */
  49.  
  50. function testIt() {
  51. const objectA = {
  52. id: 2,
  53. name: 'Jane Doe',
  54. age: 34,
  55. city: 'Chicago',
  56. };
  57.  
  58. const objectB = {
  59. id: 3,
  60. age: 33,
  61. city: 'Peoria',
  62. };
  63.  
  64. const objectC = {
  65. id: 9,
  66. name: 'Billy Bear',
  67. age: 62,
  68. city: 'Milwaukee',
  69. status: 'paused',
  70. };
  71.  
  72. const objectD = {
  73. foo: 2,
  74. bar: 'Jane Doe',
  75. bizz: 34,
  76. bang: 'Chicago',
  77. };
  78.  
  79. const expectedKeys = ['id', 'name', 'age', 'city'];
  80.  
  81. if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') {
  82. console.error('FAILURE: validateKeys should return a boolean value');
  83. return;
  84. }
  85.  
  86. if (!validateKeys(objectA, expectedKeys)) {
  87. console.error(
  88. `FAILURE: running validateKeys with the following object and keys
  89. should return true but returned false:
  90. Object: ${JSON.stringify(objectA)}
  91. Expected keys: ${expectedKeys}`
  92. );
  93. return;
  94. }
  95.  
  96. if (validateKeys(objectB, expectedKeys)) {
  97. console.error(
  98. `FAILURE: running validateKeys with the following object and keys
  99. should return false but returned true:
  100. Object: ${JSON.stringify(objectB)}
  101. Expected keys: ${expectedKeys}`
  102. );
  103. return;
  104. }
  105.  
  106. if (validateKeys(objectC, expectedKeys)) {
  107. console.error(
  108. `FAILURE: running validateKeys with the following object and keys
  109. should return false but returned true:
  110. Object: ${JSON.stringify(objectC)}
  111. Expected keys: ${expectedKeys}`
  112. );
  113. return;
  114. }
  115.  
  116. if (validateKeys(objectD, expectedKeys)) {
  117. console.error(
  118. `FAILURE: running validateKeys with the following object and keys
  119. should return false but returned true:
  120. Object: ${JSON.stringify(objectD)}
  121. Expected keys: ${expectedKeys}`
  122. );
  123. return;
  124. }
  125.  
  126. console.log('SUCCESS: validateKeys is working');
  127. }
  128.  
  129. testIt();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement