-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add number/float32/reviver
#9678
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
Draft
Neerajpathak07
wants to merge
1
commit into
stdlib-js:develop
Choose a base branch
from
Neerajpathak07:float32/reviver
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.
+690
−0
Draft
Changes from all commits
Commits
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
129 changes: 129 additions & 0 deletions
129
lib/node_modules/@stdlib/number/float32/reviver/README.md
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,129 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # reviveNumber | ||
|
|
||
| > Revive a JSON-serialized number. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var reviveNumber = require( '@stdlib/number/float32/reviver' ); | ||
| ``` | ||
|
|
||
| #### reviveNumber( key, value ) | ||
|
|
||
| Revives a JSON-serialized number. | ||
|
|
||
| ```javascript | ||
| var parseJSON = require( '@stdlib/utils/parse-json' ); | ||
|
|
||
| var str = '{"type":"float32","value":"NaN"}'; | ||
|
|
||
| var buf = parseJSON( str, reviveNumber ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| For details on the JSON serialization format, see [`@stdlib/number/float32/to-json`][@stdlib/number/float32/to-json]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var parseJSON = require( '@stdlib/utils/parse-json' ); | ||
| var number2jsonf = require( '@stdlib/number/float32/to-json' ); | ||
| var reviveNumber = require( '@stdlib/number/float32/reviver' ); | ||
|
|
||
| var str = JSON.stringify( number2jsonf( NaN ) ); | ||
| console.log( str ); | ||
| // => '{"type":"float32","value":"NaN"}' | ||
|
|
||
| var out = parseJSON( str, reviveNumber ); | ||
| if ( out instanceof Error ) { | ||
| throw out; | ||
| } | ||
| console.log( out ); | ||
| // => NaN | ||
| ``` | ||
|
|
||
| </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"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| * * * | ||
|
|
||
| ## See Also | ||
|
|
||
| - <span class="package-name">[`@stdlib/number/float64/to-json`][@stdlib/number/float32/to-json]</span><span class="delimiter">: </span><span class="description">return a JSON representation of a number.</span> | ||
|
|
||
| </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"> | ||
|
|
||
| [@stdlib/number/float32/to-json]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float32/to-json | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
72 changes: 72 additions & 0 deletions
72
lib/node_modules/@stdlib/number/float32/reviver/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,72 @@ | ||
| /** | ||
| * @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 parseJSON = require( '@stdlib/utils/parse-json' ); | ||
| var number2jsonf = require( '@stdlib/number/float32/to-json' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var reviver = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values = [ '{"type":"float32","value":"NaN"}', '{"type":"float32","value":"Infinity"}', '{"type":"float32","value":"-Infinity"}']; | ||
| var o; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| o = parseJSON( values[ i%values.length ], reviver ); | ||
| if ( o instanceof Error ) { | ||
| b.fail( 'should not return an error' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
|
|
||
| if ( o instanceof Error ) { | ||
| b.fail( 'should not return an error' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( pkg+'::number', function benchmark( b ) { | ||
| var str; | ||
| var o; | ||
| var i; | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| str = JSON.stringify( number2jsonf( 3.14 ) ); | ||
| o = parseJSON( str, reviver ); | ||
| if ( o instanceof Error ) { | ||
| b.fail( 'should not return an error' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
|
|
||
| if ( o instanceof Error ) { | ||
| b.fail( 'should not return an error' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
38 changes: 38 additions & 0 deletions
38
lib/node_modules/@stdlib/number/float32/reviver/docs/repl.txt
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,38 @@ | ||
|
|
||
| {{alias}}( key, value ) | ||
| Revives a JSON-serialized number. | ||
|
|
||
| The serialization format for a numberis a number itself | ||
| Or can have a object having the following | ||
| fields: | ||
|
|
||
| - type: value type (float32). | ||
| - value: 'NaN' or '+Infinity' or '-Infinity'. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| key: string | ||
| Key. | ||
|
|
||
| value: any | ||
| Value. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: any | ||
| Value. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var str = '{"type":"float32","value":"NaN"}'; | ||
| > var out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} ) | ||
| NaN | ||
| > str = '{"type":"float32","value":"+Infinity"}'; | ||
| > out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} ) | ||
| Infinity | ||
| > str = '{"type":"float32","value":"-Infinity"}'; | ||
| > out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} ) | ||
| -Infinity | ||
|
|
||
| See Also | ||
| -------- |
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/number/float32/reviver/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,41 @@ | ||
| /* | ||
| * @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 | ||
|
|
||
| /** | ||
| * Revives a JSON-serialized number. | ||
| * | ||
| * @param key - key | ||
| * @param value - value | ||
| * @returns value | ||
| * | ||
| * @example | ||
| * var parseJSON = require( '@stdlib/utils/parse-json' ); | ||
| * | ||
| * var str = '{"type":"float32","value":"NaN"}'; | ||
| * | ||
| * var out = parseJSON( str, reviveNumber ); | ||
| * // returns NaN | ||
| */ | ||
| declare function reviveNumber( key: string, value: any ): any; | ||
|
Check warning on line 36 in lib/node_modules/@stdlib/number/float32/reviver/docs/types/index.d.ts
|
||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = reviveNumber; | ||
47 changes: 47 additions & 0 deletions
47
lib/node_modules/@stdlib/number/float32/reviver/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,47 @@ | ||
| /* | ||
| * @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 reviveNumber = require( '../../lib/main' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function revives a JSON-serialized number... | ||
| { | ||
| const o = { | ||
| 'type': 'float64', | ||
| 'value': 'NaN' | ||
| }; | ||
| reviveNumber( 'key', o ); // $ExpectType any | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a first argument that is not a string or number... | ||
| { | ||
| reviveNumber( true, 1 ); // $ExpectError | ||
| reviveNumber( false, 1 ); // $ExpectError | ||
| reviveNumber( null, 1 ); // $ExpectError | ||
| reviveNumber( undefined, 1 ); // $ExpectError | ||
| reviveNumber( [], 1 ); // $ExpectError | ||
| reviveNumber( {}, 1 ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| reviveNumber(); // $ExpectError | ||
| reviveNumber( 'foo' ); // $ExpectError | ||
| } |
58 changes: 58 additions & 0 deletions
58
lib/node_modules/@stdlib/number/float32/reviver/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,58 @@ | ||
| /** | ||
| * @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 parseJSON = require( '@stdlib/utils/parse-json' ); | ||
| var number2jsonf = require( '@stdlib/number/float32/to-json' ); | ||
| var PINF = require( '@stdlib/constants/float32/pinf' ); | ||
| var NINF = require( '@stdlib/constants/float32/ninf' ); | ||
| var reviver = require( './../lib' ); | ||
|
|
||
| var str = JSON.stringify( number2jsonf( PINF ) ); | ||
| console.log( str ); | ||
| // => '{"type":"float32","value":"+Infinity"}' | ||
|
|
||
| var out = parseJSON( str, reviver ); | ||
| if ( out instanceof Error ) { | ||
| throw out; | ||
| } | ||
| console.log( out ); | ||
| // => Infinity | ||
|
|
||
| str = JSON.stringify( number2jsonf( NINF ) ); | ||
| console.log( str ); | ||
| // => '{"type":"float32","value":"-Infinity"}' | ||
|
|
||
| out = parseJSON( str, reviver ); | ||
| if ( out instanceof Error ) { | ||
| throw out; | ||
| } | ||
| console.log( out ); | ||
| // => -Infinity | ||
|
|
||
| str = JSON.stringify( number2jsonf( NaN ) ); | ||
| console.log( str ); | ||
| // => '{"type":"float32","value":"NaN"}' | ||
|
|
||
| out = parseJSON( str, reviver ); | ||
| if ( out instanceof Error ) { | ||
| throw out; | ||
| } | ||
| console.log( out ); | ||
| // => NaN |
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.
Will pull from upstream once the PR on this is merged!!