diff --git a/lib/node_modules/@stdlib/number/float32/to-json/README.md b/lib/node_modules/@stdlib/number/float32/to-json/README.md new file mode 100644 index 000000000000..3961990505e7 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/README.md @@ -0,0 +1,113 @@ + + +# number2jsonf + +> Return a [JSON][json] representation of a number. + + + +
+ +
+ + + + + +
+ +## 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' + } +*/ +``` + + + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var number2jsonf = require( '@stdlib/number/float32/to-json' ); + +console.log( number2jsonf( NaN ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/number/float32/to-json/benchmark/benchmark.js b/lib/node_modules/@stdlib/number/float32/to-json/benchmark/benchmark.js new file mode 100644 index 000000000000..4db067bb8e89 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/benchmark/benchmark.js @@ -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 ) { + 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(); +}); diff --git a/lib/node_modules/@stdlib/number/float32/to-json/docs/repl.txt b/lib/node_modules/@stdlib/number/float32/to-json/docs/repl.txt new file mode 100644 index 000000000000..fe3f6f9a094c --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/docs/repl.txt @@ -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 + -------- diff --git a/lib/node_modules/@stdlib/number/float32/to-json/docs/types/index.d.ts b/lib/node_modules/@stdlib/number/float32/to-json/docs/types/index.d.ts new file mode 100644 index 000000000000..1a89aaf4ea5f --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/docs/types/index.d.ts @@ -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; + + +// EXPORTS // + +export = number2jsonf; diff --git a/lib/node_modules/@stdlib/number/float32/to-json/docs/types/test.ts b/lib/node_modules/@stdlib/number/float32/to-json/docs/types/test.ts new file mode 100644 index 000000000000..32dde840ee7e --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/docs/types/test.ts @@ -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 +} diff --git a/lib/node_modules/@stdlib/number/float32/to-json/examples/index.js b/lib/node_modules/@stdlib/number/float32/to-json/examples/index.js new file mode 100644 index 000000000000..92bacc0f0c9e --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/examples/index.js @@ -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 ); diff --git a/lib/node_modules/@stdlib/number/float32/to-json/lib/index.js b/lib/node_modules/@stdlib/number/float32/to-json/lib/index.js new file mode 100644 index 000000000000..275decd5d927 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/lib/index.js @@ -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; diff --git a/lib/node_modules/@stdlib/number/float32/to-json/lib/main.js b/lib/node_modules/@stdlib/number/float32/to-json/lib/main.js new file mode 100644 index 000000000000..b4aa6e4df36a --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/lib/main.js @@ -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 PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a JSON representation of a number. +* +* @param {number} x - input value +* @throws {TypeError} must provide a number +* @returns {Object} JSON representation +* +* @example +* var json = number2jsonf( NaN ); +* // returns { 'type': 'float32', 'value': 'NaN' } +*/ +function number2jsonf( x ) { + if ( typeof x !== 'number' ) { + throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', x ) ); + } + if ( x !== x ) { + return { + 'type': 'float32', + 'value': 'NaN' + }; + } + if ( x === PINF ) { + return { + 'type': 'float32', + 'value': '+Infinity' + }; + } + if ( x === NINF ) { + return { + 'type': 'float32', + 'value': '-Infinity' + }; + } + return x; +} + + +// EXPORTS // + +module.exports = number2jsonf; diff --git a/lib/node_modules/@stdlib/number/float32/to-json/package.json b/lib/node_modules/@stdlib/number/float32/to-json/package.json new file mode 100644 index 000000000000..828a18d4bbee --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/number/float32/to-json", + "version": "0.0.0", + "description": "Return a JSON representation of a number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "utils", + "util", + "utilities", + "utility", + "number", + "serialize", + "marshal", + "tojson", + "json", + "to", + "convert" + ] +} diff --git a/lib/node_modules/@stdlib/number/float32/to-json/test/test.js b/lib/node_modules/@stdlib/number/float32/to-json/test/test.js new file mode 100644 index 000000000000..5868ec2a0aea --- /dev/null +++ b/lib/node_modules/@stdlib/number/float32/to-json/test/test.js @@ -0,0 +1,130 @@ +/** +* @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 tape = require( 'tape' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var toJSONf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toJSONf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + void 0, + true, + false, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), Error, 'throws an error when provided a ' + (typeof values[i]) ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + toJSONf( value ); + }; + } +}); + +tape( 'the function returns a JSON object', function test( t ) { + var expected; + var json; + + json = toJSONf( NaN ); + + t.strictEqual( isPlainObject( json ), true, 'returns expected value' ); + + expected = { + 'type': 'float32', + 'value': 'NaN' + }; + t.deepEqual( json, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a JSON object', function test( t ) { + var expected; + var json; + + json = toJSONf( PINF ); + + t.strictEqual( isPlainObject( json ), true, 'returns expected value' ); + + expected = { + 'type': 'float32', + 'value': '+Infinity' + }; + t.deepEqual( json, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a JSON object', function test( t ) { + var expected; + var json; + + json = toJSONf( NINF ); + + t.strictEqual( isPlainObject( json ), true, 'returns expected value' ); + + expected = { + 'type': 'float32', + 'value': '-Infinity' + }; + t.deepEqual( json, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a number', function test( t ) { + var expected; + var json; + + json = toJSONf( 3.14 ); + + t.strictEqual( typeof json, 'number', 'returns expected value' ); + + expected = 3.14; + t.strictEqual( json, expected, 'returns expected value' ); + + t.end(); +});