index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const isObj = require('is-obj');
  3. const disallowedKeys = [
  4. '__proto__',
  5. 'prototype',
  6. 'constructor'
  7. ];
  8. const isValidPath = pathSegments => !pathSegments.some(segment => disallowedKeys.includes(segment));
  9. function getPathSegments(path) {
  10. const pathArray = path.split('.');
  11. const parts = [];
  12. for (let i = 0; i < pathArray.length; i++) {
  13. let p = pathArray[i];
  14. while (p[p.length - 1] === '\\' && pathArray[i + 1] !== undefined) {
  15. p = p.slice(0, -1) + '.';
  16. p += pathArray[++i];
  17. }
  18. parts.push(p);
  19. }
  20. if (!isValidPath(parts)) {
  21. return [];
  22. }
  23. return parts;
  24. }
  25. module.exports = {
  26. get(object, path, value) {
  27. if (!isObj(object) || typeof path !== 'string') {
  28. return value === undefined ? object : value;
  29. }
  30. const pathArray = getPathSegments(path);
  31. if (pathArray.length === 0) {
  32. return;
  33. }
  34. for (let i = 0; i < pathArray.length; i++) {
  35. if (!Object.prototype.propertyIsEnumerable.call(object, pathArray[i])) {
  36. return value;
  37. }
  38. object = object[pathArray[i]];
  39. if (object === undefined || object === null) {
  40. // `object` is either `undefined` or `null` so we want to stop the loop, and
  41. // if this is not the last bit of the path, and
  42. // if it did't return `undefined`
  43. // it would return `null` if `object` is `null`
  44. // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null`
  45. if (i !== pathArray.length - 1) {
  46. return value;
  47. }
  48. break;
  49. }
  50. }
  51. return object;
  52. },
  53. set(object, path, value) {
  54. if (!isObj(object) || typeof path !== 'string') {
  55. return object;
  56. }
  57. const root = object;
  58. const pathArray = getPathSegments(path);
  59. for (let i = 0; i < pathArray.length; i++) {
  60. const p = pathArray[i];
  61. if (!isObj(object[p])) {
  62. object[p] = {};
  63. }
  64. if (i === pathArray.length - 1) {
  65. object[p] = value;
  66. }
  67. object = object[p];
  68. }
  69. return root;
  70. },
  71. delete(object, path) {
  72. if (!isObj(object) || typeof path !== 'string') {
  73. return false;
  74. }
  75. const pathArray = getPathSegments(path);
  76. for (let i = 0; i < pathArray.length; i++) {
  77. const p = pathArray[i];
  78. if (i === pathArray.length - 1) {
  79. delete object[p];
  80. return true;
  81. }
  82. object = object[p];
  83. if (!isObj(object)) {
  84. return false;
  85. }
  86. }
  87. },
  88. has(object, path) {
  89. if (!isObj(object) || typeof path !== 'string') {
  90. return false;
  91. }
  92. const pathArray = getPathSegments(path);
  93. if (pathArray.length === 0) {
  94. return false;
  95. }
  96. // eslint-disable-next-line unicorn/no-for-loop
  97. for (let i = 0; i < pathArray.length; i++) {
  98. if (isObj(object)) {
  99. if (!(pathArray[i] in object)) {
  100. return false;
  101. }
  102. object = object[pathArray[i]];
  103. } else {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. };