-
-
Notifications
You must be signed in to change notification settings - Fork 832
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
aayush0325
wants to merge
18
commits into
stdlib-js:develop
Choose a base branch
from
aayush0325:utils-jsonify-keys
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d6ee388
feat: initial commit
aayush0325 f8cdc01
feat: add utils/jsonify-keys
aayush0325 078c58e
docs: update comments for better readablity
aayush0325 0b8327d
docs: update comments for better readablity
aayush0325 0d99797
chore: update copyright years
stdlib-bot 15040d8
chore: fix linting error
aayush0325 f987845
chore: fix linting errors
aayush0325 a3a07b3
docs: use more consistent docs
aayush0325 fec7f5d
chore: use consistent spacing
aayush0325 5fc5425
chore: use consistent spacing
aayush0325 3cf60f0
chore: use consistent spacing
aayush0325 e0d11bd
chore: code review
aayush0325 05ffd69
fix: use isError instead of instanceof due to realms
aayush0325 30987ef
refactor: return a normalized string instead of a parsed JSON
aayush0325 e671ff8
refactor: update tests, examples and docs
aayush0325 c086e1b
docs: update repl.txt and types
aayush0325 2743704
docs: more readable comments
aayush0325 a561f1c
Merge remote-tracking branch 'upstream/develop' into utils-jsonify-keys
stdlib-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# JSONify keys | ||
|
||
> Parse a [JSON][json]-like string as [JSON][json]. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var jsonifyKeys = require( '@stdlib/utils/jsonify-keys' ); | ||
``` | ||
|
||
#### jsonifyKeys( str\[, reviver] ) | ||
|
||
Parses a [JSON][json]-like `string` as [JSON][json]. | ||
|
||
```javascript | ||
var out = jsonifyKeys( '{"beep":"boop"}' ); | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// returns {'beep':'boop'} | ||
``` | ||
|
||
If unable to parse a `string` as [JSON][json], the function returns an error. | ||
|
||
```javascript | ||
var out = jsonifyKeys( 'beep' ); | ||
// returns <SyntaxError> | ||
``` | ||
|
||
To transform the `string` being parsed, provide a `reviver`. | ||
|
||
```javascript | ||
function reviver( key, value ) { | ||
if ( key === '' ) { | ||
return value; | ||
} | ||
if ( key === 'beep' ) { | ||
return value; | ||
} | ||
} | ||
|
||
var str = '{ beep: "boop", a: "b"}'; | ||
var out = jsonifyKeys( str, reviver ); | ||
// returns {'beep':'boop'} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a `TypeError` if provided **any** value which is not a `string`. | ||
|
||
```javascript | ||
var out = JSON.parse( null ); | ||
// returns null | ||
|
||
out = jsonifyKeys( null ); | ||
// throws <TypeError> | ||
``` | ||
|
||
- In contrast to the native [`JSON.parse()`][json-parse], this implementation does **not** throw a `SyntaxError` if unable to parse a `string` as [JSON][json]. | ||
|
||
```javascript | ||
var out = jsonifyKeys( '{ beep: "boop}' ); | ||
// returns <SyntaxError> | ||
|
||
out = JSON.parse( '{ "beep": "boop}' ); | ||
// throws <SyntaxError> | ||
``` | ||
|
||
- In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a `TypeError` if provided a `reviver` argument which is **not** a `function`. | ||
|
||
```javascript | ||
var out = JSON.parse( '{"a":"b"}', [] ); | ||
// returns {'a':'b'} | ||
|
||
out = jsonifyKeys( '{ a: "b"}', [] ); | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// throws <TypeError> | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var jsonifyKeys = require( '@stdlib/utils/jsonify-keys' ); | ||
|
||
var out; | ||
|
||
out = jsonifyKeys( '{ beep: "boop"}' ); | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// returns {'beep':'boop'} | ||
|
||
out = jsonifyKeys( '3.14' ); | ||
// returns 3.14 | ||
|
||
out = jsonifyKeys( 'true' ); | ||
// returns true | ||
|
||
out = jsonifyKeys( 'null' ); | ||
// returns null | ||
|
||
out = jsonifyKeys( '{"beep":"boop}' ); | ||
// returns <SyntaxError> | ||
``` | ||
|
||
</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/ | ||
|
||
[json-parse]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
50 changes: 50 additions & 0 deletions
50
lib/node_modules/@stdlib/utils/jsonify-keys/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'; | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out = jsonifyKeys( str ); | ||
if ( out instanceof Error ) { | ||
b.fail( out.message ); | ||
} | ||
} | ||
b.toc(); | ||
if ( out instanceof Error ) { | ||
b.fail( 'should return JSON' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
{{alias}}( str[, reviver] ) | ||
Attempts to parse a JSON-like string as JSON. | ||
|
||
Function behavior differs from `JSON.parse()` as follows: | ||
|
||
- throws a `TypeError` if provided any value which is not a string. | ||
- throws a `TypeError` if provided a `reviver` argument which is not a | ||
function. | ||
- returns, rather than throws, a `SyntaxError` if unable to parse a string | ||
as JSON. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
String to parse. | ||
|
||
reviver: Function (optional) | ||
Transformation function. | ||
|
||
Returns | ||
------- | ||
out: any|Error | ||
Parsed value or an error. | ||
|
||
Examples | ||
-------- | ||
> var obj = {{alias}}( '{ beep: "boop"}' ) | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ 'beep': 'boop' } | ||
|
||
// Provide a reviver: | ||
> function reviver( key, value ) { | ||
... if ( key === '' ) { return value; } | ||
... if ( key === 'beep' ) { return value; } | ||
... }; | ||
> var str = '{ beep: "boop", a: "b" }'; | ||
> var out = {{alias}}( str, reviver ) | ||
{ 'beep': 'boop' } | ||
|
||
See Also | ||
-------- |
37 changes: 37 additions & 0 deletions
37
lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* @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 | ||
|
||
/** | ||
* Attempts to parse a JSON-like string as JSON. | ||
* | ||
* @param str - string to parse | ||
* @param reviver - transformation function | ||
* @returns parsed value or parse error | ||
* | ||
* @example | ||
* var obj = jsonifyKeys( '{ beep: "boop" }' ); | ||
* // returns {'beep':'boop'} | ||
*/ | ||
declare function jsonifyKeys( str: string, reviver?: Function ): any; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = jsonifyKeys; |
55 changes: 55 additions & 0 deletions
55
lib/node_modules/@stdlib/utils/jsonify-keys/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 any | ||
jsonifyKeys( '"beep"' ); // $ExpectType any | ||
jsonifyKeys( '22' ); // $ExpectType any | ||
} | ||
|
||
// 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 | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/utils/jsonify-keys/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @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"}' ); | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log( out ); | ||
// => {'beep':'boop'} | ||
|
||
out = jsonifyKeys( '3.14' ); | ||
console.log( out ); | ||
// => 3.14 | ||
|
||
out = jsonifyKeys( 'true' ); | ||
console.log( out ); | ||
// => true | ||
|
||
out = jsonifyKeys( 'null' ); | ||
console.log( out ); | ||
// => null | ||
|
||
out = jsonifyKeys( '{ beep: "boop }' ); | ||
console.log( out instanceof Error ); | ||
// => true |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im open to changing this to a more accurate description if you have any suggestions @kgryte!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait...wasn't the original idea to simply normalize a JSON-like string to something that could be parsed as JSON? This looks to both normalize and parse, which is more than is likely needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, the idea of
jsonify-keys
should be to normalize a string. That string could then be separately passed toutils/parse-json
through composition. Normalization and parsing are two separate operations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noted! pushing the required changes soon.