blob: a91901018c18297b1f8433c8b034d742fa0b72bb [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/*!
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
10var isObject = require('isobject');
11var has = require('has-value');
12
13module.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};