Skip to content

Commit 602917c

Browse files
feat: add initial setup
1 parent 279f181 commit 602917c

File tree

10 files changed

+690
-0
lines changed

10 files changed

+690
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# reviveNumber
22+
23+
> Revive a JSON-serialized number.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var reviveNumber = require( '@stdlib/number/float32/reviver' );
41+
```
42+
43+
#### reviveNumber( key, value )
44+
45+
Revives a JSON-serialized number.
46+
47+
```javascript
48+
var parseJSON = require( '@stdlib/utils/parse-json' );
49+
50+
var str = '{"type":"float32","value":"NaN"}';
51+
52+
var buf = parseJSON( str, reviveNumber );
53+
// returns NaN
54+
```
55+
56+
For details on the JSON serialization format, see [`@stdlib/number/float32/to-json`][@stdlib/number/float32/to-json].
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<!-- Package usage examples. -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var parseJSON = require( '@stdlib/utils/parse-json' );
80+
var number2jsonf = require( '@stdlib/number/float32/to-json' );
81+
var reviveNumber = require( '@stdlib/number/float32/reviver' );
82+
83+
var str = JSON.stringify( number2jsonf( NaN ) );
84+
console.log( str );
85+
// => '{"type":"float32","value":"NaN"}'
86+
87+
var out = parseJSON( str, reviveNumber );
88+
if ( out instanceof Error ) {
89+
throw out;
90+
}
91+
console.log( out );
92+
// => NaN
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
* * *
112+
113+
## See Also
114+
115+
- <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>
116+
117+
</section>
118+
119+
<!-- /.related -->
120+
121+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="links">
124+
125+
[@stdlib/number/float32/to-json]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float32/to-json
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var parseJSON = require( '@stdlib/utils/parse-json' );
25+
var number2jsonf = require( '@stdlib/number/float32/to-json' );
26+
var pkg = require( './../package.json' ).name;
27+
var reviver = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values = [ '{"type":"float32","value":"NaN"}', '{"type":"float32","value":"Infinity"}', '{"type":"float32","value":"-Infinity"}'];
34+
var o;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
o = parseJSON( values[ i%values.length ], reviver );
40+
if ( o instanceof Error ) {
41+
b.fail( 'should not return an error' );
42+
}
43+
}
44+
b.toc();
45+
46+
if ( o instanceof Error ) {
47+
b.fail( 'should not return an error' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
52+
53+
bench( pkg+'::number', function benchmark( b ) {
54+
var str;
55+
var o;
56+
var i;
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
str = JSON.stringify( number2jsonf( 3.14 ) );
60+
o = parseJSON( str, reviver );
61+
if ( o instanceof Error ) {
62+
b.fail( 'should not return an error' );
63+
}
64+
}
65+
b.toc();
66+
67+
if ( o instanceof Error ) {
68+
b.fail( 'should not return an error' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( key, value )
3+
Revives a JSON-serialized number.
4+
5+
The serialization format for a numberis a number itself
6+
Or can have a object having the following
7+
fields:
8+
9+
- type: value type (float32).
10+
- value: 'NaN' or '+Infinity' or '-Infinity'.
11+
12+
Parameters
13+
----------
14+
key: string
15+
Key.
16+
17+
value: any
18+
Value.
19+
20+
Returns
21+
-------
22+
out: any
23+
Value.
24+
25+
Examples
26+
--------
27+
> var str = '{"type":"float32","value":"NaN"}';
28+
> var out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} )
29+
NaN
30+
> str = '{"type":"float32","value":"+Infinity"}';
31+
> out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} )
32+
Infinity
33+
> str = '{"type":"float32","value":"-Infinity"}';
34+
> out = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} )
35+
-Infinity
36+
37+
See Also
38+
--------
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Revives a JSON-serialized number.
23+
*
24+
* @param key - key
25+
* @param value - value
26+
* @returns value
27+
*
28+
* @example
29+
* var parseJSON = require( '@stdlib/utils/parse-json' );
30+
*
31+
* var str = '{"type":"float32","value":"NaN"}';
32+
*
33+
* var out = parseJSON( str, reviveNumber );
34+
* // returns NaN
35+
*/
36+
declare function reviveNumber( key: string, value: any ): any;
37+
38+
39+
// EXPORTS //
40+
41+
export = reviveNumber;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import reviveNumber = require( '../../lib/main' );
20+
21+
22+
// TESTS //
23+
24+
// The function revives a JSON-serialized number...
25+
{
26+
const o = {
27+
'type': 'float64',
28+
'value': 'NaN'
29+
};
30+
reviveNumber( 'key', o ); // $ExpectType any
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument that is not a string or number...
34+
{
35+
reviveNumber( true, 1 ); // $ExpectError
36+
reviveNumber( false, 1 ); // $ExpectError
37+
reviveNumber( null, 1 ); // $ExpectError
38+
reviveNumber( undefined, 1 ); // $ExpectError
39+
reviveNumber( [], 1 ); // $ExpectError
40+
reviveNumber( {}, 1 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided insufficient arguments...
44+
{
45+
reviveNumber(); // $ExpectError
46+
reviveNumber( 'foo' ); // $ExpectError
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var parseJSON = require( '@stdlib/utils/parse-json' );
22+
var number2jsonf = require( '@stdlib/number/float32/to-json' );
23+
var PINF = require( '@stdlib/constants/float32/pinf' );
24+
var NINF = require( '@stdlib/constants/float32/ninf' );
25+
var reviver = require( './../lib' );
26+
27+
var str = JSON.stringify( number2jsonf( PINF ) );
28+
console.log( str );
29+
// => '{"type":"float32","value":"+Infinity"}'
30+
31+
var out = parseJSON( str, reviver );
32+
if ( out instanceof Error ) {
33+
throw out;
34+
}
35+
console.log( out );
36+
// => Infinity
37+
38+
str = JSON.stringify( number2jsonf( NINF ) );
39+
console.log( str );
40+
// => '{"type":"float32","value":"-Infinity"}'
41+
42+
out = parseJSON( str, reviver );
43+
if ( out instanceof Error ) {
44+
throw out;
45+
}
46+
console.log( out );
47+
// => -Infinity
48+
49+
str = JSON.stringify( number2jsonf( NaN ) );
50+
console.log( str );
51+
// => '{"type":"float32","value":"NaN"}'
52+
53+
out = parseJSON( str, reviver );
54+
if ( out instanceof Error ) {
55+
throw out;
56+
}
57+
console.log( out );
58+
// => NaN

0 commit comments

Comments
 (0)