Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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.

</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 -->
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();
});
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, 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
--------

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 46 in lib/node_modules/@stdlib/utils/json-pointer/base/resolve-token/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = resolveToken;
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
}
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'
Loading
Loading