Skip to content

Commit bfbb4d1

Browse files
Neerajpathak07kgryte
authored andcommitted
feat: add object/deep-get
Ref: #8755
1 parent f477420 commit bfbb4d1

17 files changed

Lines changed: 1715 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
# Deep Get
22+
23+
> Get a nested property value.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var deepGet = require( '@stdlib/object/deep-get' );
31+
```
32+
33+
#### deepGet( obj, path\[, options] )
34+
35+
Returns a nested property value.
36+
37+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
38+
39+
```javascript
40+
var obj = { 'a': { 'b': { 'c': 'd' } } };
41+
42+
var val = deepGet( obj, 'a.b.c' );
43+
// returns 'd'
44+
```
45+
46+
For `paths` including `arrays`, specify the numeric index.
47+
48+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
49+
50+
```javascript
51+
var arr = [
52+
{ 'a': [ {'x': 5} ] },
53+
{ 'a': [ {'x': 10} ] }
54+
];
55+
56+
var val = deepGet( arr, '1.a.0.x' );
57+
// returns 10
58+
```
59+
60+
The key `path` may be specified as either a delimited `string` or a key `array`.
61+
62+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
63+
64+
```javascript
65+
var obj = { 'a': { 'b': { 'c': 'd' } } };
66+
67+
var val = deepGet( obj, [ 'a', 'b', 'c' ] );
68+
// returns 'd'
69+
```
70+
71+
The function accepts the following `options`:
72+
73+
- **sep**: key path separator. Default: `'.'`.
74+
75+
By default, the function assumes `dot` separated key values. To specify an alternative separator, set the `sep` option.
76+
77+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
78+
79+
```javascript
80+
var obj = { 'a': { 'b': { 'c': 'd' } } };
81+
82+
var val = deepGet( obj, 'a/b/c', {
83+
'sep': '/'
84+
});
85+
// returns 'd'
86+
```
87+
88+
#### deepGet.factory( path\[, options] )
89+
90+
Creates a reusable deep get function. The factory method ensures a `deepGet` function is configured identically by using the same set of provided `options`.
91+
92+
```javascript
93+
var dget = deepGet.factory( 'a/b/c', {
94+
'sep': '/'
95+
});
96+
```
97+
98+
#### dget( obj )
99+
100+
Returns a nested property value.
101+
102+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
103+
104+
```javascript
105+
var dget = deepGet.factory( 'a.b.c' );
106+
107+
var obj = { 'a': { 'b': { 'c': 'd' } } };
108+
109+
var val = dget( obj );
110+
// returns 'd'
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var randu = require( '@stdlib/random/base/randu' );
125+
var deepGet = require( '@stdlib/object/deep-get' );
126+
127+
var data;
128+
var keys;
129+
var val;
130+
var i;
131+
132+
data = new Array( 100 );
133+
for ( i = 0; i < data.length; i++ ) {
134+
data[ i ] = {
135+
'x': Date.now(),
136+
'y': [ randu(), randu(), i ]
137+
};
138+
}
139+
140+
keys = [ 0, 'y', 2 ];
141+
for ( i = 0; i < data.length; i++ ) {
142+
keys[ 0 ] = i;
143+
val = deepGet( data, keys );
144+
console.log( val );
145+
}
146+
```
147+
148+
</section>
149+
150+
<!-- /.examples -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
* * *
157+
158+
## See Also
159+
160+
- <span class="package-name">[`@stdlib/utils/deep-pluck`][@stdlib/utils/deep-pluck]</span><span class="delimiter">: </span><span class="description">extract a nested property value from each element of an object array.</span>
161+
- <span class="package-name">[`@stdlib/utils/deep-set`][@stdlib/utils/deep-set]</span><span class="delimiter">: </span><span class="description">set a nested property value.</span>
162+
163+
</section>
164+
165+
<!-- /.related -->
166+
167+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="links">
170+
171+
<!-- <related-links> -->
172+
173+
[@stdlib/utils/deep-pluck]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/deep-pluck
174+
175+
[@stdlib/utils/deep-set]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/deep-set
176+
177+
<!-- </related-links> -->
178+
179+
</section>
180+
181+
<!-- /.links -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var pkg = require( './../package.json' ).name;
27+
var deepGet = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var arr;
34+
var obj;
35+
var out;
36+
var i;
37+
38+
obj = {
39+
'a': {
40+
'b': {
41+
'c': {
42+
'd': [ 0.0, 0.5, 1.0 ]
43+
}
44+
}
45+
}
46+
};
47+
arr = obj.a.b.c.d;
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
arr[ 1 ] = randu();
51+
out = deepGet( obj, 'a.b.c.d' );
52+
if ( typeof out !== 'object' ) {
53+
b.fail( 'should return an array' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isArray( out ) ) {
58+
b.fail( 'should return an array' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg+'::paths_array', function benchmark( b ) {
65+
var paths;
66+
var arr;
67+
var obj;
68+
var out;
69+
var i;
70+
71+
obj = {
72+
'a': {
73+
'b': {
74+
'c': {
75+
'd': [ 0.0, 0.5, 1.0 ]
76+
}
77+
}
78+
}
79+
};
80+
paths = [ 'a', 'b', 'c', 'd' ];
81+
arr = obj.a.b.c.d;
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
arr[ 1 ] = randu();
85+
out = deepGet( obj, paths );
86+
if ( typeof out !== 'object' ) {
87+
b.fail( 'should return an array' );
88+
}
89+
}
90+
b.toc();
91+
if ( !isArray( out ) ) {
92+
b.fail( 'should return an array' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
});
97+
98+
bench( pkg+':factory', function benchmark( b ) {
99+
var dget;
100+
var arr;
101+
var obj;
102+
var out;
103+
var i;
104+
105+
obj = {
106+
'a': {
107+
'b': {
108+
'c': {
109+
'd': [ 0.0, 0.5, 1.0 ]
110+
}
111+
}
112+
}
113+
};
114+
dget = deepGet.factory( 'a.b.c.d' );
115+
arr = obj.a.b.c.d;
116+
b.tic();
117+
for ( i = 0; i < b.iterations; i++ ) {
118+
arr[ 1 ] = randu();
119+
out = dget( obj );
120+
if ( typeof out !== 'object' ) {
121+
b.fail( 'should return an array' );
122+
}
123+
}
124+
b.toc();
125+
if ( !isArray( out ) ) {
126+
b.fail( 'should return an array' );
127+
}
128+
b.pass( 'benchmark finished' );
129+
b.end();
130+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
{{alias}}( obj, path[, options] )
3+
Returns a nested property value.
4+
5+
Parameters
6+
----------
7+
obj: ObjectLike
8+
Input object.
9+
10+
path: string|Array
11+
Key path.
12+
13+
options: Object (optional)
14+
Options.
15+
16+
options.sep: string (optional)
17+
Key path separator. Default: '.'.
18+
19+
Returns
20+
-------
21+
out: any
22+
Nested property value.
23+
24+
Examples
25+
--------
26+
> var obj = { 'a': { 'b': { 'c': 'd' } } };
27+
> var val = {{alias}}( obj, 'a.b.c' )
28+
'd'
29+
30+
// Specify a custom separator via the `sep` option:
31+
> var obj = { 'a': { 'b': { 'c': 'd' } } };
32+
> var val = {{alias}}( obj, 'a/b/c', { 'sep': '/' } )
33+
'd'
34+
35+
{{alias}}.factory( path[, options] )
36+
Creates a reusable deep get function.
37+
38+
Parameters
39+
----------
40+
path: string|Array
41+
Key path.
42+
43+
options: Object (optional)
44+
Options.
45+
46+
options.sep: string (optional)
47+
Key path separator. Default: '.'.
48+
49+
Returns
50+
-------
51+
out: Function
52+
Deep get factory.
53+
54+
Examples
55+
--------
56+
> var dget = {{alias}}.factory( 'a/b/c', { 'sep': '/' } );
57+
> var obj = { 'a': { 'b': { 'c': 'd' } } };
58+
> var val = dget( obj )
59+
'd'
60+
61+
See Also
62+
--------
63+

0 commit comments

Comments
 (0)