setDottedPath.js 585 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const specialProperties = require('../specialProperties');
  3. module.exports = function setDottedPath(obj, path, val) {
  4. if (path.indexOf('.') === -1) {
  5. if (specialProperties.has(path)) {
  6. return;
  7. }
  8. obj[path] = val;
  9. return;
  10. }
  11. const parts = path.split('.');
  12. const last = parts.pop();
  13. let cur = obj;
  14. for (const part of parts) {
  15. if (specialProperties.has(part)) {
  16. continue;
  17. }
  18. if (cur[part] == null) {
  19. cur[part] = {};
  20. }
  21. cur = cur[part];
  22. }
  23. if (!specialProperties.has(last)) {
  24. cur[last] = val;
  25. }
  26. };