namespace.js 393 B

12345678910111213141516171819
  1. var forEach = require('../array/forEach');
  2. /**
  3. * Create nested object if non-existent
  4. */
  5. function namespace(obj, path){
  6. if (!path) return obj;
  7. forEach(path.split('.'), function(key){
  8. if (!obj[key]) {
  9. obj[key] = {};
  10. }
  11. obj = obj[key];
  12. });
  13. return obj;
  14. }
  15. module.exports = namespace;