From 933ebe4347a6c5c400e0e5fd93faabe1639fe205 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sat, 25 May 2024 02:48:16 -0700 Subject: [PATCH] feat: add `complex/float32/reviver` This commit copies `@stdlib/complex/reviver-float32` to a new package within the `complex/float32` sub-namespace. Ref: https://github.com/stdlib-js/stdlib/issues/2260 --- .../@stdlib/complex/float32/reviver/README.md | 145 ++++++++++++ .../float32/reviver/benchmark/benchmark.js | 92 ++++++++ .../complex/float32/reviver/docs/repl.txt | 26 +++ .../float32/reviver/docs/types/index.d.ts | 41 ++++ .../float32/reviver/docs/types/test.ts | 50 +++++ .../complex/float32/reviver/examples/index.js | 44 ++++ .../complex/float32/reviver/lib/index.js | 43 ++++ .../complex/float32/reviver/lib/main.js | 60 +++++ .../complex/float32/reviver/package.json | 72 ++++++ .../complex/float32/reviver/test/test.js | 207 ++++++++++++++++++ 10 files changed, 780 insertions(+) create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/README.md create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/examples/index.js create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/lib/index.js create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/lib/main.js create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/package.json create mode 100644 lib/node_modules/@stdlib/complex/float32/reviver/test/test.js diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/README.md b/lib/node_modules/@stdlib/complex/float32/reviver/README.md new file mode 100644 index 00000000000..5738264311b --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/README.md @@ -0,0 +1,145 @@ + + +# reviveComplex64 + +> Revive a JSON-serialized 64-bit [complex number][@stdlib/complex/float32]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var reviveComplex64 = require( '@stdlib/complex/float32/reviver' ); +``` + +#### reviveComplex64( key, value ) + +Revives a JSON-serialized 64-bit [complex number][@stdlib/complex/float32]. + +```javascript +var parseJSON = require( '@stdlib/utils/parse-json' ); + +var str = '{"type":"Complex64","re":5,"im":3}'; + +var z = parseJSON( str, reviveComplex64 ); +// returns +``` + +For details on the JSON serialization format, see [`Complex64`][@stdlib/complex/float32]. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var parseJSON = require( '@stdlib/utils/parse-json' ); +var reviveComplex64 = require( '@stdlib/complex/float32/reviver' ); + +var z = new Complex64( 5.0, 3.0 ); +var str = JSON.stringify( z ); +// returns '{"type":"Complex64","re":5,"im":3}' + +var w = parseJSON( str, reviveComplex64 ); +if ( w instanceof Error ) { + throw w; +} +var bool = ( w instanceof z.constructor ); +// returns true + +bool = ( w.re === z.re ); +// returns true + +bool = ( w.im === z.im ); +// returns true +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/float32/reviver/benchmark/benchmark.js new file mode 100644 index 00000000000..92aced7bf96 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/benchmark/benchmark.js @@ -0,0 +1,92 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 pkg = require( './../package.json' ).name; +var reviver = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var str; + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}'; + o = parseJSON( str, reviver ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::no_reviver', function benchmark( b ) { + var str; + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}'; + o = parseJSON( str ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::no_reviver,built-in', function benchmark( b ) { + var str; + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '{"type":"Complex64","re":'+i+',"im":'+(i+1)+'}'; + o = JSON.parse( str ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/docs/repl.txt b/lib/node_modules/@stdlib/complex/float32/reviver/docs/repl.txt new file mode 100644 index 00000000000..4d1cedb9b1a --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( key, value ) + Revives a JSON-serialized 64-bit complex number. + + Parameters + ---------- + key: string + Key. + + value: any + Value. + + Returns + ------- + out: any + Value or complex number. + + Examples + -------- + > var str = '{"type":"Complex64","re":5,"im":3}'; + > var z = {{alias:@stdlib/utils/parse-json}}( str, {{alias}} ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/index.d.ts new file mode 100644 index 00000000000..3ed000749bb --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/index.d.ts @@ -0,0 +1,41 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 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 64-bit complex number. +* +* @param key - key +* @param value - value +* @returns value or 64-bit complex number +* +* @example +* var parseJSON = require( '@stdlib/utils/parse-json' ); +* +* var str = '{"type":"Complex64","re":5,"im":3}'; +* +* var z = parseJSON( str, reviveComplex64 ); +* // returns +*/ +declare function reviveComplex64( key: string, value: any ): any; + + +// EXPORTS // + +export = reviveComplex64; diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/test.ts b/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/test.ts new file mode 100644 index 00000000000..af2049cef40 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/docs/types/test.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 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 reviveComplex64 = require( './index' ); + + +// TESTS // + +// The function revives a serialized object... +{ + const o = { + 'type': 'Complex64', + 're': 5, + 'im': 3 + }; + reviveComplex64( 'foo', o ); // $ExpectType any +} + +// The compiler throws an error if the function is provided a first argument that is not a string... +{ + reviveComplex64( true, 1 ); // $ExpectError + reviveComplex64( false, 1 ); // $ExpectError + reviveComplex64( null, 1 ); // $ExpectError + reviveComplex64( undefined, 1 ); // $ExpectError + reviveComplex64( 5, 1 ); // $ExpectError + reviveComplex64( [], 1 ); // $ExpectError + reviveComplex64( {}, 1 ); // $ExpectError + reviveComplex64( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + reviveComplex64(); // $ExpectError + reviveComplex64( 'beep' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/examples/index.js b/lib/node_modules/@stdlib/complex/float32/reviver/examples/index.js new file mode 100644 index 00000000000..a796dbcd869 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 Complex64 = require( '@stdlib/complex/float32/ctor' ); +var parseJSON = require( '@stdlib/utils/parse-json' ); +var reviveComplex64 = require( './../lib' ); + +var z = new Complex64( 5.0, 3.0 ); +var str = JSON.stringify( z ); +console.log( str ); +// => '{"type":"Complex64","re":5,"im":3}' + +var w = parseJSON( str, reviveComplex64 ); +if ( w instanceof Error ) { + throw w; +} +var bool = ( w instanceof z.constructor ); +console.log( bool ); +// => true + +bool = ( w.re === z.re ); +console.log( bool ); +// => true + +bool = ( w.im === z.im ); +console.log( bool ); +// => true diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/lib/index.js b/lib/node_modules/@stdlib/complex/float32/reviver/lib/index.js new file mode 100644 index 00000000000..e0b00b858ec --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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'; + +/** +* Revive a JSON-serialized 64-bit complex number. +* +* @module @stdlib/complex/float32/reviver +* +* @example +* var parseJSON = require( '@stdlib/utils/parse-json' ); +* var reviveComplex64 = require( '@stdlib/complex/float32/reviver' ); +* +* var str = '{"type":"Complex64","re":5,"im":3}'; +* +* var z = parseJSON( str, reviveComplex64 ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/lib/main.js b/lib/node_modules/@stdlib/complex/float32/reviver/lib/main.js new file mode 100644 index 00000000000..f57d769b88a --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/lib/main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var Complex64 = require( '@stdlib/complex/float32/ctor' ); + + +// MAIN // + +/** +* Revives a JSON-serialized 64-bit complex number. +* +* @param {string} key - key +* @param {*} value - value +* @returns {(*|Complex64)} value or 64-bit complex number +* +* @example +* var parseJSON = require( '@stdlib/utils/parse-json' ); +* +* var str = '{"type":"Complex64","re":5,"im":3}'; +* +* var z = parseJSON( str, reviveComplex64 ); +* // returns +*/ +function reviveComplex64( key, value ) { + if ( + value && + value.type && + value.type === 'Complex64' && + isNumber( value.re ) && + isNumber( value.im ) + ) { + return new Complex64( value.re, value.im ); + } + return value; +} + + +// EXPORTS // + +module.exports = reviveComplex64; diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/package.json b/lib/node_modules/@stdlib/complex/float32/reviver/package.json new file mode 100644 index 00000000000..46bc98010f0 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/complex/float32/reviver", + "version": "0.0.0", + "description": "Revive a JSON-serialized 64-bit complex 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", + "complex", + "complex64", + "cmplx", + "json", + "reviver", + "revive", + "unmarshal", + "deserialize", + "from", + "convert", + "parse", + "object", + "obj" + ] +} diff --git a/lib/node_modules/@stdlib/complex/float32/reviver/test/test.js b/lib/node_modules/@stdlib/complex/float32/reviver/test/test.js new file mode 100644 index 00000000000..03365962b28 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/float32/reviver/test/test.js @@ -0,0 +1,207 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 copy = require( '@stdlib/utils/copy' ); +var parseJSON = require( '@stdlib/utils/parse-json' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var reviveComplex64 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reviveComplex64, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'values which are not recognized as serialized 64-bit complex numbers are unaffected', function test( t ) { + var expected; + var actual; + + expected = { + 'beep': 'boop' + }; + actual = parseJSON( '{"beep":"boop"}', reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + // Null edge case: + actual = parseJSON( 'null', reviveComplex64 ); + t.strictEqual( actual, null, 'equals null' ); + + t.end(); +}); + +tape( 'an object must have a recognized "type" field in order to be revived', function test( t ) { + var expected; + var actual; + var json; + + json = { + 'type': 'Boop', + 're': 5.0, + 'im': 3.0 + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + t.end(); +}); + +tape( 'an object must have a numeric "re" field in order to be revived', function test( t ) { + var expected; + var actual; + var json; + + json = { + 'type': 'Complex64', + 'im': 3.0 + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + json = { + 'type': 'Complex64', + 'im': 3.0, + 're': '5.0' + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + json = { + 'type': 'Complex64', + 'im': 3.0, + 're': null + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + t.end(); +}); + +tape( 'an object must have a numeric "im" field in order to be revived', function test( t ) { + var expected; + var actual; + var json; + + json = { + 'type': 'Complex64', + 're': 3.0 + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + json = { + 'type': 'Complex64', + 're': 3.0, + 'im': '5.0' + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + json = { + 'type': 'Complex64', + 're': 3.0, + 'im': null + }; + + expected = copy( json ); + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.deepEqual( actual, expected, 'deep equal' ); + + t.end(); +}); + +tape( 'the function will revive a JSON-serialized 64-bit complex number', function test( t ) { + var json; + var z; + var w; + + z = new Complex64( 5.0, 3.0 ); + json = JSON.stringify( z ); + + w = parseJSON( json, reviveComplex64 ); + + t.strictEqual( w instanceof Complex64, true, 'is an instance' ); + t.strictEqual( w.re, z.re, true, 'has expected property value' ); + t.strictEqual( w.im, z.im, true, 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will revive deeply nested serialized 64-bit complex numbers (array)', function test( t ) { + var actual; + var arr; + var i; + + arr = [ + new Complex64( 5.0, 3.0 ), + new Complex64( -2.0, -4.0 ) + ]; + + actual = parseJSON( JSON.stringify( arr ), reviveComplex64 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( actual[i] instanceof Complex64, true, 'is an instance' ); + t.strictEqual( actual[i].re, arr[i].re, 'has expected property value' ); + t.strictEqual( actual[i].im, arr[i].im, 'has expected property value' ); + } + t.end(); +}); + +tape( 'the function will revive deeply nested serialized 64-bit complex numbers (object)', function test( t ) { + var actual; + var json; + + json = { + 'beep': { + 'boop': new Complex64( 5.0, 3.0 ) + } + }; + actual = parseJSON( JSON.stringify( json ), reviveComplex64 ); + + t.strictEqual( actual.beep.boop instanceof Complex64, true, 'is an instance' ); + t.strictEqual( actual.beep.boop.re, json.beep.boop.re, 'has expected property value' ); + t.strictEqual( actual.beep.boop.im, json.beep.boop.im, 'has expected property value' ); + + t.end(); +});