main.js (2854B)
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, either own or inherited. 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 * function Foo() { 47 * return this; 48 * } 49 * Foo.prototype.b = { 50 * 'c': 'd' 51 * }; 52 * 53 * var obj = { 54 * 'a': new Foo() 55 * }; 56 * 57 * var bool = deepHasProp( obj, 'a.b.c' ); 58 * // returns true 59 * 60 * @example 61 * var arr = [ 62 * { 63 * 'a': [ 64 * { 65 * 'b': [ 66 * { 'c': 'd' }, 67 * { 'e': 'f' } 68 * ] 69 * } 70 * ] 71 * } 72 * ]; 73 * var bool = deepHasProp( arr, '0.a.0.b.0.c' ); 74 * // returns true 75 * 76 * @example 77 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 78 * var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); 79 * // returns true 80 * 81 * @example 82 * var obj = { 'a': { 'b': { 'c': 'd' } } }; 83 * var bool = deepHasProp( obj, 'a/b/c', { 84 * 'sep': '/' 85 * }); 86 * // returns true 87 */ 88 function deepHasProp( value, path, options ) { 89 var isStr; 90 var props; 91 var opts; 92 var err; 93 94 isStr = isString( path ); 95 if ( !isStr && !isArray( path ) ) { 96 throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); 97 } 98 opts = copy( defaults ); 99 if ( arguments.length > 2 ) { 100 err = validate( opts, options ); 101 if ( err ) { 102 throw err; 103 } 104 } 105 if ( value === void 0 || value === null ) { 106 return false; 107 } 108 if ( isStr ) { 109 props = path.split( opts.sep ); 110 } else { 111 props = path; 112 } 113 return has( value, props ); 114 } 115 116 117 // EXPORTS // 118 119 module.exports = deepHasProp;