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
128 changes: 128 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/parse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!--

@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.

-->

# parse

> Parse a [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) string into an array of reference tokens.

<section class="usage">

## Usage

```javascript
var parse = require( '@stdlib/utils/json-pointer/parse' );
```

#### parse( pointer )

Parses a [JSON Pointer][json-pointer] string into an array of unescaped reference tokens.

```javascript
var tokens = parse( '/foo/bar' );
// returns [ 'foo', 'bar' ]

tokens = parse( '/a~1b/c~0d' );
// returns [ 'a/b', 'c~d' ]

tokens = parse( '' );
// returns []
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Per [RFC 6901][rfc-6901], an empty string refers to the root of the document and returns an empty array `[]`.
- Non-empty pointers must begin with a `/` character.
- Escape sequences are decoded sequentially: `~1` is unescaped to `/` before `~0` is unescaped to `~`.

</section>

<!-- /.notes -->

<section class="examples">

<!-- eslint no-undef: "error" -->

## Examples

```javascript
var parse = require( '@stdlib/utils/json-pointer/parse' );

var tokens = parse( '/foo/bar' );
// returns [ 'foo', 'bar' ]

tokens = parse( '/foo/0' );
// returns [ 'foo', '0' ]

tokens = parse( '/a~1b/c~0d' );
// returns [ 'a/b', 'c~d' ]

tokens = parse( '/~01' );
// returns [ '~1' ]

tokens = parse( '/' );
// returns [ '' ]

tokens = parse( '' );
// returns []
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

## References

- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901]

</section>

<!-- /.references -->

<!-- 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://datatracker.ietf.org/doc/html/rfc6901

[rfc-6901]: https://datatracker.ietf.org/doc/html/rfc6901

<!-- <related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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 bench = require( '@stdlib/bench' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var parse = require( './../lib' );


// MAIN //

bench( format( '%s:shallow', pkg ), function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = parse( '/foo/bar' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( out.length !== 2 ) {
b.fail( 'should return expected tokens' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:escaped', pkg), function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = parse( '/a~1b/c~0d/foo/0' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( out.length !== 4 ) {
b.fail( 'should return expected tokens' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:deep', pkg ), function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = parse( '/a/b/c/d/e/f/g/h' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( out.length !== 8 ) {
b.fail( 'should return expected tokens' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:root', pkg ), function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = parse( '' );
if ( typeof out !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( out.length !== 0 ) {
b.fail( 'should return empty array' );
}
b.pass( 'benchmark finished' );
b.end();
});
32 changes: 32 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/parse/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

{{alias}}( pointer )
Parses a JSON Pointer string into an array of reference tokens.

Per RFC 6901, an empty string refers to the root and returns an empty array.
Non-empty pointers must begin with a '/' character.

Escape sequences are decoded: `~1` is unescaped to `/` before `~0` is
unescaped to `~`.

Parameters
----------
pointer: string
JSON Pointer string.

Returns
-------
tokens: Array<string>
Array of reference tokens.

Examples
--------
> var tokens = {{alias}}( '/foo/bar' )
[ 'foo', 'bar' ]
> tokens = {{alias}}( '/a~1b/c~0d' )
[ 'a/b', 'c~d' ]
> tokens = {{alias}}( '' )
[]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @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

/**
* Parses a JSON Pointer string into an array of reference tokens.
*
* ## Notes
*
* - Implements parsing per RFC 6901.
* - An empty string returns an empty array (refers to the root of the document).
* - Escape sequences are decoded: `~1` is unescaped to `/` before `~0` is unescaped to `~`.
*
* @param pointer - JSON Pointer string
* @throws must provide a string
* @throws pointer must be empty or start with a `/` character
* @returns array of reference tokens
*
* @example
* var tokens = parse( '/foo/bar' );
* // returns [ 'foo', 'bar' ]
*
* @example
* var tokens = parse( '/a~1b/c~0d' );
* // returns [ 'a/b', 'c~d' ]
*
* @example
* var tokens = parse( '' );
* // returns []
*/
declare function parse( pointer: string ): Array<string>;


// EXPORTS //

export = parse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @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 parse = require( './index' );


// TESTS //

// The function returns an array of strings...
{
parse( '/foo/bar' ); // $ExpectType string[]
parse( '' ); // $ExpectType string[]
}

// The compiler throws an error if the function is provided an argument which is not a string...
{
parse( 123 ); // $ExpectError
parse( true ); // $ExpectError
parse( false ); // $ExpectError
parse( null ); // $ExpectError
parse( undefined ); // $ExpectError
parse( [] ); // $ExpectError
parse( {} ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid number of arguments...
{
parse(); // $ExpectError
parse( '/foo', 2 ); // $ExpectError
}
Loading