Skip to content
Open
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
12 changes: 9 additions & 3 deletions lib/node_modules/@stdlib/utils/json-pointer/get/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ out = get( obj, '/foo' );

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

out = get( obj, '/missing' );
// returns undefined
```

</section>
Expand All @@ -60,7 +63,9 @@ out = get( obj, '' );
## 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`.
- If a pointer references a nonexistent location in the document, the function returns `undefined`.
- The `-` token on an array references the position after the last element (i.e., a nonexistent element), and, thus, the function returns `undefined`.
- Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.

</section>

Expand Down Expand Up @@ -99,6 +104,9 @@ out = get( doc, '/m~0n' );

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

out = get( doc, '/missing' );
// returns undefined
```

</section>
Expand Down Expand Up @@ -133,8 +141,6 @@ out = get( doc, '' );

[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 -->
11 changes: 11 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/get/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

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

If a pointer references a nonexistent location in the document, the
function returns `undefined`.

The `-` token on an array references the position after the last element
(i.e., a nonexistent element), and, thus, the function returns `undefined`.

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

Parameters
----------
document: Object|Array
Expand All @@ -25,6 +34,8 @@
'bar'
> out = {{alias}}( { 'foo': 'bar' }, '' )
{ 'foo': 'bar' }
> out = {{alias}}( { 'foo': 'bar' }, '/missing' )
undefined

See Also
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
*
* - Implements retrieval per RFC 6901.
* - An empty string pointer refers to the root of the document.
* - If a pointer references a nonexistent location in the document, the function returns `undefined`.
* - Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
*
* @param document - JSON document
* @param pointer - JSON Pointer string
* @throws first argument must be an object or array
* @throws first argument must be an object or an array
* @throws second argument must be a string
* @throws pointer must resolve to an existing location in the document
* @throws pointer must be an empty string or begin with `/`
* @throws tokens must not be prototype pollution vectors
* @throws array index must be within bounds
* @returns value at the specified location
*
* @example
Expand All @@ -46,8 +47,12 @@
* @example
* var out = get( { 'foo': 'bar' }, '' );
* // returns { 'foo': 'bar' }
*
* @example
* var out = get( { 'foo': 'bar' }, '/missing' );
* // returns undefined
*/
declare function get( document: Record<string, any> | Array<any>, pointer: string ): any;

Check warning on line 55 in lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 55 in lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 55 in lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ console.log( out );
out = get( doc, '' );
console.log( out );
// => { 'foo': [ 'bar', 'baz' ], '': 0, 'a/b': 1, 'm~n': 8 }

out = get( doc, '/missing' );
console.log( out );
// => undefined
61 changes: 56 additions & 5 deletions lib/node_modules/@stdlib/utils/json-pointer/get/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@

// MODULES //

var resolve = require( '@stdlib/utils/json-pointer/resolve' );
var isArray = require( '@stdlib/assert/is-array' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var format = require( '@stdlib/string/format' );
var parse = require( '@stdlib/utils/json-pointer/parse' );
var isSafeToken = require( '@stdlib/utils/json-pointer/base/assert/is-safe-token' );
var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' );


// MAIN //
Expand All @@ -32,14 +38,15 @@ var resolve = require( '@stdlib/utils/json-pointer/resolve' );
*
* - Implements retrieval per RFC 6901.
* - An empty string pointer refers to the root of the document.
* - If a pointer references a nonexistent location in the document, the function returns `undefined`.
* - Tokens `__proto__`, `constructor`, and `prototype` are rejected to prevent prototype pollution.
*
* @param {(Object|Array)} document - JSON document
* @param {string} pointer - JSON Pointer string
* @throws {TypeError} first argument must be an object or array
* @throws {TypeError} first argument must be an object or an array
* @throws {TypeError} second argument must be a string
* @throws {Error} pointer must resolve to an existing location in the document
* @throws {Error} pointer must be an empty string or begin with `/`
* @throws {Error} tokens must not be prototype pollution vectors
* @throws {RangeError} array index must be within bounds
* @returns {*} value at the specified location
*
* @example
Expand All @@ -53,9 +60,53 @@ var resolve = require( '@stdlib/utils/json-pointer/resolve' );
* @example
* var out = get( { 'foo': 'bar' }, '' );
* // returns { 'foo': 'bar' }
*
* @example
* var out = get( { 'foo': 'bar' }, '/missing' );
* // returns undefined
*/
function get( document, pointer ) {
return resolve( document, pointer ).value;
var current;
var tokens;
var token;
var idx;
var i;

if ( !isPlainObject( document ) && !isArray( document ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an object or an array. Value: `%s`.', document ) );
}
tokens = parse( pointer );

// Validate all tokens upfront for security to guarantee deterministic rejection:
for ( i = 0; i < tokens.length; i++ ) {
if ( !isSafeToken( tokens[ i ] ) ) {
throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', tokens[ i ] ) );
}
}

// Traverse all tokens to find the target value:
current = document;
for ( i = 0; i < tokens.length; i++ ) {
token = tokens[ i ];
if ( isArray( current ) ) {
idx = resolveToken( current, token );
if ( idx >= current.length ) {
// Array index is out-of-bounds...
return;
}
current = current[ idx ];
} else if ( isPlainObject( current ) ) {
if ( !hasOwnProp( current, token ) ) {
// Pointer references a nonexistent property...
return;
}
current = current[ token ];
} else {
// Pointer references a non-traversable value...
return;
}
}
return current;
}


Expand Down
80 changes: 66 additions & 14 deletions lib/node_modules/@stdlib/utils/json-pointer/get/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,29 @@ tape( 'the function returns undefined for the `-` token on an array', function t

out = get( doc, '/foo/-' );
t.strictEqual( out, void 0, 'returns expected value' );

out = get( doc, '/foo/-/bar' );
t.strictEqual( out, void 0, 'returns expected value' );
t.end();
});

tape( 'the function throws an error for nonexistent object properties', function test( t ) {
tape( 'the function throws an error if pointer syntax is invalid', function test( t ) {
var values;
var doc;
var i;

doc = {
'foo': 'bar'
};
t.throws( badValue( '/missing' ), Error, 'throws an error when provided nonexistent object properties' );
values = [
'foo',
'foo/bar',
'/foo~2',
'/foo~'
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] );
}
t.end();

function badValue( value ) {
Expand All @@ -224,13 +237,29 @@ tape( 'the function throws an error for nonexistent object properties', function
}
});

tape( 'the function throws an error for out-of-bounds array indices', function test( t ) {
tape( 'the function throws an error for unsafe tokens', function test( t ) {
var values;
var doc;
var i;

doc = {
'foo': [ 1, 2, 3 ]
'foo': {
'bar': 1
}
};
t.throws( badValue( '/foo/5' ), RangeError, 'throws an error when provided out-of-bounds array indices' );
values = [
'/__proto__',
'/constructor',
'/prototype',
'/foo/__proto__',
'/foo/constructor',
'/missing/__proto__',
'/missing/constructor',
'/a/b/c/prototype'
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] );
}
t.end();

function badValue( value ) {
Expand All @@ -240,6 +269,29 @@ tape( 'the function throws an error for out-of-bounds array indices', function t
}
});

tape( 'the function returns undefined for nonexistent object properties', function test( t ) {
var doc;

doc = {
'foo': 'bar'
};
t.strictEqual( get( doc, '/missing' ), void 0, 'returns expected value' );
t.strictEqual( get( doc, '/missing/nested' ), void 0, 'returns expected value' );
t.end();
});

tape( 'the function returns undefined for out-of-bounds array indices', function test( t ) {
var doc;

doc = {
'foo': [ 1, 2, 3 ]
};
t.strictEqual( get( doc, '/foo/3' ), void 0, 'returns expected value' );
t.strictEqual( get( doc, '/foo/5' ), void 0, 'returns expected value' );
t.strictEqual( get( doc, '/foo/5/bar' ), void 0, 'returns expected value' );
t.end();
});

tape( 'the function throws an error for invalid array indices', function test( t ) {
var values;
var doc;
Expand All @@ -266,23 +318,23 @@ tape( 'the function throws an error for invalid array indices', function test( t
}
});

tape( 'the function throws an error when traversing into a primitive', function test( t ) {
tape( 'the function returns undefined when attempting to traverse into a primitive', function test( t ) {
var doc;

doc = {
'foo': 'bar'
};
t.throws( badValue( '/foo/5' ), TypeError, 'throws an error when traversing into a primitive' );
t.strictEqual( get( doc, '/foo/baz' ), void 0, 'returns expected value' );
t.strictEqual( get( doc, '/foo/0' ), void 0, 'returns expected value' );

doc = {
'foo': 42
};
t.throws( badValue( '/foo/bar' ), TypeError, 'throws an error when traversing into a primitive' );
t.end();
t.strictEqual( get( doc, '/foo/bar' ), void 0, 'returns expected value' );

function badValue( value ) {
return function badValue() {
get( doc, value );
};
}
doc = {
'foo': null
};
t.strictEqual( get( doc, '/foo/bar' ), void 0, 'returns expected value' );
t.end();
});