index.js
 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  
 8  'use strict';
 9  
10  var isObject = require('isobject');
11  var has = require('has-value');
12  
13  module.exports = function unset(obj, prop) {
14    if (!isObject(obj)) {
15      throw new TypeError('expected an object.');
16    }
17    if (obj.hasOwnProperty(prop)) {
18      delete obj[prop];
19      return true;
20    }
21  
22    if (has(obj, prop)) {
23      var segs = prop.split('.');
24      var last = segs.pop();
25      while (segs.length && segs[segs.length - 1].slice(-1) === '\\') {
26        last = segs.pop().slice(0, -1) + '.' + last;
27      }
28      while (segs.length) obj = obj[prop = segs.shift()];
29      return (delete obj[last]);
30    }
31    return true;
32  };