Skip to content
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
113 changes: 113 additions & 0 deletions lib/node_modules/@stdlib/number/float32/to-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!--

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

-->

# number2jsonf

> Return a [JSON][json] representation of a 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 number2jsonf = require( '@stdlib/number/float32/to-json' );
```

#### number2jsonf( x )

Returns a [JSON][json] representation of a number.

```javascript
var json = number2jsonf( NaN );
/* returns
{
'type': 'float32',
'value': 'NaN'
}
*/
```

<!-- For guidance on reviving a JSON-serialized [`Buffer`][@stdlib/buffer/ctor], see [`reviver()`][@stdlib/buffer/reviver]. -->

</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 number2jsonf = require( '@stdlib/number/float32/to-json' );

console.log( number2jsonf( 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">

</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 -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @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 PINF = require( '@stdlib/constants/float32/pinf' );
var NINF = require( '@stdlib/constants/float32/ninf' );
var pkg = require( './../package.json' ).name;
var toJsonf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values = [NINF, PINF, NaN];
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = toJsonf( values[ i%values.length ] );
if ( typeof v !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( typeof v !== 'object' ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::number', function benchmark( b ) {

Check warning on line 52 in lib/node_modules/@stdlib/number/float32/to-json/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = toJsonf( 3.14 );
if ( typeof v !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( typeof v !== 'number' ) {
b.fail( 'should return a number' );
}
b.pass( 'benchmark finished' );
b.end();
});
36 changes: 36 additions & 0 deletions lib/node_modules/@stdlib/number/float32/to-json/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

{{alias}}( x )
Returns a JSON representation of a number.

Parameters
----------
x: number
Input value to serialize.

Returns
-------
out: Object
JSON representation if x is NaN or Infinity.

out.type: string
Value type. The assigned value is always "float32".

out.value: string
Value type. The assigned value can be "NaN", "+Infinity", "-Infinity".

out: number
In all other cases, the input value is returned.

Examples
--------
> var json = {{alias}}( NaN )
{ 'type': 'float32', 'value': 'NaN' }
> json = {{alias}}( Infinity )
{ 'type': 'float32', 'value': '+Infinity' }
> json = {{alias}}( -Infinity )
{ 'type': 'float32', 'value': '-Infinity' }
> json = {{alias}}( 5.0 )
5.0

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* @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

/**
* Returns a JSON representation of a number.
*
* @param x - input value
* @returns JSON representation
*
* @example
* var str = JSON.stringify( number2jsonf( NaN ) );
* // returns '{"type":"float32","value":"NaN"}'
*/
declare function number2jsonf( x: number ): any;

Check warning on line 31 in lib/node_modules/@stdlib/number/float32/to-json/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = number2jsonf;
41 changes: 41 additions & 0 deletions lib/node_modules/@stdlib/number/float32/to-json/docs/types/test.ts
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.
*/

import number2jsonf = require( '../../lib/main' );


// TESTS //

// The function returns an object...
{
const num = 3.14;
number2jsonf( num ); // $ExpectType JSON
}

// The compiler throws an error if the function is provided a value other than a number...
{
number2jsonf( 'abc' ); // $ExpectError
number2jsonf( true ); // $ExpectError
number2jsonf( false ); // $ExpectError
number2jsonf( undefined ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
number2jsonf(); // $ExpectError
}
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/number/float32/to-json/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @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 number2jsonf = require( './../lib' );

var str = JSON.stringify( number2jsonf( NaN ) );
console.log( str );

str = JSON.stringify( number2jsonf( Infinity ) );
console.log( str );

str = JSON.stringify( number2jsonf( -Infinity ) );
console.log( str );
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/number/float32/to-json/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @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';

/**
* Return a JSON representation of a number.
*
* @module @stdlib/number/float32/to-json
*
* @example
* var number2jsonf = require( '@stdlib/number/float32/to-json' );
*
* var str = JSON.stringify( number2jsonf( NaN ) );
* // returns '{"type":"float32","value":"NaN"}'
*/

// MODULES //

var number2jsonf = require( './main.js' );


// EXPORTS //

module.exports = number2jsonf;
Loading