unset.js 490 B

1234567891011121314151617181920212223
  1. var has = require('./has');
  2. /**
  3. * Unset object property.
  4. */
  5. function unset(obj, prop){
  6. if (has(obj, prop)) {
  7. var parts = prop.split('.'),
  8. last = parts.pop();
  9. while (prop = parts.shift()) {
  10. obj = obj[prop];
  11. }
  12. return (delete obj[last]);
  13. } else {
  14. // if property doesn't exist treat as deleted
  15. return true;
  16. }
  17. }
  18. module.exports = unset;