Skip to content
Draft
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/format/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.

-->

# format

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

<section class="usage">

## Usage

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

#### format( tokens )

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

```javascript
var ptr = format( [ 'foo', 'bar' ] );
// returns '/foo/bar'

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

ptr = format( [] );
// returns ''
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Per [RFC 6901][rfc-6901], an empty array refers to the root of the document and returns an empty string `''`.
- Escape sequences are encoded sequentially: `~` is escaped to `~0` before `/` is escaped to `~1`.
- This function is the inverse of [`@stdlib/utils/json-pointer/parse`][@stdlib/utils/json-pointer/parse].

</section>

<!-- /.notes -->

<section class="examples">

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

## Examples

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

var ptr = format( [ 'foo', 'bar' ] );
// returns '/foo/bar'

ptr = format( [ 'foo', '0' ] );
// returns '/foo/0'

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

ptr = format( [ '~1' ] );
// returns '/~01'

ptr = format( [ '' ] );
// returns '/'

ptr = format( [] );
// 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

[@stdlib/utils/json-pointer/parse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/json-pointer/parse

</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 formatTokens = 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 = formatTokens( [ 'foo', 'bar' ] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( out !== '/foo/bar' ) {
b.fail( 'should return expected value' );
}
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 = formatTokens( [ 'a/b', 'c~d', 'foo', '0' ] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( out !== '/a~1b/c~0d/foo/0' ) {
b.fail( 'should return expected value' );
}
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 = formatTokens( [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( out !== '/a/b/c/d/e/f/g/h' ) {
b.fail( 'should return expected value' );
}
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 = formatTokens( [] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( out !== '' ) {
b.fail( 'should return empty string' );
}
b.pass( 'benchmark finished' );
b.end();
});
32 changes: 32 additions & 0 deletions lib/node_modules/@stdlib/utils/json-pointer/format/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

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

Per RFC 6901, an empty array returns an empty string (refers to the root of
the document).

Escape sequences are encoded: `~` is escaped to `~0` before `/` is escaped
to `~1`.

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

Returns
-------
pointer: string
JSON Pointer string.

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

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

/**
* Formats an array of reference tokens into a JSON Pointer string.
*
* ## Notes
*
* - Implements formatting per RFC 6901.
* - An empty array returns an empty string (refers to the root of the document).
* - Escape sequences are encoded: `~` is escaped to `~0` before `/` is escaped to `~1`.
*
* @param tokens - array of reference tokens
* @throws must provide an array of strings
* @returns JSON Pointer string
*
* @example
* var ptr = format( [ 'foo', 'bar' ] );
* // returns '/foo/bar'
*
* @example
* var ptr = format( [ 'a/b', 'c~d' ] );
* // returns '/a~1b/c~0d'
*
* @example
* var ptr = format( [] );
* // returns ''
*/
declare function format( tokens: Array<string> ): string;


// EXPORTS //

export = format;
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 format = require( './index' );


// TESTS //

// The function returns a string...
{
format( [ 'foo', 'bar' ] ); // $ExpectType string
format( [] ); // $ExpectType string
}

// The compiler throws an error if the function is provided an argument which is not an array of strings...
{
format( 'abc' ); // $ExpectError
format( 123 ); // $ExpectError
format( true ); // $ExpectError
format( false ); // $ExpectError
format( null ); // $ExpectError
format( undefined ); // $ExpectError
format( {} ); // $ExpectError
}

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