From 3d5e69986c938d3b85dd9807968f52808e67f263 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Tue, 6 Oct 2026 01:54:13 +0530 Subject: [PATCH] feat: add json-pointer/format --- .../utils/json-pointer/format/README.md | 128 ++++++++++++++++ .../format/benchmark/benchmark.js | 105 ++++++++++++++ .../utils/json-pointer/format/docs/repl.txt | 32 ++++ .../json-pointer/format/docs/types/index.d.ts | 51 +++++++ .../json-pointer/format/docs/types/test.ts | 45 ++++++ .../json-pointer/format/examples/index.js | 45 ++++++ .../utils/json-pointer/format/lib/index.js | 46 ++++++ .../utils/json-pointer/format/lib/main.js | 78 ++++++++++ .../utils/json-pointer/format/package.json | 71 +++++++++ .../utils/json-pointer/format/test/test.js | 137 ++++++++++++++++++ 10 files changed, 738 insertions(+) create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/README.md create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/examples/index.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/lib/index.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/lib/main.js create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/package.json create mode 100644 lib/node_modules/@stdlib/utils/json-pointer/format/test/test.js diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/README.md b/lib/node_modules/@stdlib/utils/json-pointer/format/README.md new file mode 100644 index 000000000000..a12603a81cd6 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/README.md @@ -0,0 +1,128 @@ + + +# format + +> Format an array of reference tokens into a [JSON Pointer][json-pointer] ([RFC 6901][rfc-6901]) string. + +
+ +## 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 '' +``` + +
+ + + +
+ +## 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]. + +
+ + + +
+ + + +## 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 '' +``` + +
+ + + + + +
+ +## References + +- [RFC 6901 - JavaScript Object Notation (JSON) Pointer][rfc-6901] + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/json-pointer/format/benchmark/benchmark.js new file mode 100644 index 000000000000..76f33f6ccd9b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/docs/repl.txt b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/repl.txt new file mode 100644 index 000000000000..5648b23105cd --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/repl.txt @@ -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 + 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 + -------- + diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/index.d.ts new file mode 100644 index 000000000000..2439dfde6434 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/index.d.ts @@ -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; + + +// EXPORTS // + +export = format; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/test.ts b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/test.ts new file mode 100644 index 000000000000..e7ad58683e82 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/docs/types/test.ts @@ -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 +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/examples/index.js b/lib/node_modules/@stdlib/utils/json-pointer/format/examples/index.js new file mode 100644 index 000000000000..c3302c5cef8f --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/examples/index.js @@ -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. +*/ + +'use strict'; + +var format = require( './../lib' ); + +var ptr = format( [ 'foo', 'bar' ] ); +console.log( ptr ); +// => '/foo/bar' + +ptr = format( [ 'foo', '0' ] ); +console.log( ptr ); +// => '/foo/0' + +ptr = format( [ 'a/b', 'c~d' ] ); +console.log( ptr ); +// => '/a~1b/c~0d' + +ptr = format( [ '~1' ] ); +console.log( ptr ); +// => '/~01' + +ptr = format( [ '' ] ); +console.log( ptr ); +// => '/' + +ptr = format( [] ); +console.log( ptr ); +// => '' diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/lib/index.js b/lib/node_modules/@stdlib/utils/json-pointer/format/lib/index.js new file mode 100644 index 000000000000..0d1d8ce443df --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* Format an array of reference tokens into a JSON Pointer string. +* +* @module @stdlib/utils/json-pointer/format +* +* @example +* var format = require( '@stdlib/utils/json-pointer/format' ); +* +* var ptr = format( [ 'foo', 'bar' ] ); +* // returns '/foo/bar' +* +* ptr = format( [ 'a/b', 'c~d' ] ); +* // returns '/a~1b/c~0d' +* +* ptr = format( [] ); +* // returns '' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/lib/main.js b/lib/node_modules/@stdlib/utils/json-pointer/format/lib/main.js new file mode 100644 index 000000000000..7d35b90227a5 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/lib/main.js @@ -0,0 +1,78 @@ +/** +* @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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +var isArray = require( '@stdlib/assert/is-array' ); +var replace = require( '@stdlib/string/base/replace' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* 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 {Array} tokens - array of reference tokens +* @throws {TypeError} must provide an array of strings +* @returns {string} JSON Pointer string +* +* @example +* var ptr = formatPointer( [ 'foo', 'bar' ] ); +* // returns '/foo/bar' +* +* @example +* var ptr = formatPointer( [ 'a/b', 'c~d' ] ); +* // returns '/a~1b/c~0d' +* +* @example +* var ptr = formatPointer( [] ); +* // returns '' +*/ +function formatPointer( tokens ) { + var out; + var i; + + if ( isArray( tokens ) && tokens.length === 0 ) { + return ''; + } + if ( !isStringArray( tokens ) ) { + throw new TypeError( format( 'invalid argument. Must provide an array of strings. Value: `%s`.', tokens ) ); + } + out = ''; + + // Escape reference tokens per RFC 6901 (note: '~' must be replaced before '/' to avoid corrupting sequences such as '~1'): + for ( i = 0; i < tokens.length; i++ ) { + out += '/' + replace( replace( tokens[ i ], /~/g, '~0' ), /\//g, '~1' ); + } + return out; +} + + +// EXPORTS // + +module.exports = formatPointer; diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/package.json b/lib/node_modules/@stdlib/utils/json-pointer/format/package.json new file mode 100644 index 000000000000..a4d53c09a245 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/utils/json-pointer/format", + "version": "0.0.0", + "description": "Format an array of reference tokens into a JSON Pointer string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "json", + "pointer", + "json-pointer", + "rfc6901", + "rfc", + "6901", + "format", + "serialize", + "encode", + "reference", + "token" + ] +} diff --git a/lib/node_modules/@stdlib/utils/json-pointer/format/test/test.js b/lib/node_modules/@stdlib/utils/json-pointer/format/test/test.js new file mode 100644 index 000000000000..c9ff109d3dd3 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/json-pointer/format/test/test.js @@ -0,0 +1,137 @@ +/** +* @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. +*/ + +/* eslint-disable no-useless-escape */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var parse = require( '@stdlib/utils/json-pointer/parse' ); +var format = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof format, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an array of strings', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 5, + NaN, + true, + null, + void 0, + {}, + function noop() {}, + [ 1, 2, 3 ], + [ 'foo', null ], + [ 'foo', 5 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + format( value ); + }; + } +}); + +tape( 'the function returns an empty string for an empty array', function test( t ) { + var ptr = format( [] ); + t.strictEqual( ptr, '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function formats an array of tokens into a JSON pointer', function test( t ) { + var expected; + var values; + var i; + + values = [ + [ 'foo', 'bar' ], + [ 'foo' ], + [ 'a/b', 'c~d' ], + [ '~1' ], + [ '' ], + [ '', '' ], + [ 'foo', '-' ], + [ 'foo', '0', 'bar', '12' ], + [ ' ' ], + [ 'c%d' ], + [ 'e^f' ], + [ 'g|h' ], + [ 'i\\j' ], + [ 'k\"l' ] + ]; + expected = [ + '/foo/bar', + '/foo', + '/a~1b/c~0d', + '/~01', + '/', + '//', + '/foo/-', + '/foo/0/bar/12', + '/ ', + '/c%d', + '/e^f', + '/g|h', + '/i\\j', + '/k\"l' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( format( values[ i ] ), expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function is the inverse of parse', function test( t ) { + var ptrs; + var i; + + ptrs = [ + '/foo/bar', + '/a~1b/c~0d', + '/~01', + '/', + '//', + '', + '/foo/-', + '/ ', + '/c%d', + '/e^f' + ]; + for ( i = 0; i < ptrs.length; i++ ) { + t.strictEqual( format( parse( ptrs[ i ] ) ), ptrs[ i ], 'returns expected value' ); + } + t.end(); +});