README.md (5087B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # deepHasProp 22 23 > Test whether an object contains a nested key path, either own or inherited. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var deepHasProp = require( '@stdlib/assert/deep-has-property' ); 31 ``` 32 33 #### deepHasProp( value, path\[, options] ) 34 35 Returns a `boolean` indicating if a `value` has a specified `path`, either own or inherited. 36 37 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 38 39 ```javascript 40 function Foo() { 41 return this; 42 } 43 Foo.prototype.b = { 44 'c': 'd' 45 }; 46 47 var obj = { 'a': new Foo() }; 48 49 var bool = deepHasProp( obj, 'a.b.c' ); 50 // returns true 51 52 bool = deepHasProp( obj, 'a.b.c.d.e' ); 53 // returns false 54 ``` 55 56 If a key path includes an `array`, specify the numeric index. 57 58 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 59 60 ```javascript 61 var arr = [ 62 { 63 'a': [ 64 { 65 'b': [ 66 { 'c': 'd' }, 67 { 'e': 'f' } 68 ] 69 } 70 ] 71 } 72 ]; 73 74 var bool = deepHasProp( arr, '0.a.0.b.0.c' ); 75 // returns true 76 77 bool = deepHasProp( arr, '0.a.1.b.0.c' ); 78 // returns false 79 80 bool = deepHasProp( arr, '0.a.0.b.1.c' ); 81 // returns false 82 ``` 83 84 The key path may be specified as either a delimited `string` or a key `array`. 85 86 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 87 88 ```javascript 89 var obj = { 'a': { 'b': { 'c': 'd' } } }; 90 91 var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); 92 // returns true 93 ``` 94 95 The function accepts the following `options`: 96 97 - **sep**: key path separator. Default: `'.'`. 98 99 By default, the function assumes `.` separated key values. To specify an alternative separator, set the `sep` option. 100 101 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 102 103 ```javascript 104 var obj = { 'a': { 'b': { 'c': 'd' } } }; 105 106 var bool = deepHasProp( obj, 'a/b/c', { 107 'sep': '/' 108 }); 109 // returns true 110 ``` 111 112 #### deepHasProp.factory( path\[, options] ) 113 114 Returns a `function` which tests whether a `value` contains a nested key `path`, either own or inherited. 115 116 ```javascript 117 var has = deepHasProp.factory( 'a/b/c', { 118 'sep': '/' 119 }); 120 ``` 121 122 #### has( value ) 123 124 Returns a `boolean` indicating whether a `value` contains a nested key `path`, either own or inherited. 125 126 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 127 128 ```javascript 129 var has = deepHasProp.factory( 'a.b.c' ); 130 131 function Foo() { 132 return this; 133 } 134 Foo.prototype.b = { 135 'c': 'd' 136 }; 137 138 var obj = { 'a': new Foo() }; 139 140 var bool = has( obj ); 141 // returns true 142 ``` 143 144 </section> 145 146 <!-- /.usage --> 147 148 <section class="notes"> 149 150 ## Notes 151 152 - When provided `null` or `undefined`, the function result is always `false`. 153 154 ```javascript 155 var bool = deepHasProp( null, 'a.b.c' ); 156 // returns false 157 158 bool = deepHasProp( void 0, 'a.b.c' ); 159 // returns false 160 ``` 161 162 - Property values other than `null` or `undefined` are coerced to `objects`. 163 164 ```javascript 165 var obj = { 166 'a': 'b' 167 }; 168 169 var bool = deepHasProp( obj, 'a.length' ); 170 // returns true 171 ``` 172 173 - Key path `array` elements are coerced to `strings`. 174 175 ```javascript 176 var obj = { 177 'null': false 178 }; 179 var bool = deepHasProp( obj, [ null ] ); 180 // returns true 181 182 obj = { 183 '[object Object]': false 184 }; 185 bool = deepHasProp( obj, [ {} ] ); 186 // returns true 187 ``` 188 189 </section> 190 191 <!-- /.notes --> 192 193 <section class="examples"> 194 195 ## Examples 196 197 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 198 199 <!-- eslint no-undef: "error" --> 200 201 ```javascript 202 var deepHasProp = require( '@stdlib/assert/deep-has-property' ); 203 204 var bool; 205 var has; 206 207 bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c' ); 208 // returns true 209 210 bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, [ 'a', 'b', 'hasOwnProperty' ] ); 211 // returns true 212 213 bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c', { 214 'sep': '/' 215 }); 216 // returns true 217 218 bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c.d' ); 219 // returns false 220 221 bool = deepHasProp( { 'a': [ { 'b': { 'c': 'd' } } ] }, [ 'a', '0', 'b', 'c', 'd' ] ); 222 // returns false 223 224 bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c/d/e', { 225 'sep': '/' 226 }); 227 // returns false 228 229 // Create a customized function: 230 has = deepHasProp.factory( 'a_b_c', { 231 'sep': '_' 232 }); 233 234 bool = has( { 'a': { 'b': { 'c': 'd' } } } ); 235 // returns true 236 237 bool = has( { 'a': [ { 'b': { 'c': 'd' } } ] } ); 238 // returns false 239 ``` 240 241 </section> 242 243 <!-- /.examples --> 244 245 <section class="links"> 246 247 </section> 248 249 <!-- /.links -->