Skip to content
Draft
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
129 changes: 129 additions & 0 deletions lib/node_modules/@stdlib/number/float32/reviver/README.md
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 -->
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' );
Copy link
Member Author

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!!

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 lib/node_modules/@stdlib/number/float32/reviver/docs/repl.txt
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
--------
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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 36 in lib/node_modules/@stdlib/number/float32/reviver/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = reviveNumber;
47 changes: 47 additions & 0 deletions lib/node_modules/@stdlib/number/float32/reviver/docs/types/test.ts
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 lib/node_modules/@stdlib/number/float32/reviver/examples/index.js
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
Loading
Loading