Skip to content

Commit 416eac7

Browse files
committed
Auto-generated commit
1 parent 33cd844 commit 416eac7

11 files changed

Lines changed: 719 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`2593ea6`](https://github.com/stdlib-js/stdlib/commit/2593ea6b21e1190ee06c710d5cf7a597e2e245c0) - add `utils/json-pointer/base/resolve-token` [(#16326)](https://github.com/stdlib-js/stdlib/pull/16326)
1314
- [`0a22d73`](https://github.com/stdlib-js/stdlib/commit/0a22d737a2f828ebf30c8ea8ba1952d1bed155c7) - add `utils/json-pointer/base/assert/is-safe-token` [(#16320)](https://github.com/stdlib-js/stdlib/pull/16320)
1415

1516
</section>
@@ -45,6 +46,7 @@ A total of 10 issues were closed in this release:
4546

4647
<details>
4748

49+
- [`2593ea6`](https://github.com/stdlib-js/stdlib/commit/2593ea6b21e1190ee06c710d5cf7a597e2e245c0) - **feat:** add `utils/json-pointer/base/resolve-token` [(#16326)](https://github.com/stdlib-js/stdlib/pull/16326) _(by Gururaj Gurram, Athan Reines)_
4850
- [`0a22d73`](https://github.com/stdlib-js/stdlib/commit/0a22d737a2f828ebf30c8ea8ba1952d1bed155c7) - **feat:** add `utils/json-pointer/base/assert/is-safe-token` [(#16320)](https://github.com/stdlib-js/stdlib/pull/16320) _(by Gururaj Gurram, Athan Reines)_
4951
- [`a3ef833`](https://github.com/stdlib-js/stdlib/commit/a3ef8332fc0b02f15802a6edb81df0ede5457fc6) - **fix:** add support for `d`, `s`, `u`, and `v` regexp flags [(#16543)](https://github.com/stdlib-js/stdlib/pull/16543) _(by Rajan Pantha)_
5052
- [`78ad2fa`](https://github.com/stdlib-js/stdlib/commit/78ad2fa5dbb12ce808dec93197d5343745ff91b8) - **docs:** normalize trailing newlines in REPL help texts [(#16420)](https://github.com/stdlib-js/stdlib/pull/16420) _(by Philipp Burckhardt)_
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# resolveToken
22+
23+
> Resolve a JSON Pointer reference token against a container.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
This function resolves a [JSON Pointer][json-pointer] reference token against a container, returning the appropriate key.
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' );
43+
```
44+
45+
#### resolveToken( container, token )
46+
47+
Resolves a JSON Pointer reference token against a container.
48+
49+
```javascript
50+
var obj = {
51+
'foo': 'bar'
52+
};
53+
var arr = [ 1, 2, 3 ];
54+
55+
var key = resolveToken( obj, 'foo' );
56+
// returns 'foo'
57+
58+
key = resolveToken( arr, '0' );
59+
// returns 0
60+
61+
key = resolveToken( arr, '-' );
62+
// returns 3
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- For arrays, the function returns an integer index.
76+
- For arrays, the `-` token is resolved to the array length (i.e., `container.length`).
77+
- For objects, the function returns the token string as-is.
78+
- If `container` is an array and `token` is not a valid array index (or `-`), the function throws an error.
79+
80+
</section>
81+
82+
<!-- /.notes -->
83+
84+
<!-- Package usage examples. -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
93+
var resolveToken = require( '@stdlib/utils/json-pointer/base/resolve-token' );
94+
95+
var arr = [ 'apple', 'banana', 'cherry' ];
96+
var obj = {
97+
'foo': 'bar',
98+
'0': 'baz'
99+
};
100+
101+
var key = resolveToken( arr, '0' );
102+
console.log( key );
103+
// => 0
104+
105+
key = resolveToken( arr, '2' );
106+
console.log( key );
107+
// => 2
108+
109+
key = resolveToken( arr, '-' );
110+
console.log( key );
111+
// => 3
112+
113+
key = resolveToken( obj, 'foo' );
114+
console.log( key );
115+
// => 'foo'
116+
117+
key = resolveToken( obj, '0' );
118+
console.log( key );
119+
// => '0'
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
127+
128+
<section class="related">
129+
130+
</section>
131+
132+
<!-- /.related -->
133+
134+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="links">
137+
138+
[json-pointer]: https://tools.ietf.org/html/rfc6901
139+
140+
</section>
141+
142+
<!-- /.links -->
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var format = require( '@stdlib/string/format' );
25+
var pkg = require( './../package.json' ).name;
26+
var resolveToken = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( format( '%s::array', pkg ), function benchmark( b ) {
32+
var tokens;
33+
var arr;
34+
var out;
35+
var i;
36+
37+
arr = [ 1, 2, 3, 4, 5 ];
38+
tokens = [ '0', '1', '2', '3', '4', '-' ];
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
out = resolveToken( arr, tokens[ i % tokens.length ] );
43+
if ( typeof out !== 'number' ) {
44+
b.fail( 'should return a number' );
45+
}
46+
}
47+
b.toc();
48+
if ( typeof out !== 'number' ) {
49+
b.fail( 'should return a number' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( format( '%s::object', pkg ), function benchmark( b ) {
56+
var tokens;
57+
var obj;
58+
var out;
59+
var i;
60+
61+
obj = {
62+
'foo': 1,
63+
'bar': 2,
64+
'baz': 3,
65+
'qux': 4
66+
};
67+
tokens = [ 'foo', 'bar', 'baz', 'qux' ];
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
out = resolveToken( obj, tokens[ i % tokens.length ] );
72+
if ( typeof out !== 'string' ) {
73+
b.fail( 'should return a string' );
74+
}
75+
}
76+
b.toc();
77+
if ( typeof out !== 'string' ) {
78+
b.fail( 'should return a string' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( container, token )
3+
Resolves a JSON Pointer reference token against a container.
4+
5+
For arrays, the function returns an integer index. The '-' token is resolved
6+
to the array length. Invalid array indices throw an error.
7+
8+
For objects, the function returns the token string as-is.
9+
10+
Parameters
11+
----------
12+
container: Object|Array
13+
Parent container.
14+
15+
token: string
16+
JSON Pointer reference token.
17+
18+
Returns
19+
-------
20+
key: string|integer
21+
Resolved key.
22+
23+
Examples
24+
--------
25+
> var key = {{alias}}( [ 1, 2, 3 ], '0' )
26+
0
27+
> key = {{alias}}( [ 1, 2, 3 ], '-' )
28+
3
29+
> key = {{alias}}( { 'foo': 'bar' }, 'foo' )
30+
'foo'
31+
32+
See Also
33+
--------
34+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Resolves a JSON Pointer reference token against a container.
23+
*
24+
* @param container - parent container
25+
* @param token - reference token
26+
* @throws invalid array index in pointer
27+
* @returns resolved key
28+
*
29+
* @example
30+
* var arr = [ 1, 2, 3 ];
31+
* var key = resolveToken( arr, '0' );
32+
* // returns 0
33+
*
34+
* @example
35+
* var arr = [ 1, 2, 3 ];
36+
* var key = resolveToken( arr, '-' );
37+
* // returns 3
38+
*
39+
* @example
40+
* var obj = {
41+
* 'foo': 'bar'
42+
* };
43+
* var key = resolveToken( obj, 'foo' );
44+
* // returns 'foo'
45+
*/
46+
declare function resolveToken( container: Array<any> | Record<string, any>, token: string ): string | number;
47+
48+
49+
// EXPORTS //
50+
51+
export = resolveToken;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import resolveToken = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string or number...
25+
{
26+
resolveToken( [ 1, 2, 3 ], '0' ); // $ExpectType string | number
27+
resolveToken( { 'foo': 'bar' }, 'foo' ); // $ExpectType string | number
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is neither an array nor an object...
31+
{
32+
resolveToken( 'abc', '0' ); // $ExpectError
33+
resolveToken( 5, '0' ); // $ExpectError
34+
resolveToken( true, '0' ); // $ExpectError
35+
resolveToken( false, '0' ); // $ExpectError
36+
resolveToken( null, '0' ); // $ExpectError
37+
resolveToken( undefined, '0' ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided a second argument which is not a string...
41+
{
42+
resolveToken( [ 1, 2, 3 ], true ); // $ExpectError
43+
resolveToken( [ 1, 2, 3 ], false ); // $ExpectError
44+
resolveToken( [ 1, 2, 3 ], null ); // $ExpectError
45+
resolveToken( [ 1, 2, 3 ], undefined ); // $ExpectError
46+
resolveToken( [ 1, 2, 3 ], 5 ); // $ExpectError
47+
resolveToken( [ 1, 2, 3 ], [] ); // $ExpectError
48+
resolveToken( [ 1, 2, 3 ], {} ); // $ExpectError
49+
resolveToken( [ 1, 2, 3 ], ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
resolveToken(); // $ExpectError
55+
resolveToken( [ 1, 2, 3 ] ); // $ExpectError
56+
resolveToken( [ 1, 2, 3 ], '0', 123 ); // $ExpectError
57+
}

0 commit comments

Comments
 (0)