deep_set.js (3225B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isObjectLike = require( '@stdlib/assert/is-object-like' ); 24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 25 var isArray = require( '@stdlib/assert/is-array' ); 26 var copy = require( './../../copy' ); 27 var validate = require( './validate.js' ); 28 var defaults = require( './defaults.json' ); 29 var dset = require( './dset.js' ); 30 31 32 // MAIN // 33 34 /** 35 * Sets a nested property value. 36 * 37 * @param {ObjectLike} obj - input object 38 * @param {(string|Array)} path - key path 39 * @param {*} value - value to set 40 * @param {Options} [options] - function options 41 * @param {boolean} [options.create=false] - boolean indicating whether to create a path if the key path does not already exist 42 * @param {string} [options.sep='.'] - key path separator 43 * @throws {TypeError} second argument must be a string primitive or key array 44 * @throws {TypeError} options argument must be an object 45 * @throws {TypeError} must provide valid options 46 * @returns {boolean} boolean indicating if the property was successfully set 47 * 48 * @example 49 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 50 * var bool = deepSet( obj, 'a.b.c', 'woot' ); 51 * // returns true 52 * 53 * @example 54 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 55 * var bool = deepSet( obj, 'a.beep.c', 'boop' ); 56 * // returns false 57 * 58 * @example 59 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 60 * var bool = deepSet( null, 'a.beep.c', 'boop' ); 61 * // returns false 62 * 63 * @example 64 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 65 * bool = deepSet( 'bap', 'a.beep.c', 'boop' ); 66 * // returns false 67 * 68 * @example 69 * var arr = [ 70 * { 'a': [ {'x': 5} ] }, 71 * { 'a': [ {'x': 10} ] } 72 * ]; 73 * var bool = deepSet( arr, '1.a.0.x', 25 ); 74 * // returns true 75 * 76 * @example 77 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 78 * var bool = deepSet( obj, 'a/b/c', 'beep', { 79 * 'sep': '/' 80 * }); 81 * // returns true 82 * 83 * @example 84 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 85 * var bool = deepSet( obj, 'a.e.c', 'boop', { 86 * 'create': true 87 * }); 88 * // returns true 89 */ 90 function deepSet( obj, path, value, options ) { 91 var isStr; 92 var props; 93 var opts; 94 var err; 95 if ( !isObjectLike( obj ) ) { 96 return false; 97 } 98 isStr = isString( path ); 99 if ( !isStr && !isArray( path ) ) { 100 throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); 101 } 102 opts = copy( defaults ); 103 if ( arguments.length > 3 ) { 104 err = validate( opts, options ); 105 if ( err ) { 106 throw err; 107 } 108 } 109 if ( isStr ) { 110 props = path.split( opts.sep ); 111 } else { 112 props = path; 113 } 114 return dset( obj, props, opts.create, value ); 115 } 116 117 118 // EXPORTS // 119 120 module.exports = deepSet;