diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/README.md b/lib/node_modules/@stdlib/utils/json-pointer/resolve/README.md new file mode 100644 index 000000000000..871a42a90e95 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/README.md @@ -0,0 +1,140 @@ + + +# resolve + +> Resolve a [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) against a document and return the target, key, and value. + +
+ +## 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' } } +``` + +
+ + + +
+ +## 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`. + +
+ + + +
+ + + +## 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 } +``` + +
+ + + + + +
+ +## References + +- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901] + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/benchmark/benchmark.js new file mode 100644 index 000000000000..25c8a4b1ead3 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/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 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(); +}); diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/repl.txt b/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/repl.txt new file mode 100644 index 000000000000..b8a535d6c609 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/repl.txt @@ -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 + -------- + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/types/index.d.ts new file mode 100644 index 000000000000..80f002147431 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/types/index.d.ts @@ -0,0 +1,75 @@ +/* +* @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 + +/** +* Result object returned by the resolve function. +*/ +interface ResolveResult { + /** + * Parent container (object or array). Null for root pointer. + */ + target: Record | Array | null; + + /** + * Final reference key. Null for root pointer. + */ + key: string | number | null; + + /** + * Value at the pointer location. + */ + value: any; +} + +/** +* Resolves a JSON Pointer against a document and returns the target, key, and value. +* +* ## Notes +* +* - Implements resolution per RFC 6901. +* - An empty string pointer 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. +* +* @param document - JSON document +* @param pointer - JSON Pointer string +* @throws first argument must be an object or array +* @throws pointer must resolve to an existing location +* @throws tokens must not be prototype pollution vectors +* @returns result object +* +* @example +* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' ); +* // returns { 'target': [ 'bar', 'baz' ], 'key': 0, 'value': 'bar' } +* +* @example +* var out = resolve( { 'foo': 'bar' }, '/foo' ); +* // returns { 'target': { 'foo': 'bar' }, 'key': 'foo', 'value': 'bar' } +* +* @example +* var out = resolve( { 'foo': 'bar' }, '' ); +* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } } +*/ +declare function resolve( document: Record | Array, pointer: string ): ResolveResult; + + +// EXPORTS // + +export = resolve; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/types/test.ts b/lib/node_modules/@stdlib/utils/json-pointer/resolve/docs/types/test.ts new file mode 100644 index 000000000000..429704d64473 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/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 resolve = require( './index' ); + + +// TESTS // + +// The function returns a resolve result object... +{ + resolve( { 'foo': 'bar' }, '/foo' ); // $ExpectType ResolveResult + resolve( [ 1, 2, 3 ], '/0' ); // $ExpectType ResolveResult + resolve( { 'foo': 'bar' }, '' ); // $ExpectType ResolveResult +} + +// The compiler throws an error if the function is provided a first argument which is not an object or array... +{ + resolve( 'abc', '/foo' ); // $ExpectError + resolve( 123, '/foo' ); // $ExpectError + resolve( true, '/foo' ); // $ExpectError + resolve( null, '/foo' ); // $ExpectError + resolve( undefined, '/foo' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string... +{ + resolve( { 'foo': 'bar' }, 123 ); // $ExpectError + resolve( { 'foo': 'bar' }, true ); // $ExpectError + resolve( { 'foo': 'bar' }, false ); // $ExpectError + resolve( { 'foo': 'bar' }, null ); // $ExpectError + resolve( { 'foo': 'bar' }, undefined ); // $ExpectError + resolve( { 'foo': 'bar' }, [] ); // $ExpectError + resolve( { 'foo': 'bar' }, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + resolve(); // $ExpectError + resolve( { 'foo': 'bar' } ); // $ExpectError + resolve( { 'foo': 'bar' }, '/foo', 'extra' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/examples/index.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/examples/index.js new file mode 100644 index 000000000000..0e05887c1c0c --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/examples/index.js @@ -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. +*/ + +'use strict'; + +var resolve = require( './../lib' ); + +var doc = { + 'foo': [ 'bar', 'baz' ], + '': 0, + 'a/b': 1, + 'm~n': 8 +}; + +var out = resolve( doc, '/foo' ); +console.log( out ); +// => { 'target': { 'foo': [...], ... }, 'key': 'foo', 'value': [ 'bar', 'baz' ] } + +out = resolve( doc, '/foo/0' ); +console.log( out ); +// => { 'target': [ 'bar', 'baz' ], 'key': 0, 'value': 'bar' } + +out = resolve( doc, '/' ); +console.log( out ); +// => { 'target': { ... }, 'key': '', 'value': 0 } + +out = resolve( doc, '/a~1b' ); +console.log( out ); +// => { 'target': { ... }, 'key': 'a/b', 'value': 1 } + +out = resolve( doc, '/m~0n' ); +console.log( out ); +// => { 'target': { ... }, 'key': 'm~n', 'value': 8 } + +out = resolve( doc, '' ); +console.log( out ); +// => { 'target': null, 'key': null, 'value': { ... } } + +out = resolve( doc, '/foo/-' ); +console.log( out ); +// => { 'target': [ 'bar', 'baz' ], 'key': 2, 'value': undefined } diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/index.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/index.js new file mode 100644 index 000000000000..f82170ea42ca --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/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'; + +/** +* Resolve a JSON Pointer against a document and return the target, key, and value. +* +* @module @stdlib/utils/json-pointer/resolve +* +* @example +* var resolve = require( '@stdlib/utils/json-pointer/resolve' ); +* +* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' ); +* // returns { 'target': [ 'bar', 'baz' ], 'key': 0, 'value': 'bar' } +* +* out = resolve( { 'foo': 'bar' }, '/foo' ); +* // returns { 'target': { 'foo': 'bar' }, 'key': 'foo', 'value': 'bar' } +* +* out = resolve( { 'foo': 'bar' }, '' ); +* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/is_safe_token.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/is_safe_token.js new file mode 100644 index 000000000000..071ef112f36a --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/is_safe_token.js @@ -0,0 +1,48 @@ +/** +* @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'; + +// MAIN // + +/** +* Tests whether a token is safe (i.e., not a prototype pollution vector). +* +* @private +* @param {string} token - reference token +* @returns {boolean} boolean indicating whether a token is safe +* +* @example +* var bool = isSafeToken( 'foo' ); +* // returns true +* +* @example +* var bool = isSafeToken( '__proto__' ); +* // returns false +*/ +function isSafeToken( token ) { + if ( token === '__proto__' || token === 'constructor' || token === 'prototype' ) { + return false; + } + return true; +} + + +// EXPORTS // + +module.exports = isSafeToken; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/main.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/main.js new file mode 100644 index 000000000000..f4f493af6027 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/lib/main.js @@ -0,0 +1,154 @@ +/** +* @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 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( './is_safe_token.js' ); + + +// VARIABLES // + +var RE_ARRAY_INDEX = /^(?:0|[1-9][0-9]*)$/; + + +// MAIN // + +/** +* Resolves a JSON Pointer against a document and returns the target, key, and value. +* +* ## Notes +* +* - Implements resolution per RFC 6901. +* - An empty string pointer 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. +* +* @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 {Object} result object +* +* @example +* var out = resolve( { 'foo': [ 'bar', 'baz' ] }, '/foo/0' ); +* // returns { 'target': [ 'bar', 'baz' ], 'key': 0, 'value': 'bar' } +* +* @example +* var out = resolve( { 'foo': 'bar' }, '/foo' ); +* // returns { 'target': { 'foo': 'bar' }, 'key': 'foo', 'value': 'bar' } +* +* @example +* var out = resolve( { 'foo': 'bar' }, '' ); +* // returns { 'target': null, 'key': null, 'value': { 'foo': 'bar' } } +*/ +function resolve( document, pointer ) { + var current; + var tokens; + var token; + var last; + var key; + 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 ); + + // An empty pointer refers to the root: + if ( tokens.length === 0 ) { + return { + 'target': null, + 'key': null, + 'value': document + }; + } + + current = document; + last = tokens.length - 1; + + // Traverse all tokens except the last to find the target: + for ( i = 0; i < last; i++ ) { + token = tokens[ i ]; + if ( !isSafeToken( token ) ) { + throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', token ) ); + } + if ( isArray( current ) ) { + if ( !RE_ARRAY_INDEX.test( token ) ) { + throw new Error( format( 'invalid argument. Invalid array index in pointer. Value: `%s`.', token ) ); + } + idx = parseInt( token, 10 ); + if ( idx >= current.length ) { + throw new RangeError( format( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.', idx, current.length ) ); + } + current = current[ idx ]; + } else if ( isPlainObject( current ) ) { + if ( !hasOwnProp( current, token ) ) { + throw new Error( format( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.', token ) ); + } + current = current[ token ]; + } else { + throw new TypeError( format( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.', token ) ); + } + } + + // Handle the last token to determine the final key and value: + token = tokens[ last ]; + if ( !isSafeToken( token ) ) { + throw new Error( format( 'invalid argument. Pointer contains an unsafe token. Value: `%s`.', token ) ); + } + if ( isArray( current ) ) { + if ( token === '-' ) { + key = current.length; + } else if ( RE_ARRAY_INDEX.test( token ) ) { + key = parseInt( token, 10 ); + if ( key >= current.length ) { + throw new RangeError( format( 'invalid argument. Array index out of bounds. Index: `%d`. Length: `%d`.', key, current.length ) ); + } + } else { + throw new Error( format( 'invalid argument. Invalid array index in pointer. Value: `%s`.', token ) ); + } + } else if ( isPlainObject( current ) ) { + if ( !hasOwnProp( current, token ) ) { + throw new Error( format( 'invalid argument. Pointer references a nonexistent property. Value: `%s`.', token ) ); + } + key = token; + } else { + throw new TypeError( format( 'invalid argument. Pointer references a non-traversable value. Value: `%s`.', token ) ); + } + return { + 'target': current, + 'key': key, + 'value': current[ key ] + }; +} + + +// EXPORTS // + +module.exports = resolve; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/package.json b/lib/node_modules/@stdlib/utils/json-pointer/resolve/package.json new file mode 100644 index 000000000000..42e500fb5762 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/utils/json-pointer/resolve", + "version": "0.0.0", + "description": "Resolve a JSON Pointer against a document and return the target, key, and value.", + "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", + "resolve", + "traverse", + "walk", + "reference", + "path", + "target" + ] +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/resolve/test/test.js b/lib/node_modules/@stdlib/utils/json-pointer/resolve/test/test.js new file mode 100644 index 000000000000..1fbf7647ba6c --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/resolve/test/test.js @@ -0,0 +1,346 @@ +/** +* @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 resolve = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof resolve, '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() { + resolve( 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() { + resolve( doc, value ); + }; + } +}); + +tape( 'the function returns the root document for an empty pointer', function test( t ) { + var doc; + var out; + + doc = { + 'foo': 'bar' + }; + out = resolve( doc, '' ); + + t.strictEqual( out.target, null, 'returns expected value' ); + t.strictEqual( out.key, null, 'returns expected value' ); + t.strictEqual( out.value, doc, 'returns expected value' ); + t.end(); +}); + +tape( 'the function resolves pointers on 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 = [ + { + 'key': 'foo', + 'value': doc.foo + }, + { + 'key': '', + 'value': 0 + }, + { + 'key': 'a/b', + 'value': 1 + }, + { + 'key': 'c%d', + 'value': 2 + }, + { + 'key': 'e^f', + 'value': 3 + }, + { + 'key': 'g|h', + 'value': 4 + }, + { + 'key': 'i\\j', + 'value': 5 + }, + { + 'key': 'k\"l', + 'value': 6 + }, + { + 'key': ' ', + 'value': 7 + }, + { + 'key': 'm~n', + 'value': 8 + } + ]; + + for ( i = 0; i < values.length; i++ ) { + out = resolve( doc, values[ i ] ); + t.strictEqual( out.target, doc, 'returns expected value' ); + t.strictEqual( out.key, expected[ i ].key, 'returns expected value' ); + t.deepEqual( out.value, expected[ i ].value, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function resolves pointers on arrays', function test( t ) { + var doc; + var out; + + doc = { + 'foo': [ 'bar', 'baz', 'qux' ] + }; + + out = resolve( doc, '/foo/0' ); + t.strictEqual( out.target, doc.foo, 'returns expected value' ); + t.strictEqual( out.key, 0, 'returns expected value' ); + t.strictEqual( out.value, 'bar', 'returns expected value' ); + + out = resolve( doc, '/foo/1' ); + t.strictEqual( out.target, doc.foo, 'returns expected value' ); + t.strictEqual( out.key, 1, 'returns expected value' ); + t.strictEqual( out.value, 'baz', 'returns expected value' ); + + out = resolve( doc, '/foo/2' ); + t.strictEqual( out.target, doc.foo, 'returns expected value' ); + t.strictEqual( out.key, 2, 'returns expected value' ); + t.strictEqual( out.value, 'qux', 'returns expected value' ); + t.end(); +}); + +tape( 'the function resolves nested pointers', function test( t ) { + var doc; + var out; + + doc = { + 'a': { + 'b': { + 'c': 42 + } + } + }; + + out = resolve( doc, '/a/b/c' ); + t.strictEqual( out.target, doc.a.b, 'returns expected value' ); + t.strictEqual( out.key, 'c', 'returns expected value' ); + t.strictEqual( out.value, 42, 'returns expected value' ); + t.end(); +}); + +tape( 'the function resolves the `-` token on arrays to the past-the-end index', function test( t ) { + var doc; + var out; + + doc = { + 'foo': [ 'a', 'b', 'c' ] + }; + + out = resolve( doc, '/foo/-' ); + t.strictEqual( out.target, doc.foo, 'returns expected value' ); + t.strictEqual( out.key, 3, 'returns expected value' ); + t.strictEqual( out.value, void 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error for unsafe tokens', function test( t ) { + var values; + var doc; + var i; + + doc = { + 'foo': { + 'bar': 1 + } + }; + values = [ + '/__proto__', + '/constructor', + '/prototype', + '/foo/__proto__', + '/foo/constructor' + ]; + 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() { + resolve( doc, value ); + }; + } +}); + +tape( 'the function throws an error for nonexistent object properties', function test( t ) { + var doc; + + doc = { + 'foo': 'bar' + }; + t.throws( function badValue() { + resolve( doc, '/missing' ); + }, Error, 'throws an error when provided /missing' ); + t.end(); +}); + +tape( 'the function throws an error for out-of-bounds array indices', function test( t ) { + var doc; + + doc = { + 'foo': [ 1, 2, 3 ] + }; + t.throws( function badValue() { + resolve( doc, '/foo/5' ); + }, RangeError, 'throws an error when provided /foo/5' ); + t.end(); +}); + +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() { + resolve( doc, value ); + }; + } +}); + +tape( 'the function throws an error when traversing into a primitive', function test( t ) { + var doc; + + doc = { + 'foo': 'bar' + }; + t.throws( function badValue() { + resolve( doc, '/foo/baz' ); + }, TypeError, 'throws an error when traversing into a string' ); + + doc = { + 'foo': 42 + }; + t.throws( function badValue() { + resolve( doc, '/foo/bar' ); + }, TypeError, 'throws an error when traversing into a number' ); + t.end(); +});