time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (2620B)


      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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     24 var isArray = require( '@stdlib/assert/is-array' );
     25 var isObjectLike = require( '@stdlib/assert/is-object-like' );
     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 * Creates a reusable deep set function.
     36 *
     37 * @param {(string|Array)} path - key path
     38 * @param {Options} [options] - function options
     39 * @param {boolean} [options.create=false] - boolean indicating whether to create a path if the key path does not already exist
     40 * @param {string} [options.sep='.'] - key path separator
     41 * @throws {TypeError} first argument must be a string primitive or key array
     42 * @throws {TypeError} options argument must be an object
     43 * @throws {TypeError} must provide valid options
     44 * @returns {Function} deep set function
     45 *
     46 * @example
     47 * var dset = factory( 'a/b/c', {
     48 *     'create': true,
     49 *     'sep': '/'
     50 * });
     51 */
     52 function factory( path, options ) {
     53 	var isStr;
     54 	var props;
     55 	var opts;
     56 	var err;
     57 
     58 	isStr = isString( path );
     59 	if ( !isStr && !isArray( path ) ) {
     60 		throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' );
     61 	}
     62 	opts = copy( defaults );
     63 	if ( arguments.length > 1 ) {
     64 		err = validate( opts, options );
     65 		if ( err ) {
     66 			throw err;
     67 		}
     68 	}
     69 	if ( isStr ) {
     70 		props = path.split( opts.sep );
     71 	} else {
     72 		props = path;
     73 	}
     74 	return deepSet;
     75 
     76 	/**
     77 	* Sets a nested property.
     78 	*
     79 	* @private
     80 	* @param {ObjectLike} obj - input object
     81 	* @param {*} value - value to set
     82 	* @returns {boolean} boolean indicating if the property was successfully set
     83 	*
     84 	* @example
     85 	* var obj = { 'a': { 'b': { 'c': 'd' } } };
     86 	* var bool = dset( obj, 'beep' );
     87 	*/
     88 	function deepSet( obj, value ) {
     89 		if ( isObjectLike( obj ) ) {
     90 			return dset( obj, props, opts.create, value );
     91 		}
     92 		return false;
     93 	}
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = factory;