time-to-botec

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

factory.js (2488B)


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