diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/README.md b/lib/node_modules/@stdlib/utils/json-pointer/get/README.md new file mode 100644 index 000000000000..3e059dfdebe3 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/README.md @@ -0,0 +1,140 @@ + + +# get + +> Retrieve the value at a specified [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) from a document. + +
+ +## 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' } +``` + +
+ + + +
+ +## 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`. + +
+ + + +
+ + + +## 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 } +``` + +
+ + + + + +
+ +## References + +- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901] + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/json-pointer/get/benchmark/benchmark.js new file mode 100644 index 000000000000..0f7b92fc8d8f --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/docs/repl.txt b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/repl.txt new file mode 100644 index 000000000000..7eddf412c043 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/repl.txt @@ -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 + -------- + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts new file mode 100644 index 000000000000..bf9a8d852169 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/index.d.ts @@ -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 | Array, pointer: string ): any; + + +// EXPORTS // + +export = get; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/test.ts b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/test.ts new file mode 100644 index 000000000000..bdf39c41bb22 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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. +*/ + +import get = require( './index' ); + + +// TESTS // + +// The function returns a value... +{ + get( { 'foo': 'bar' }, '/foo' ); // $ExpectType any + get( [ 1, 2, 3 ], '/0' ); // $ExpectType any + get( { 'foo': 'bar' }, '' ); // $ExpectType any +} + +// The compiler throws an error if the function is provided a first argument which is not an object or array... +{ + get( 'abc', '/foo' ); // $ExpectError + get( 123, '/foo' ); // $ExpectError + get( true, '/foo' ); // $ExpectError + get( null, '/foo' ); // $ExpectError + get( undefined, '/foo' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string... +{ + get( { 'foo': 'bar' }, 123 ); // $ExpectError + get( { 'foo': 'bar' }, true ); // $ExpectError + get( { 'foo': 'bar' }, false ); // $ExpectError + get( { 'foo': 'bar' }, null ); // $ExpectError + get( { 'foo': 'bar' }, undefined ); // $ExpectError + get( { 'foo': 'bar' }, [] ); // $ExpectError + get( { 'foo': 'bar' }, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + get(); // $ExpectError + get( { 'foo': 'bar' } ); // $ExpectError + get( { 'foo': 'bar' }, '/foo', 'extra' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/examples/index.js b/lib/node_modules/@stdlib/utils/json-pointer/get/examples/index.js new file mode 100644 index 000000000000..dfcea441c2d4 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/examples/index.js @@ -0,0 +1,52 @@ +/** +* @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'; + +var get = require( './../lib' ); + +var doc = { + 'foo': [ 'bar', 'baz' ], + '': 0, + 'a/b': 1, + 'm~n': 8 +}; + +var out = get( doc, '/foo' ); +console.log( out ); +// => [ 'bar', 'baz' ] + +out = get( doc, '/foo/0' ); +console.log( out ); +// => 'bar' + +out = get( doc, '/' ); +console.log( out ); +// => 0 + +out = get( doc, '/a~1b' ); +console.log( out ); +// => 1 + +out = get( doc, '/m~0n' ); +console.log( out ); +// => 8 + +out = get( doc, '' ); +console.log( out ); +// => { 'foo': [ 'bar', 'baz' ], '': 0, 'a/b': 1, 'm~n': 8 } diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/lib/index.js b/lib/node_modules/@stdlib/utils/json-pointer/get/lib/index.js new file mode 100644 index 000000000000..13e665ec5744 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* Retrieve the value at a specified JSON Pointer from a document. +* +* @module @stdlib/utils/json-pointer/get +* +* @example +* var get = require( '@stdlib/utils/json-pointer/get' ); +* +* var out = get( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' ); +* // returns 'bar' +* +* out = get( { 'foo': 'bar' }, '/foo' ); +* // returns 'bar' +* +* out = get( { 'foo': 'bar' }, '' ); +* // returns { 'foo': 'bar' } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/lib/main.js b/lib/node_modules/@stdlib/utils/json-pointer/get/lib/main.js new file mode 100644 index 000000000000..aad88d3c7fd1 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/lib/main.js @@ -0,0 +1,64 @@ +/** +* @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 resolve = require( '@stdlib/utils/json-pointer/resolve' ); + + +// MAIN // + +/** +* 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 {(Object|Array)} document - JSON document +* @param {string} pointer - JSON Pointer string +* @throws {TypeError} first argument must be an object or array +* @throws {TypeError} second argument must be a string +* @throws {Error} pointer must resolve to an existing location in the document +* @throws {Error} tokens must not be prototype pollution vectors +* @throws {RangeError} 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' } +*/ +function get( document, pointer ) { + return resolve( document, pointer ).value; +} + + +// EXPORTS // + +module.exports = get; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/package.json b/lib/node_modules/@stdlib/utils/json-pointer/get/package.json new file mode 100644 index 000000000000..03b4721993b0 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/utils/json-pointer/get", + "version": "0.0.0", + "description": "Retrieve the value at a specified JSON Pointer from a document.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "json", + "pointer", + "json-pointer", + "rfc6901", + "rfc", + "6901", + "get", + "value", + "read", + "lookup", + "resolve" + ] +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/get/test/test.js b/lib/node_modules/@stdlib/utils/json-pointer/get/test/test.js new file mode 100644 index 000000000000..567b46f20dac --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/get/test/test.js @@ -0,0 +1,288 @@ +/** +* @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. +*/ + +/* eslint-disable no-useless-escape */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var get = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof get, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not an object or array', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 5, + NaN, + true, + null, + void 0, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + get( value, '/foo' ); + }; + } +}); + +tape( 'the function throws an error if the second argument is not a string', function test( t ) { + var values; + var doc; + var i; + + doc = { + 'foo': 'bar' + }; + values = [ + 5, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + get( doc, value ); + }; + } +}); + +tape( 'the function returns the root document for an empty pointer', function test( t ) { + var doc; + var out; + + doc = { + 'foo': 'bar' + }; + out = get( doc, '' ); + t.strictEqual( out, doc, 'returns expected value' ); + + doc = [ 1, 2, 3 ]; + out = get( doc, '' ); + t.strictEqual( out, doc, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function retrieves values from objects', function test( t ) { + var expected; + var values; + var doc; + var out; + var i; + + doc = { + 'foo': [ 'bar', 'baz' ], + '': 0, + 'a/b': 1, + 'c%d': 2, + 'e^f': 3, + 'g|h': 4, + 'i\\j': 5, + 'k\"l': 6, + ' ': 7, + 'm~n': 8 + }; + + values = [ + '/foo', + '/', + '/a~1b', + '/c%d', + '/e^f', + '/g|h', + '/i\\j', + '/k\"l', + '/ ', + '/m~0n' + ]; + expected = [ + doc.foo, + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8 + ]; + + for ( i = 0; i < values.length; i++ ) { + out = get( doc, values[ i ] ); + t.deepEqual( out, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function retrieves values from arrays', function test( t ) { + var doc; + var out; + + doc = { + 'foo': [ 'bar', 'baz', 'qux' ] + }; + + out = get( doc, '/foo/0' ); + t.strictEqual( out, 'bar', 'returns expected value' ); + + out = get( doc, '/foo/1' ); + t.strictEqual( out, 'baz', 'returns expected value' ); + + out = get( doc, '/foo/2' ); + t.strictEqual( out, 'qux', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function retrieves nested values', function test( t ) { + var doc; + var out; + + doc = { + 'a': { + 'b': { + 'c': 42 + } + } + }; + + out = get( doc, '/a/b/c' ); + t.strictEqual( out, 42, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns undefined for the `-` token on an array', function test( t ) { + var doc; + var out; + + doc = { + 'foo': [ 'a', 'b', 'c' ] + }; + + out = get( doc, '/foo/-' ); + t.strictEqual( out, void 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error for nonexistent object properties', function test( t ) { + var doc; + + doc = { + 'foo': 'bar' + }; + t.throws( badValue( '/missing' ), Error, 'throws an error when provided nonexistent object properties' ); + t.end(); + + function badValue( value ) { + return function badValue() { + get( doc, value ); + }; + } +}); + +tape( 'the function throws an error for out-of-bounds array indices', function test( t ) { + var doc; + + doc = { + 'foo': [ 1, 2, 3 ] + }; + t.throws( badValue( '/foo/5' ), RangeError, 'throws an error when provided out-of-bounds array indices' ); + t.end(); + + function badValue( value ) { + return function badValue() { + get( doc, value ); + }; + } +}); + +tape( 'the function throws an error for invalid array indices', function test( t ) { + var values; + var doc; + var i; + + doc = { + 'foo': [ 1, 2, 3 ] + }; + values = [ + '/foo/01', + '/foo/bar', + '/foo/-1', + '/foo/1.5' + ]; + 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 ) { + return function badValue() { + get( doc, value ); + }; + } +}); + +tape( 'the function throws an error when traversing 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' ); + + doc = { + 'foo': 42 + }; + t.throws( badValue( '/foo/bar' ), TypeError, 'throws an error when traversing into a primitive' ); + t.end(); + + function badValue( value ) { + return function badValue() { + get( doc, value ); + }; + } +});