diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/README.md b/lib/node_modules/@stdlib/utils/jsonify-keys/README.md new file mode 100644 index 000000000000..3e002935a7f0 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/README.md @@ -0,0 +1,84 @@ + + +# jsonifyKeys + +> Return a normalized string that can be parsed as [JSON][json]. + +
+ +## Usage + +```javascript +var jsonifyKeys = require( '@stdlib/utils/jsonify-keys' ); +``` + +#### jsonifyKeys( str ) + +Returns a normalized string that can be parsed as [JSON][json]. + +```javascript +var out = jsonifyKeys( '{ beep: "boop" }' ); +// returns '{ "beep": "boop" }' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var jsonifyKeys = require( '@stdlib/utils/jsonify-keys' ); + +var out = jsonifyKeys( '{ beep: "boop" }' ); +console.log( out ); +// => '{ "beep": "boop" }' + +var json = JSON.parse( out ); +console.log( json ); +// => { 'beep': 'boop' } +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/jsonify-keys/benchmark/benchmark.js new file mode 100644 index 000000000000..0b46c527b52e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var jsonifyKeys = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var str; + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '{ beep: "boop",'+fromCodePoint( 97 + (i%26) ) + ':true }'; + out = jsonifyKeys( str ); + if ( out instanceof Error ) { + b.fail( out.message ); + } + } + b.toc(); + if ( out instanceof Error ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/docs/repl.txt b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/repl.txt new file mode 100644 index 000000000000..9e4d1920c608 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/repl.txt @@ -0,0 +1,21 @@ + +{{alias}}( str ) + Returns a normalized string that can be parsed as JSON. + + Parameters + ---------- + str: string + String to parse. + + Returns + ------- + out: string + Normalized string that can be parsed as JSON. + + Examples + -------- + > var obj = {{alias}}( '{ beep: "boop" }' ) + '{ "beep": "boop" }' + + See Also + -------- diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/index.d.ts new file mode 100644 index 000000000000..c8621bb0a62d --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/index.d.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 + +/** +* Returns a normalized string that can be parsed as JSON. +* +* @param str - string to parse +* @returns normalized string +* +* @example +* var obj = jsonifyKeys( '{ beep: "boop" }' ); +* // returns '{ "beep": "boop" }' +*/ +declare function jsonifyKeys( str: string ): string; + + +// EXPORTS // + +export = jsonifyKeys; diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/test.ts b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/test.ts new file mode 100644 index 000000000000..38b809771720 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/test.ts @@ -0,0 +1,55 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 jsonifyKeys = require( './index' ); + + +// TESTS // + +// The function returns a parsed value... +{ + jsonifyKeys( '{ beep: "boop" }' ); // $ExpectType string + jsonifyKeys( '"beep"' ); // $ExpectType string + jsonifyKeys( '22' ); // $ExpectType string +} + +// The function does not compile if the first argument is a value other than a string... +{ + jsonifyKeys( true ); // $ExpectError + jsonifyKeys( false ); // $ExpectError + jsonifyKeys( 5 ); // $ExpectError + jsonifyKeys( [] ); // $ExpectError + jsonifyKeys( {} ); // $ExpectError + jsonifyKeys( ( x: number ): number => x ); // $ExpectError +} + +// The function does not compile if the second argument is a value other than a function... +{ + jsonifyKeys( '{ beep: "boop" }', true ); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', false ); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', 5 ); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', [] ); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', {} ); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', 'baz' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + jsonifyKeys(); // $ExpectError + jsonifyKeys( '{ beep: "boop" }', 'baz', 'boz' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/examples/index.js b/lib/node_modules/@stdlib/utils/jsonify-keys/examples/index.js new file mode 100644 index 000000000000..7b7eca968db5 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 jsonifyKeys = require( './../lib' ); + +var out = jsonifyKeys( '{ beep: "boop" }' ); +console.log( out ); +// => '{ "beep": "boop" }' + +var json = JSON.parse( out ); +console.log( json ); +// => { 'beep': 'boop' } diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/lib/index.js b/lib/node_modules/@stdlib/utils/jsonify-keys/lib/index.js new file mode 100644 index 000000000000..59fb2949788e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Return a normalized string that can be parsed as JSON. +* +* @module @stdlib/utils/jsonify-keys +* +* @example +* var jsonifyKeys = require( '@stdlib/utils/jsonify-keys' ); +* +* var obj = jsonifyKeys( '{ beep: "boop" }' ); +* // returns '{ "beep": "boop" }' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/lib/main.js b/lib/node_modules/@stdlib/utils/jsonify-keys/lib/main.js new file mode 100644 index 000000000000..5970700f7372 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var replace = require( '@stdlib/string/replace' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a normalized string that can be parsed as JSON. +* +* @param {string} str - string to parse +* @throws {TypeError} first argument must be a string +* @returns {string} normalized string +* +* @example +* var obj = jsonifyKeys( '{ beep: "boop" }' ); +* // returns '{ "beep": "boop" }' +*/ +function jsonifyKeys( str ) { + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + // Find the word that is preceded by a `{` or followed by a `:` and add quotes around it. + return replace( str, /(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '$1"$3":' ); +} + + +// EXPORTS // + +module.exports = jsonifyKeys; diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/package.json b/lib/node_modules/@stdlib/utils/jsonify-keys/package.json new file mode 100644 index 000000000000..48e17721042a --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/utils/jsonify-keys", + "version": "0.0.0", + "description": "Return a normalized string that can be parsed as JSON.", + "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", + "parse", + "try", + "trap", + "catch", + "json.parse", + "string", + "str" + ] +} diff --git a/lib/node_modules/@stdlib/utils/jsonify-keys/test/test.js b/lib/node_modules/@stdlib/utils/jsonify-keys/test/test.js new file mode 100644 index 000000000000..37633d028762 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/jsonify-keys/test/test.js @@ -0,0 +1,86 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 tape = require( 'tape' ); +var jsonifyKeys = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof jsonifyKeys, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 3.14, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + jsonifyKeys( value ); + }; + } +}); + +tape( 'the function returns a string value', function test( t ) { + var expected; + var actual; + + expected = '{ "beep": "boop" }'; + actual = jsonifyKeys( '{ beep: "boop" }' ); + t.deepEqual( actual, expected, 'deep equal' ); + + t.end(); +}); + +tape( 'the resultant string can be parsed as JSON', function test( t ) { + var expected; + var actual; + var out; + + expected = { + 'beep': 'boop' + }; + out = jsonifyKeys( '{ beep: "boop" }' ); + actual = JSON.parse( out ); + t.deepEqual( actual, expected, 'deep equal' ); + + t.end(); +});