Skip to content

feat: add @stdlib/utils/jsonify-keys #5415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
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
84 changes: 84 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!--

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

-->

# jsonifyKeys

> Return a normalized string that can be parsed as [JSON][json].

<section class="usage">

## 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" }'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```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' }
```

</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]: http://www.json.org/

</section>

<!-- /.links -->
50 changes: 50 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -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();
});
21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -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
--------
36 changes: 36 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
55 changes: 55 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/examples/index.js
Original file line number Diff line number Diff line change
@@ -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' }
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions lib/node_modules/@stdlib/utils/jsonify-keys/lib/main.js
Original file line number Diff line number Diff line change
@@ -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;
Loading