Skip to content

Commit c32cefa

Browse files
committed
Auto-generated commit
1 parent b085597 commit c32cefa

11 files changed

Lines changed: 944 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`6514036`](https://github.com/stdlib-js/stdlib/commit/6514036b042522b853e8c77111fc8b7c2eaf42cc) - add `utils/json-pointer/get` [(#16245)](https://github.com/stdlib-js/stdlib/pull/16245)
1314
- [`14e0c0a`](https://github.com/stdlib-js/stdlib/commit/14e0c0a39ad89aa89893c929bb24675fd6047e13) - add `utils/json-pointer/format` [(#15925)](https://github.com/stdlib-js/stdlib/pull/15925)
1415
- [`a5f29ac`](https://github.com/stdlib-js/stdlib/commit/a5f29acf8a9479c642edd3a59a8ba8ca10b77b26) - add `utils/json-pointer/resolve` [(#15854)](https://github.com/stdlib-js/stdlib/pull/15854)
1516
- [`a9780f1`](https://github.com/stdlib-js/stdlib/commit/a9780f1bd8903e6946f3d565d929a26ba4356970) - add `utils/json-pointer/parse` [(#14824)](https://github.com/stdlib-js/stdlib/pull/14824)
@@ -49,6 +50,7 @@ A total of 10 issues were closed in this release:
4950

5051
<details>
5152

53+
- [`6514036`](https://github.com/stdlib-js/stdlib/commit/6514036b042522b853e8c77111fc8b7c2eaf42cc) - **feat:** add `utils/json-pointer/get` [(#16245)](https://github.com/stdlib-js/stdlib/pull/16245) _(by Gururaj Gurram, Athan Reines)_
5254
- [`14e0c0a`](https://github.com/stdlib-js/stdlib/commit/14e0c0a39ad89aa89893c929bb24675fd6047e13) - **feat:** add `utils/json-pointer/format` [(#15925)](https://github.com/stdlib-js/stdlib/pull/15925) _(by Gururaj Gurram, Athan Reines)_
5355
- [`a5f29ac`](https://github.com/stdlib-js/stdlib/commit/a5f29acf8a9479c642edd3a59a8ba8ca10b77b26) - **feat:** add `utils/json-pointer/resolve` [(#15854)](https://github.com/stdlib-js/stdlib/pull/15854) _(by Gururaj Gurram, Athan Reines)_
5456
- [`7932712`](https://github.com/stdlib-js/stdlib/commit/7932712b3b65765e50d383477a4866c556f72398) - **chore:** clean-up [(#16570)](https://github.com/stdlib-js/stdlib/pull/16570) _(by Philipp Burckhardt)_

‎json-pointer/get/README.md‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# get
22+
23+
> Retrieve the value at a specified [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) from a document.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var get = require( '@stdlib/utils/json-pointer/get' );
31+
```
32+
33+
#### get( document, pointer )
34+
35+
Retrieves the value at a specified [JSON Pointer][json-pointer] from a document.
36+
37+
```javascript
38+
var obj = {
39+
'foo': [ 'bar', 'baz' ]
40+
};
41+
var out = get( obj, '/foo/0' );
42+
// returns 'bar'
43+
44+
obj = {
45+
'foo': 'bar'
46+
};
47+
out = get( obj, '/foo' );
48+
// returns 'bar'
49+
50+
out = get( obj, '' );
51+
// returns { 'foo': 'bar' }
52+
```
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document.
63+
- This function is a convenience wrapper around [`@stdlib/utils/json-pointer/resolve`][@stdlib/utils/json-pointer/resolve] that returns the resolved `value`.
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<section class="examples">
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
## Examples
74+
75+
```javascript
76+
var get = require( '@stdlib/utils/json-pointer/get' );
77+
78+
var doc = {
79+
'foo': [ 'bar', 'baz' ],
80+
'': 0,
81+
'a/b': 1,
82+
'm~n': 8
83+
};
84+
85+
var out = get( doc, '/foo' );
86+
// returns [ 'bar', 'baz' ]
87+
88+
out = get( doc, '/foo/0' );
89+
// returns 'bar'
90+
91+
out = get( doc, '/' );
92+
// returns 0
93+
94+
out = get( doc, '/a~1b' );
95+
// returns 1
96+
97+
out = get( doc, '/m~0n' );
98+
// returns 8
99+
100+
out = get( doc, '' );
101+
// returns { 'foo': [ 'bar', 'baz' ], '': 0, 'a/b': 1, 'm~n': 8 }
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="references">
111+
112+
## References
113+
114+
- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901]
115+
116+
</section>
117+
118+
<!-- /.references -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
[json-pointer]: https://datatracker.ietf.org/doc/html/rfc6901
133+
134+
[rfc-6901]: https://datatracker.ietf.org/doc/html/rfc6901
135+
136+
[@stdlib/utils/json-pointer/resolve]: https://github.com/stdlib-js/utils/tree/main/json-pointer/resolve
137+
138+
</section>
139+
140+
<!-- /.links -->
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
25+
var pkg = require( './../package.json' ).name;
26+
var get = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( format( '%s::shallow', pkg ), function benchmark( b ) {
32+
var doc;
33+
var out;
34+
var i;
35+
36+
doc = {
37+
'foo': 'bar',
38+
'baz': 'qux'
39+
};
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = get( doc, '/foo' );
44+
if ( typeof out !== 'string' ) {
45+
b.fail( 'should return a string' );
46+
}
47+
}
48+
b.toc();
49+
if ( out !== 'bar' ) {
50+
b.fail( 'should return expected value' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( format( '%s::deep', pkg ), function benchmark( b ) {
57+
var doc;
58+
var out;
59+
var i;
60+
61+
doc = {
62+
'a': {
63+
'b': {
64+
'c': {
65+
'd': {
66+
'e': {
67+
'f': 42
68+
}
69+
}
70+
}
71+
}
72+
}
73+
};
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = get( doc, '/a/b/c/d/e/f' );
78+
if ( typeof out !== 'number' ) {
79+
b.fail( 'should return a number' );
80+
}
81+
}
82+
b.toc();
83+
if ( out !== 42 ) {
84+
b.fail( 'should return expected value' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
89+
90+
bench( format( '%s::array', pkg ), function benchmark( b ) {
91+
var doc;
92+
var out;
93+
var i;
94+
95+
doc = {
96+
'arr': [
97+
[ 10, 20 ],
98+
[ 30, 40 ]
99+
]
100+
};
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
out = get( doc, '/arr/0/1' );
105+
if ( typeof out !== 'number' ) {
106+
b.fail( 'should return a number' );
107+
}
108+
}
109+
b.toc();
110+
if ( out !== 20 ) {
111+
b.fail( 'should return expected value' );
112+
}
113+
b.pass( 'benchmark finished' );
114+
b.end();
115+
});
116+
117+
bench( format( '%s::root', pkg ), function benchmark( b ) {
118+
var doc;
119+
var out;
120+
var i;
121+
122+
doc = {
123+
'foo': 'bar'
124+
};
125+
126+
b.tic();
127+
for ( i = 0; i < b.iterations; i++ ) {
128+
out = get( doc, '' );
129+
if ( typeof out !== 'object' ) {
130+
b.fail( 'should return an object' );
131+
}
132+
}
133+
b.toc();
134+
if ( out !== doc ) {
135+
b.fail( 'should return root document' );
136+
}
137+
b.pass( 'benchmark finished' );
138+
b.end();
139+
});

‎json-pointer/get/docs/repl.txt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( document, pointer )
3+
Retrieves the value at a specified JSON Pointer from a document.
4+
5+
Per RFC 6901, an empty string refers to the root of the document.
6+
7+
Parameters
8+
----------
9+
document: Object|Array
10+
JSON document.
11+
12+
pointer: string
13+
JSON Pointer string.
14+
15+
Returns
16+
-------
17+
out: any
18+
Value at the pointer location.
19+
20+
Examples
21+
--------
22+
> var out = {{alias}}( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' )
23+
'bar'
24+
> out = {{alias}}( { 'foo': 'bar' }, '/foo' )
25+
'bar'
26+
> out = {{alias}}( { 'foo': 'bar' }, '' )
27+
{ 'foo': 'bar' }
28+
29+
See Also
30+
--------
31+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Retrieves the value at a specified JSON Pointer from a document.
23+
*
24+
* ## Notes
25+
*
26+
* - Implements retrieval per RFC 6901.
27+
* - An empty string pointer refers to the root of the document.
28+
*
29+
* @param document - JSON document
30+
* @param pointer - JSON Pointer string
31+
* @throws first argument must be an object or array
32+
* @throws second argument must be a string
33+
* @throws pointer must resolve to an existing location in the document
34+
* @throws tokens must not be prototype pollution vectors
35+
* @throws array index must be within bounds
36+
* @returns value at the specified location
37+
*
38+
* @example
39+
* var out = get( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' );
40+
* // returns 'bar'
41+
*
42+
* @example
43+
* var out = get( { 'foo': 'bar' }, '/foo' );
44+
* // returns 'bar'
45+
*
46+
* @example
47+
* var out = get( { 'foo': 'bar' }, '' );
48+
* // returns { 'foo': 'bar' }
49+
*/
50+
declare function get( document: Record<string, any> | Array<any>, pointer: string ): any;
51+
52+
53+
// EXPORTS //
54+
55+
export = get;

0 commit comments

Comments
 (0)