index.js 756 B

1234567891011121314151617181920212223242526272829303132
  1. /*!
  2. * unset-value <https://github.com/jonschlinkert/unset-value>
  3. *
  4. * Copyright (c) 2015, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var isObject = require('isobject');
  9. var has = require('has-value');
  10. module.exports = function unset(obj, prop) {
  11. if (!isObject(obj)) {
  12. throw new TypeError('expected an object.');
  13. }
  14. if (obj.hasOwnProperty(prop)) {
  15. delete obj[prop];
  16. return true;
  17. }
  18. if (has(obj, prop)) {
  19. var segs = prop.split('.');
  20. var last = segs.pop();
  21. while (segs.length && segs[segs.length - 1].slice(-1) === '\\') {
  22. last = segs.pop().slice(0, -1) + '.' + last;
  23. }
  24. while (segs.length) obj = obj[prop = segs.shift()];
  25. return (delete obj[last]);
  26. }
  27. return true;
  28. };