From 74f5d7cf3e1ca6c0c1c9a7a6aa0e864315dd6cbe Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Tue, 27 Oct 2026 12:11:38 +0530 Subject: [PATCH] feat: add utils/json-pointer/base/resolve-token --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: skipped - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../json-pointer/base/resolve-token/README.md | 142 ++++++++++++++++++ .../base/resolve-token/benchmark/benchmark.js | 82 ++++++++++ .../base/resolve-token/docs/repl.txt | 34 +++++ .../base/resolve-token/docs/types/index.d.ts | 51 +++++++ .../base/resolve-token/docs/types/test.ts | 35 +++++ .../base/resolve-token/examples/index.js | 47 ++++++ .../base/resolve-token/lib/index.js | 51 +++++++ .../base/resolve-token/lib/main.js | 84 +++++++++++ .../base/resolve-token/package.json | 67 +++++++++ .../base/resolve-token/test/test.js | 105 +++++++++++++ 10 files changed, 698 insertions(+) create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/README.md create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/examples/index.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/index.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/main.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/package.json create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/test/test.js diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/README.md b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/README.md new file mode 100644 index 000000000000..3cddba288ceb --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/README.md @@ -0,0 +1,142 @@ + + +# resolveToken + +> Resolve a JSON Pointer reference token against a container. + + + +
+ +This function resolves a [JSON Pointer][json-pointer] reference token against a container, returning the appropriate key. + +
+ + + + + +
+ +## Usage + +```javascript +var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' ); +``` + +#### resolveToken( container, token ) + +Resolves a JSON Pointer reference token against a container. + +```javascript +var obj = { + 'foo': 'bar' +}; +var arr = [ 1, 2, 3 ]; + +var key = resolveToken( obj, 'foo' ); +// returns 'foo' + +key = resolveToken( arr, '0' ); +// returns 0 + +key = resolveToken( arr, '-' ); +// returns 3 +``` + +
+ + + + + +
+ +## Notes + +- For arrays, returns an integer index. +- For arrays, the `-` token is resolved to the array length (`container.length`). +- For objects, returns the token string as-is. +- If `container` is an array and `token` is not a valid array index (or `-`), the function throws an error. + +
+ + + + + +
+ +## Examples + + + +```javascript +var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' ); + +var arr = [ 'apple', 'banana', 'cherry' ]; +var obj = { + 'foo': 'bar', + '0': 'baz' +}; + +var key = resolveToken( arr, '0' ); +console.log( key ); +// => 0 + +key = resolveToken( arr, '2' ); +console.log( key ); +// => 2 + +key = resolveToken( arr, '-' ); +console.log( key ); +// => 3 + +key = resolveToken( obj, 'foo' ); +console.log( key ); +// => 'foo' + +key = resolveToken( obj, '0' ); +console.log( key ); +// => '0' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/benchmark/benchmark.js new file mode 100644 index 000000000000..07b22056921c --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/benchmark/benchmark.js @@ -0,0 +1,82 @@ +/** +* @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 resolveToken = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::array', pkg ), function benchmark( b ) { + var tokens; + var arr; + var out; + var i; + + arr = [ 1, 2, 3, 4, 5 ]; + tokens = [ '0', '1', '2', '3', '4', '-' ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = resolveToken( arr, tokens[ i % tokens.length ] ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::object', pkg ), function benchmark( b ) { + var tokens; + var obj; + var out; + var i; + + obj = { + 'foo': 1, + 'bar': 2, + 'baz': 3, + 'qux': 4 + }; + tokens = [ 'foo', 'bar', 'baz', 'qux' ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = resolveToken( obj, tokens[ i % tokens.length ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/repl.txt b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/repl.txt new file mode 100644 index 000000000000..6d89f541f66b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( container, token ) + Resolves a JSON Pointer reference token against a container. + + For arrays, returns an integer index. The '-' token is resolved to the + array length. Invalid array indices throw an error. + + For objects, returns the token string as-is. + + Parameters + ---------- + container: Object|Array + Parent container. + + token: string + JSON Pointer reference token. + + Returns + ------- + key: string|integer + Resolved key. + + Examples + -------- + > var key = {{alias}}( [ 1, 2, 3 ], '0' ) + 0 + > key = {{alias}}( [ 1, 2, 3 ], '-' ) + 3 + > key = {{alias}}( { 'foo': 'bar' }, 'foo' ) + 'foo' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts new file mode 100644 index 000000000000..e86b9a5e7c6b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts @@ -0,0 +1,51 @@ +/* +* @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 + +/** +* Resolves a JSON Pointer reference token against a container. +* +* @param container - parent container +* @param token - reference token +* @throws invalid array index in pointer +* @returns resolved key +* +* @example +* var arr = [ 1, 2, 3 ]; +* var key = resolveToken( arr, '0' ); +* // returns 0 +* +* @example +* var arr = [ 1, 2, 3 ]; +* var key = resolveToken( arr, '-' ); +* // returns 3 +* +* @example +* var obj = { +* 'foo': 'bar' +* }; +* var key = resolveToken( obj, 'foo' ); +* // returns 'foo' +*/ +declare function resolveToken( container: Array | Record, token: string ): string | number; + + +// EXPORTS // + +export = resolveToken; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/test.ts b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/test.ts new file mode 100644 index 000000000000..2d8c742052fc --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/test.ts @@ -0,0 +1,35 @@ +/* +* @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 resolveToken = require( './index' ); + + +// TESTS // + +// The function returns a string or number... +{ + resolveToken( [ 1, 2, 3 ], '0' ); // $ExpectType string | number + resolveToken( { 'foo': 'bar' }, 'foo' ); // $ExpectType string | number +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + resolveToken(); // $ExpectError + resolveToken( [ 1, 2, 3 ] ); // $ExpectError + resolveToken( [ 1, 2, 3 ], '0', 123 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/examples/index.js b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/examples/index.js new file mode 100644 index 000000000000..263bbd0880ac --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/examples/index.js @@ -0,0 +1,47 @@ +/** +* @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 resolveToken = require( './../lib' ); + +var arr = [ 'apple', 'banana', 'cherry' ]; +var obj = { + 'foo': 'bar', + '0': 'baz' +}; + +var key = resolveToken( arr, '0' ); +console.log( key ); +// => 0 + +key = resolveToken( arr, '2' ); +console.log( key ); +// => 2 + +key = resolveToken( arr, '-' ); +console.log( key ); +// => 3 + +key = resolveToken( obj, 'foo' ); +console.log( key ); +// => 'foo' + +key = resolveToken( obj, '0' ); +console.log( key ); +// => '0' diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/index.js b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/index.js new file mode 100644 index 000000000000..1b1b64625119 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/index.js @@ -0,0 +1,51 @@ +/** +* @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 reference token against a container. +* +* @module @stdlib/utils/json-pointer/base/resolve-token +* +* @example +* var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' ); +* +* var obj = { +* 'foo': 'bar' +* }; +* var arr = [ 1, 2, 3 ]; +* +* var key = resolveToken( obj, 'foo' ); +* // returns 'foo' +* +* key = resolveToken( arr, '0' ); +* // returns 0 +* +* key = resolveToken( arr, '-' ); +* // returns 3 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/main.js b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/main.js new file mode 100644 index 000000000000..9843a5784644 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/lib/main.js @@ -0,0 +1,84 @@ +/** +* @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 format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var RE_ARRAY_INDEX = /^(?:0|[1-9][0-9]*)$/; + + +// MAIN // + +/** +* Resolves a JSON Pointer reference token against a container. +* +* ## Notes +* +* - For arrays, returns an integer index. +* - For arrays, the `-` token is resolved to `container.length`. +* - For objects, returns the token string as-is. +* +* @param {(Object|Array)} container - parent container +* @param {string} token - reference token +* @throws {Error} invalid array index in pointer +* @returns {(string|NonNegativeInteger)} resolved key +* +* @example +* var arr = [ 1, 2, 3 ]; +* var key = resolveToken( arr, '0' ); +* // returns 0 +* +* @example +* var arr = [ 1, 2, 3 ]; +* var key = resolveToken( arr, '-' ); +* // returns 3 +* +* @example +* var obj = { +* 'foo': 'bar' +* }; +* var key = resolveToken( obj, 'foo' ); +* // returns 'foo' +*/ +function resolveToken( container, token ) { + var idx; + + if ( isArray( container ) ) { + if ( token === '-' ) { + return container.length; + } + if ( !RE_ARRAY_INDEX.test( token ) ) { + throw new Error( format( 'invalid argument. Invalid array index in pointer. Value: `%s`.', token ) ); + } + idx = parseInt( token, 10 ); + return idx; + } + return token; +} + + +// EXPORTS // + +module.exports = resolveToken; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/package.json b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/package.json new file mode 100644 index 000000000000..6183221aae4c --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/utils/json-pointer/base/resolve-token", + "version": "0.0.0", + "description": "Resolve a JSON Pointer reference token against a container.", + "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", + "utilities", + "utility", + "utils", + "util", + "base", + "json", + "pointer", + "json-pointer", + "rfc6901", + "token", + "resolve", + "key" + ] +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/test/test.js b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/test/test.js new file mode 100644 index 000000000000..0bbfd2045720 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/test/test.js @@ -0,0 +1,105 @@ +/** +* @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 tape = require( 'tape' ); +var resolveToken = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof resolveToken, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function resolves a valid numeric token to an integer for arrays', function test( t ) { + var arr; + + arr = [ 10, 20, 30 ]; + + t.strictEqual( resolveToken( arr, '0' ), 0, 'returns expected value' ); + t.strictEqual( resolveToken( arr, '1' ), 1, 'returns expected value' ); + t.strictEqual( resolveToken( arr, '2' ), 2, 'returns expected value' ); + t.strictEqual( resolveToken( arr, '10' ), 10, 'returns expected value' ); + t.strictEqual( resolveToken( arr, '100' ), 100, 'returns expected value' ); + t.end(); +}); + +tape( 'the function resolves the `-` token to the array length for arrays', function test( t ) { + t.strictEqual( resolveToken( [], '-' ), 0, 'returns expected value' ); + t.strictEqual( resolveToken( [ 1 ], '-' ), 1, 'returns expected value' ); + t.strictEqual( resolveToken( [ 1, 2, 3 ], '-' ), 3, 'returns expected value' ); + t.strictEqual( resolveToken( [ 1, 2, 3, 4, 5 ], '-' ), 5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error for invalid array tokens', function test( t ) { + var values; + var arr; + var i; + + arr = [ 1, 2, 3 ]; + values = [ + 'foo', + '-1', + '01', + '00', + '1.5', + '', + ' ', + ' 0', + '0 ', + 'NaN', + 'null', + 'undefined', + '--' + ]; + 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() { + resolveToken( arr, value ); + }; + } +}); + +tape( 'the function returns the token as-is for objects', function test( t ) { + var obj; + + obj = { + 'foo': 1, + 'bar': 2, + '0': 3, + '-': 4 + }; + + t.strictEqual( resolveToken( obj, 'foo' ), 'foo', 'returns expected value' ); + t.strictEqual( resolveToken( obj, 'bar' ), 'bar', 'returns expected value' ); + t.strictEqual( resolveToken( obj, '0' ), '0', 'returns expected value' ); + t.strictEqual( resolveToken( obj, '-' ), '-', 'returns expected value' ); + t.strictEqual( resolveToken( obj, 'missing' ), 'missing', 'returns expected value' ); + t.end(); +});