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/resolve/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.

-->

# resolve

> Resolve a [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) against a document and return the target, key, and value.

<section class="usage">

## Usage

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

#### resolve( document, pointer )

Resolves a [JSON Pointer][json-pointer] against a document and returns an object with `target`, `key`, and `value` properties.

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

obj = {
'foo': 'bar'
};
out = resolve( obj, '/foo' );
// returns { 'target': { 'foo': 'bar' }, 'key': 'foo', 'value': 'bar' }

out = resolve( obj, '' );
// returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } }
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document.
- The `-` token on the last segment of a pointer targeting an array resolves to the array's length (past-the-end index), useful for JSON Patch `add` operations.
- Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
- The returned `target` is the parent container (object or array) holding the referenced value. For root pointers, `target` and `key` are both `null`.

</section>

<!-- /.notes -->

<section class="examples">

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

## Examples

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

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

var out = resolve( doc, '/foo' );
// returns { 'target': doc, 'key': 'foo', 'value': [ 'bar', 'baz' ] }

out = resolve( doc, '/foo/0' );
// returns { 'target': [ 'bar', 'baz' ], 'key': 0, 'value': 'bar' }

out = resolve( doc, '/' );
// returns { 'target': doc, 'key': '', 'value': 0 }

out = resolve( doc, '/a~1b' );
// returns { 'target': doc, 'key': 'a/b', 'value': 1 }

out = resolve( doc, '/m~0n' );
// returns { 'target': doc, 'key': 'm~n', 'value': 8 }

out = resolve( doc, '' );
// returns { 'target': null, 'key': null, 'value': doc }
```

</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

</section>

<!-- /.links -->
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 resolve = 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 = resolve( doc, '/foo' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out.value !== '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 = resolve( doc, '/a/b/c/d/e/f' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out.value !== 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 = resolve( doc, '/arr/0/1' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out.value !== 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 = resolve( doc, '' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( out.target !== null ) {
b.fail( 'should return null target for root' );
}
b.pass( 'benchmark finished' );
b.end();
});
47 changes: 47 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

{{alias}}( document, pointer )
Resolves a JSON Pointer against a document and returns the target
container, key, and value.

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

The `-` token on the last segment of a pointer targeting an array resolves
to the array's length (past-the-end index).

Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent
prototype pollution.

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

pointer: string
JSON Pointer string.

Returns
-------
out: Object
Result object.

out.target: Object|Array|null
Parent container. Null for root pointer.

out.key: string|integer|null
Final reference key. Null for root pointer.

out.value: any
Value at the pointer location.

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

See Also
--------

Loading
Loading