setDottedPath.js 357 B

1234567891011121314151617181920
  1. 'use strict';
  2. module.exports = function setDottedPath(obj, path, val) {
  3. if (path.indexOf('.') === -1) {
  4. obj[path] = val;
  5. return;
  6. }
  7. const parts = path.split('.');
  8. const last = parts.pop();
  9. let cur = obj;
  10. for (const part of parts) {
  11. if (cur[part] == null) {
  12. cur[part] = {};
  13. }
  14. cur = cur[part];
  15. }
  16. cur[last] = val;
  17. };