README.md (5310B)
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 # deepHasOwnProp 22 23 > Test whether an object contains a nested key path. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); 31 ``` 32 33 #### deepHasOwnProp( value, path\[, options] ) 34 35 Returns a `boolean` indicating if a `value` has a specified `path`. 36 37 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 38 39 ```javascript 40 var obj = { 'a': { 'b': { 'c': 'd' } } }; 41 42 var bool = deepHasOwnProp( obj, 'a.b.c' ); 43 // returns true 44 45 bool = deepHasOwnProp( obj, 'a.b.c.d.e' ); 46 // returns false 47 ``` 48 49 If a key path includes an `array`, specify the numeric index. 50 51 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 52 53 ```javascript 54 var arr = [ 55 { 56 'a': [ 57 { 58 'b': [ 59 { 'c': 'd' }, 60 { 'e': 'f' } 61 ] 62 } 63 ] 64 } 65 ]; 66 67 var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' ); 68 // returns true 69 70 bool = deepHasOwnProp( arr, '0.a.1.b.0.c' ); 71 // returns false 72 73 bool = deepHasOwnProp( arr, '0.a.0.b.1.c' ); 74 // returns false 75 ``` 76 77 The key path may be specified as either a delimited `string` or a key `array`. 78 79 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 80 81 ```javascript 82 var obj = { 'a': { 'b': { 'c': 'd' } } }; 83 84 var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); 85 // returns true 86 ``` 87 88 The function accepts the following `options`: 89 90 - **sep**: key path separator. Default: `'.'`. 91 92 By default, the function assumes `.` separated key values. To specify an alternative separator, set the `sep` option. 93 94 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 95 96 ```javascript 97 var obj = { 'a': { 'b': { 'c': 'd' } } }; 98 99 var bool = deepHasOwnProp( obj, 'a/b/c', { 100 'sep': '/' 101 }); 102 // returns true 103 ``` 104 105 #### deepHasOwnProp.factory( path\[, options] ) 106 107 Returns a `function` which tests whether a `value` contains a nested key `path`. 108 109 ```javascript 110 var has = deepHasOwnProp.factory( 'a/b/c', { 111 'sep': '/' 112 }); 113 ``` 114 115 #### has( value ) 116 117 Returns a `boolean` indicating whether a `value` contains a nested key `path`. 118 119 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 120 121 ```javascript 122 var has = deepHasOwnProp.factory( 'a.b.c' ); 123 124 var obj = { 'a': { 'b': { 'c': 'd' } } }; 125 126 var bool = has( obj ); 127 // returns true 128 ``` 129 130 </section> 131 132 <!-- /.usage --> 133 134 <section class="notes"> 135 136 ## Notes 137 138 - Only **own** properties are tested. Paths matching inherited properties are not considered. 139 140 ```javascript 141 function Foo() { 142 this.a = 'b'; 143 return this; 144 } 145 Foo.prototype.c = 'd'; 146 147 var foo = new Foo(); 148 149 var bool = deepHasOwnProp( foo, 'a' ); 150 // returns true 151 152 bool = deepHasOwnProp( foo, 'c' ); 153 // returns false 154 ``` 155 156 - When provided `null` or `undefined`, the function result is always `false`. 157 158 ```javascript 159 var bool = deepHasOwnProp( null, 'a.b.c' ); 160 // returns false 161 162 bool = deepHasOwnProp( void 0, 'a.b.c' ); 163 // returns false 164 ``` 165 166 - Property values other than `null` or `undefined` are coerced to `objects`. 167 168 ```javascript 169 var obj = { 170 'a': 'b' 171 }; 172 173 var bool = deepHasOwnProp( obj, 'a.length' ); 174 // returns true 175 ``` 176 177 - Key path `array` elements are coerced to `strings`. 178 179 ```javascript 180 var obj = { 181 'null': false 182 }; 183 var bool = deepHasOwnProp( obj, [ null ] ); 184 // returns true 185 186 obj = { 187 '[object Object]': false 188 }; 189 bool = deepHasOwnProp( obj, [ {} ] ); 190 // returns true 191 ``` 192 193 </section> 194 195 <!-- /.notes --> 196 197 <section class="examples"> 198 199 ## Examples 200 201 <!-- eslint-disable object-curly-newline, object-curly-spacing --> 202 203 <!-- eslint no-undef: "error" --> 204 205 ```javascript 206 var deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); 207 208 var bool; 209 var has; 210 211 bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c' ); 212 // returns true 213 214 bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, [ 'a', 'b', 'c' ] ); 215 // returns true 216 217 bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c', { 218 'sep': '/' 219 }); 220 // returns true 221 222 bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c.d' ); 223 // returns false 224 225 bool = deepHasOwnProp( { 'a': [ { 'b': { 'c': 'd' } } ] }, [ 'a', '0', 'b', 'c', 'd' ] ); 226 // returns false 227 228 bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c/d/e', { 229 'sep': '/' 230 }); 231 // returns false 232 233 // Create a customized function: 234 has = deepHasOwnProp.factory( 'a_b_c', { 235 'sep': '_' 236 }); 237 238 bool = has( { 'a': { 'b': { 'c': 'd' } } } ); 239 // returns true 240 241 bool = has( { 'a': [ { 'b': { 'c': 'd' } } ] } ); 242 // returns false 243 ``` 244 245 </section> 246 247 <!-- /.examples --> 248 249 <section class="links"> 250 251 </section> 252 253 <!-- /.links -->