time-to-botec

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

main.js (2763B)


      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 * Tests whether an object contains a nested key path.
     35 *
     36 * @param {*} value - value to test
     37 * @param {(string|Array)} path - key path
     38 * @param {Options} [options] - function options
     39 * @param {string} [options.sep='.'] - key path separator
     40 * @throws {TypeError} second 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 {boolean} boolean indicating whether an object has a nested property
     44 *
     45 * @example
     46 * var obj = { 'a': { 'b': { 'c': 'd' } } };
     47 * var bool = deepHasOwnProp( obj, 'a.b.c' );
     48 * // returns true
     49 *
     50 * @example
     51 * var arr = [
     52 *     {
     53 *         'a': [
     54 *             {
     55 *                 'b': [
     56 *                     { 'c': 'd' },
     57 *                     { 'e': 'f' }
     58 *                 ]
     59 *             }
     60 *         ]
     61 *     }
     62 * ];
     63 * var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' );
     64 * // returns true
     65 *
     66 * @example
     67 * var obj = { 'a': { 'b': { 'c': 'd' } } };
     68 * var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] );
     69 * // returns true
     70 *
     71 * @example
     72 * var obj = { 'a': { 'b': { 'c': 'd' } } };
     73 * var bool = deepHasOwnProp( obj, 'a/b/c', {
     74 *     'sep': '/'
     75 * });
     76 * // returns true
     77 */
     78 function deepHasOwnProp( value, path, options ) {
     79 	var isStr;
     80 	var props;
     81 	var opts;
     82 	var err;
     83 
     84 	isStr = isString( path );
     85 	if ( !isStr && !isArray( path ) ) {
     86 		throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' );
     87 	}
     88 	opts = copy( defaults );
     89 	if ( arguments.length > 2 ) {
     90 		err = validate( opts, options );
     91 		if ( err ) {
     92 			throw err;
     93 		}
     94 	}
     95 	if ( value === void 0 || value === null ) {
     96 		return false;
     97 	}
     98 	if ( isStr ) {
     99 		props = path.split( opts.sep );
    100 	} else {
    101 		props = path;
    102 	}
    103 	return has( value, props );
    104 }
    105 
    106 
    107 // EXPORTS //
    108 
    109 module.exports = deepHasOwnProp;