Repository navigation
feat: add utils/json-pointer/base/resolve-token
#16326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gururaj1512
wants to merge
5
commits into
stdlib-js:develop
Choose a base branch
from
gururaj1512:json-pointer/base/resolve-token
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+695
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74f5d7c
feat: add utils/json-pointer/base/resolve-token
gururaj1512 d60159a
Merge branch 'develop' into json-pointer/base/resolve-token
gururaj1512 0c5f2d2
Merge branch 'develop' into json-pointer/base/resolve-token
kgryte 092a114
Apply batched suggestions from code review
kgryte ceadf66
docs: update note
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # resolveToken | ||
|
|
||
| > Resolve a JSON Pointer reference token against a container. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| This function resolves a [JSON Pointer][json-pointer] reference token against a container, returning the appropriate key. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## 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 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - For arrays, the function returns an integer index. | ||
| - For arrays, the `-` token is resolved to the array length (i.e., `container.length`). | ||
| - For objects, the function 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```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' | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- 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://tools.ietf.org/html/rfc6901 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
82 changes: 82 additions & 0 deletions
82
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }); |
34 changes: 34 additions & 0 deletions
34
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
| {{alias}}( container, token ) | ||
| Resolves a JSON Pointer reference token against a container. | ||
|
|
||
| For arrays, the function returns an integer index. The '-' token is resolved to the | ||
| array length. Invalid array indices throw an error. | ||
|
|
||
| For objects, the function 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 | ||
| -------- | ||
|
|
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<any> | Record<string, any>, token: string ): string | number; | ||
|
Check warning on line 46 in lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts
|
||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = resolveToken; | ||
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing tests for the arguments.