Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/get/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# get

> Retrieve the value at a specified [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) from a document.

<section class="usage">

## Usage

```javascript
var get = require( '@stdlib/utils/json-pointer/get' );
```

#### get( document, pointer )

Retrieves the value at a specified [JSON Pointer][json-pointer] from a document.

```javascript
var obj = {
'foo': [ 'bar', 'baz' ]
};
var out = get( obj, '/foo/0' );
// returns 'bar'

obj = {
'foo': 'bar'
};
out = get( obj, '/foo' );
// returns 'bar'

out = get( obj, '' );
// returns { 'foo': 'bar' }
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document.
- This function is a convenience wrapper around [`@stdlib/utils/json-pointer/resolve`][@stdlib/utils/json-pointer/resolve] that returns the resolved `value`.

</section>

<!-- /.notes -->

<section class="examples">

<!-- eslint no-undef: "error" -->

## Examples

```javascript
var get = require( '@stdlib/utils/json-pointer/get' );

var doc = {
'foo': [ 'bar', 'baz' ],
'': 0,
'a/b': 1,
'm~n': 8
};

var out = get( doc, '/foo' );
// returns [ 'bar', 'baz' ]

out = get( doc, '/foo/0' );
// returns 'bar'

out = get( doc, '/' );
// returns 0

out = get( doc, '/a~1b' );
// returns 1

out = get( doc, '/m~0n' );
// returns 8

out = get( doc, '' );
// returns { 'foo': [ 'bar', 'baz' ], '': 0, 'a/b': 1, 'm~n': 8 }
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

## References

- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901]

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[json-pointer]: https://datatracker.ietf.org/doc/html/rfc6901

[rfc-6901]: https://datatracker.ietf.org/doc/html/rfc6901

[@stdlib/utils/json-pointer/resolve]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/json-pointer/resolve

</section>

<!-- /.links -->
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/get/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var get = require( './../lib' );


// MAIN //

bench( format( '%s:shallow', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'foo': 'bar',
'baz': 'qux'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = get( doc, '/foo' );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( out !== 'bar' ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:deep', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'a': {
'b': {
'c': {
'd': {
'e': {
'f': 42
}
}
}
}
}
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = get( doc, '/a/b/c/d/e/f' );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( out !== 42 ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:array', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'arr': [
[ 10, 20 ],
[ 30, 40 ]
]
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = get( doc, '/arr/0/1' );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( out !== 20 ) {
b.fail( 'should return expected value' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:root', pkg ), function benchmark( b ) {
var doc;
var out;
var i;

doc = {
'foo': 'bar'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = get( doc, '' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out !== doc ) {
b.fail( 'should return root document' );
}
b.pass( 'benchmark finished' );
b.end();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/get/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( document, pointer )
Retrieves the value at a specified JSON Pointer from a document.

Per RFC 6901, an empty string refers to the root of the document.

Parameters
----------
document: Object|Array
JSON document.

pointer: string
JSON Pointer string.

Returns
-------
out: any
Value at the pointer location.

Examples
--------
> var out = {{alias}}( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' )
'bar'
> out = {{alias}}( { 'foo': 'bar' }, '/foo' )
'bar'
> out = {{alias}}( { 'foo': 'bar' }, '' )
{ 'foo': 'bar' }

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Retrieves the value at a specified JSON Pointer from a document.
*
* ## Notes
*
* - Implements retrieval per RFC 6901.
* - An empty string pointer refers to the root of the document.
*
* @param document - JSON document
* @param pointer - JSON Pointer string
* @throws first argument must be an object or array
* @throws second argument must be a string
* @throws pointer must resolve to an existing location in the document
* @throws tokens must not be prototype pollution vectors
* @throws array index must be within bounds
* @returns value at the specified location
*
* @example
* var out = get( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' );
* // returns 'bar'
*
* @example
* var out = get( { 'foo': 'bar' }, '/foo' );
* // returns 'bar'
*
* @example
* var out = get( { 'foo': 'bar' }, '' );
* // returns { 'foo': 'bar' }
*/
declare function get( document: Record<string, any> | Array<any>, pointer: string ): any;


// EXPORTS //

export = get;
Loading
Loading