time-to-botec

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

factory.js (2376B)


      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 dget = require( './dget.js' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns a function for retrieving a nested property value.
     36 *
     37 * @param {(string|Array)} path - key path
     38 * @param {Options} [options] - function options
     39 * @param {string} [options.sep='.'] - key path separator
     40 * @throws {TypeError} first argument must be a string primitive or key array
     41 * @throws {TypeError} options argument must be an object
     42 * @throws {TypeError} must provide valid options
     43 * @returns {Function} deep get function
     44 *
     45 * @example
     46 * var dget = factory( 'a/b/c', {
     47 *     'sep': '/'
     48 * });
     49 */
     50 function factory( path, options ) {
     51 	var isStr;
     52 	var props;
     53 	var opts;
     54 	var err;
     55 	isStr = isString( path );
     56 	if ( !isStr && !isArray( path ) ) {
     57 		throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' );
     58 	}
     59 	opts = copy( defaults );
     60 	if ( arguments.length > 1 ) {
     61 		err = validate( opts, options );
     62 		if ( err ) {
     63 			throw err;
     64 		}
     65 	}
     66 	if ( isStr ) {
     67 		props = path.split( opts.sep );
     68 	} else {
     69 		props = path;
     70 	}
     71 	return deepGet;
     72 
     73 	/**
     74 	* Returns a nested property value.
     75 	*
     76 	* @private
     77 	* @param {ObjectLike} obj - input object
     78 	* @returns {*} nested property value
     79 	*
     80 	* @example
     81 	* var obj = { 'a': { 'b': { 'c': 'd' } } };
     82 	* var val = deepGet( obj );
     83 	*/
     84 	function deepGet( obj ) {
     85 		if ( isObjectLike( obj ) ) {
     86 			return dget( obj, props );
     87 		}
     88 	}
     89 }
     90 
     91 
     92 // EXPORTS //
     93 
     94 module.exports = factory;