From 8b8797c66d85d449a48c06b7f0ac2f20e6457c44 Mon Sep 17 00:00:00 2001 From: Snehil Shah Date: Sat, 24 Feb 2024 08:07:18 +0530 Subject: [PATCH 01/19] Add `@stdlib/complex/parse-float64` Signed-off-by: Snehil Shah --- .../@stdlib/complex/parse-float64/README.md | 142 ++++++++++++++++++ .../parse-float64/benchmark/benchmark.js | 54 +++++++ .../complex/parse-float64/docs/repl.txt | 27 ++++ .../parse-float64/docs/types/index.d.ts | 44 ++++++ .../complex/parse-float64/docs/types/test.ts | 58 +++++++ .../complex/parse-float64/examples/index.js | 43 ++++++ .../complex/parse-float64/lib/index.js | 42 ++++++ .../@stdlib/complex/parse-float64/lib/main.js | 63 ++++++++ .../complex/parse-float64/package.json | 67 +++++++++ .../complex/parse-float64/test/test.js | 98 ++++++++++++ 10 files changed, 638 insertions(+) create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/README.md create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/examples/index.js create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/lib/index.js create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/lib/main.js create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/package.json create mode 100644 lib/node_modules/@stdlib/complex/parse-float64/test/test.js diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md new file mode 100644 index 000000000000..77b9531ce13c --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -0,0 +1,142 @@ + + +# parseComplex128 + +> Parse a string representation of a 128-bit [complex number][@stdlib/complex/float64]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +``` + +#### parseComplex128( str ) + +Parses a string representation of a 128-bit [complex number][@stdlib/complex/float64]. Throws an error if the string is not recognized as a complex number. + + +```javascript +var str = '5 + 3j'; + +try { + var z = parseComplex128( str ); + // returns +} catch (error) { + console.error('An error occurred:', error.message); +} +``` + +For details on the string format, see [Complex128][@stdlib/complex/float64]. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var Complex128 = require( '@stdlib/complex/float64' ); +var parseComplex128 = require( '@stdlib/complex/parse-float64' ); + +var str = '5 + 3j'; +try { + var z = parseComplex128( str ); + var bool = ( z instanceof Complex128 ); + // returns true + + bool = ( z.re === 5.0 ); + // returns true + + bool = ( z.im === 3.0 ); + // returns true +} catch (error) { + console.error('An error occurred:', error.message); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js new file mode 100644 index 000000000000..6528fe86060d --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pkg = require( './../package.json' ).name; +var parse = require( './../lib' ); +var Complex128 = require( '@stdlib/complex/float64' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var str; + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = i + ' + ' + (i+1) + 'j'; + try { + z = parse( str ); + if ( !(z instanceof Complex128) ) { + b.fail( 'should return a Complex128' ); + } + } catch (error) { + b.fail( 'should not throw an error' ); + } + } + b.toc(); + if ( !(z instanceof Complex128) ) { + b.fail( 'should return a Complex128' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt new file mode 100644 index 000000000000..bb4c7a8c7767 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( str ) + Parses a string representation of a 128-bit complex number. + + Parameters + ---------- + str: string + String representation of a complex number. + + Returns + ------- + out: Complex128 + If the string is recognized as a complex number, a Complex128 instance is returned. + + Throws + ------ + Error + If the string is not recognized as a complex number, an error is thrown. + + Examples + -------- + > var str = '5 + 3j'; + > var z = {{alias}}( str ) + + + See Also + -------- diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts new file mode 100644 index 000000000000..907863f5b7b1 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 + +// MODULES // + +import Complex128 from '@stdlib/complex/float64'; + + +/** +* Parses a string representation of a 128-bit complex number. +* +* @param str - string representation of a complex number +* @returns Complex128 instance +* @throws Will throw an error if the string is not recognized as a complex number +* +* @example +* var str = '5 + 3j'; +* +* var z = parseComplex128( str ); +* // returns +*/ +declare function parseComplex128( str: string ): Complex128; + + +// EXPORTS // + +export = parseComplex128; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts new file mode 100644 index 000000000000..a5b393bc1b3c --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 parseComplex128 = require( './index' ); + + +// TESTS // + +// The function parses a string representation of a complex number... +{ + const str = '5 + 3j'; + parseComplex128( str ); // $ExpectType Complex128 +} + +// The function parses a string representation of a complex number (only real part)... +{ + const str = '3'; + parseComplex128( str ); // $ExpectType Complex128 +} + +// The function parses a string representation of a complex number (only imaginary part)... +{ + const str = '8j'; + parseComplex128( str ); // $ExpectType Complex128 +} + +// The function throws an error if the input is not recognized as a complex number... +{ + const str = 'beep boop'; + parseComplex128( str ); // $ExpectError +} + +// The compiler throws an error if the function is provided a first argument that is not a string... +{ + parseComplex128( true ); // $ExpectError + parseComplex128( false ); // $ExpectError + parseComplex128( null ); // $ExpectError + parseComplex128( undefined ); // $ExpectError + parseComplex128( 5 ); // $ExpectError + parseComplex128( [] ); // $ExpectError + parseComplex128( {} ); // $ExpectError + parseComplex128( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js new file mode 100644 index 000000000000..348d7b19d884 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Complex128 = require( '@stdlib/complex/float64' ); +var parseComplex128 = require( './../lib' ); + +var str = '5 + 3j'; +try { + var z = parseComplex128( str ); + console.log( z ); + // => + + var bool = ( z instanceof Complex128 ); + console.log( bool ); + // => true + + bool = ( z.re === 5.0 ); + console.log( bool ); + // => true + + bool = ( z.im === 3.0 ); + console.log( bool ); + // => true +} catch (error) { + console.error('An error occurred:', error.message); +} diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js new file mode 100644 index 000000000000..397b7c75c3df --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Parses a string representation of a complex number and returns a Complex128 instance. +* +* @module @stdlib/complex/parse-float64 +* +* @example +* var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +* +* var str = '1 + 2j'; +* +* var z = parseComplex128( str ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js new file mode 100644 index 000000000000..2842ccd31522 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Complex128 = require( '@stdlib/complex/float64' ); + + +// MAIN // + +/** +* Parses a string representation of a complex number and returns a Complex128 instance. +* +* @param {string} str - string representation of a complex number +* @returns {Complex128} 128-bit complex number +* @throws {TypeError} must provide a string +* @throws {Error} must provide a valid string representation of a complex number +* +* @example +* var str = '1 + 2j'; +* +* var z = parseComplex128( str ); +* // returns +*/ +function parseComplex128(str) { + if (typeof str !== 'string') { + throw new TypeError('Invalid input: expected a string.'); + } + + // Regular expression to match a complex number in the form 'a + bj', 'a', or 'bj' + const regex = /^([-+]?\d+(?:\.\d+)?)?\s*([-+])?\s*(\d+(?:\.\d+)?)?j?$/; + const match = str.match(regex); + + if (match) { + const re = match[1] ? parseFloat(match[1]) : 0; // real part + const im = match[3] ? ((match[2] === '-' ? -1 : 1) * parseFloat(match[3])) : 0; // imaginary part + return new Complex128(re, im); + } else { + throw new Error('Invalid input: expected a string representation of a complex number in the form "a + bj", "a", or "bj".'); + } +} + + +// EXPORTS // + +module.exports = parseComplex128; diff --git a/lib/node_modules/@stdlib/complex/parse-float64/package.json b/lib/node_modules/@stdlib/complex/parse-float64/package.json new file mode 100644 index 000000000000..37e0a63ab094 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/complex/parse-float64", + "version": "0.0.0", + "description": "Parse a string representation of a 128-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", + "complex128", + "cmplx", + "parse", + "string", + "convert", + "object", + "obj" + ] +} diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js new file mode 100644 index 000000000000..819e3ea63851 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Complex128 = require( '@stdlib/complex/float64' ); +var parseComplex128 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseComplex128, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error for inputs not recognized as complex numbers', function test( t ) { + t.throws(function() { + parseComplex128( 'beep boop' ); + }, Error, 'throws an error for invalid input' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 3.0 ); + w = parseComplex128( '5 + 3j' ); + + t.strictEqual( w instanceof Complex128, 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 parse a string representation of a complex number (negative values)', function test( t ) { + var z; + var w; + + z = new Complex128( -2.0, -4.0 ); + w = parseComplex128( '-2 - 4j' ); + + t.strictEqual( w instanceof Complex128, 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 parse a string representation of a complex number (only real part)', function test( t ) { + var z; + var w; + + z = new Complex128( 3.0, 0.0 ); + w = parseComplex128( '3' ); + + t.strictEqual( w instanceof Complex128, 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 parse a string representation of a complex number (only imaginary part)', function test( t ) { + var z; + var w; + + z = new Complex128( 0.0, 8.0 ); + w = parseComplex128( '8j' ); + + t.strictEqual( w instanceof Complex128, 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(); +}); From 9d259093b0e523199e6a10ab02d31f8aac494159 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 05:14:29 +0000 Subject: [PATCH 02/19] fix: failing tests Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/lib/main.js | 38 +++++++----- .../complex/parse-float64/test/test.js | 62 ++++++++++++++----- 2 files changed, 67 insertions(+), 33 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index 2842ccd31522..4691dba80b38 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -29,9 +29,9 @@ var Complex128 = require( '@stdlib/complex/float64' ); * Parses a string representation of a complex number and returns a Complex128 instance. * * @param {string} str - string representation of a complex number -* @returns {Complex128} 128-bit complex number * @throws {TypeError} must provide a string * @throws {Error} must provide a valid string representation of a complex number +* @returns {Complex128} 128-bit complex number * * @example * var str = '1 + 2j'; @@ -40,21 +40,27 @@ var Complex128 = require( '@stdlib/complex/float64' ); * // returns */ function parseComplex128(str) { - if (typeof str !== 'string') { - throw new TypeError('Invalid input: expected a string.'); - } - - // Regular expression to match a complex number in the form 'a + bj', 'a', or 'bj' - const regex = /^([-+]?\d+(?:\.\d+)?)?\s*([-+])?\s*(\d+(?:\.\d+)?)?j?$/; - const match = str.match(regex); - - if (match) { - const re = match[1] ? parseFloat(match[1]) : 0; // real part - const im = match[3] ? ((match[2] === '-' ? -1 : 1) * parseFloat(match[3])) : 0; // imaginary part - return new Complex128(re, im); - } else { - throw new Error('Invalid input: expected a string representation of a complex number in the form "a + bj", "a", or "bj".'); - } + var regex; + var match; + var re; + var im; + + if (typeof str !== 'string') { + throw new TypeError('Invalid input: expected a string.'); + } + + // Regular expression to match a complex number in the form 'a + bj', 'a - bj', 'bj', or 'a' + regex = /^([-+]?\d+(?:\.\d+)?)?([-+])?(\d+(?:\.\d+)?j)?$/; + match = str.replace(/\s/g, '').match(regex); + + if (!match) { + throw new Error('Invalid input: expected a string representation of a complex number in the form "a + bj", "a - bj", "bj", or "a".'); + } + + re = ( match[1] ) ? parseFloat(match[1]) : 0; // real part + im = ( match[3] ) ? (( match[2] === '-' ) ? -1 : 1) * parseFloat(match[3].slice(0, -1)) : 0; // imaginary part + + return new Complex128(re, im); } diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js index 819e3ea63851..9b86b71e66f1 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -34,7 +34,7 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function throws an error for inputs not recognized as complex numbers', function test( t ) { - t.throws(function() { + t.throws(function invalidInput() { parseComplex128( 'beep boop' ); }, Error, 'throws an error for invalid input' ); @@ -70,29 +70,57 @@ tape( 'the function will parse a string representation of a complex number (nega }); tape( 'the function will parse a string representation of a complex number (only real part)', function test( t ) { - var z; - var w; + var z; + var w; - z = new Complex128( 3.0, 0.0 ); - w = parseComplex128( '3' ); + z = new Complex128( 3.0, 0.0 ); + w = parseComplex128( '3' ); - t.strictEqual( w instanceof Complex128, 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.strictEqual( w instanceof Complex128, 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(); + t.end(); }); tape( 'the function will parse a string representation of a complex number (only imaginary part)', function test( t ) { - var z; - var w; + var z; + var w; + + z = new Complex128( 0.0, 8.0 ); + w = parseComplex128( '8j' ); + + t.strictEqual( w instanceof Complex128, 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 parse a string representation of a complex number (only real part, negative)', function test( t ) { + var z; + var w; - z = new Complex128( 0.0, 8.0 ); - w = parseComplex128( '8j' ); + z = new Complex128( -3.0, 0.0 ); + w = parseComplex128( '-3' ); - t.strictEqual( w instanceof Complex128, 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.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected real part' ); + t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + + t.end(); +}); - t.end(); +tape( 'the function will parse a string representation of a complex number (only imaginary part, negative)', function test( t ) { + var z; + var w; + + z = new Complex128( 0.0, -3.0 ); + w = parseComplex128( '-3j' ); + + t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected real part' ); + t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + + t.end(); }); From 5ee83f7b910d17582bad146914131db194828ceb Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 05:21:13 +0000 Subject: [PATCH 03/19] fix: benchmarks Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../complex/parse-float64/benchmark/benchmark.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js index 6528fe86060d..303cf53b4389 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js @@ -21,9 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); var pkg = require( './../package.json' ).name; var parse = require( './../lib' ); -var Complex128 = require( '@stdlib/complex/float64' ); // MAIN // @@ -36,17 +36,13 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { str = i + ' + ' + (i+1) + 'j'; - try { - z = parse( str ); - if ( !(z instanceof Complex128) ) { - b.fail( 'should return a Complex128' ); - } - } catch (error) { - b.fail( 'should not throw an error' ); + z = parse( str ); + if ( !(isComplex128(z)) ) { + b.fail( 'should return a Complex128' ); } } b.toc(); - if ( !(z instanceof Complex128) ) { + if ( !(isComplex128(z)) ) { b.fail( 'should return a Complex128' ); } b.pass( 'benchmark finished' ); From fc7bd0aa5223fcf2a63391c15a3ab9e302f3296e Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 06:01:15 +0000 Subject: [PATCH 04/19] fix: lint errors and suggested changes Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/README.md | 60 ++++++++----------- .../complex/parse-float64/docs/repl.txt | 12 ++-- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index 77b9531ce13c..f97c99f04f27 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -8,7 +8,7 @@ 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 + 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, @@ -42,18 +42,22 @@ var parseComplex128 = require( '@stdlib/complex/parse-float64' ); #### parseComplex128( str ) -Parses a string representation of a 128-bit [complex number][@stdlib/complex/float64]. Throws an error if the string is not recognized as a complex number. - +Parses a string representation of a 128-bit [complex number][@stdlib/complex/float64]. ```javascript +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); + var str = '5 + 3j'; -try { - var z = parseComplex128( str ); - // returns -} catch (error) { - console.error('An error occurred:', error.message); -} +var z = parseComplex128( str ); +// returns + +var re = real( z ); +// returns 5.0 + +var im = imag( z ); +// returns 3.0 ``` For details on the string format, see [Complex128][@stdlib/complex/float64]. @@ -82,20 +86,17 @@ For details on the string format, see [Complex128][@stdlib/complex/float64]. var Complex128 = require( '@stdlib/complex/float64' ); var parseComplex128 = require( '@stdlib/complex/parse-float64' ); -var str = '5 + 3j'; -try { - var z = parseComplex128( str ); - var bool = ( z instanceof Complex128 ); - // returns true - - bool = ( z.re === 5.0 ); - // returns true - - bool = ( z.im === 3.0 ); - // returns true -} catch (error) { - console.error('An error occurred:', error.message); -} +var str = '8 - 5j'; + +var z = parseComplex128( str ); +var bool = ( z instanceof Complex128 ); +// returns true + +bool = ( z.re === 8.0 ); +// returns true + +bool = ( z.im === -5.0 ); +// returns true ``` @@ -114,13 +115,6 @@ try { @@ -131,12 +125,6 @@ try { [@stdlib/complex/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64 - - -[@stdlib/complex/parse-float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/parse-float32 - - - diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt index bb4c7a8c7767..b34a872f96aa 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -10,12 +10,8 @@ Returns ------- out: Complex128 - If the string is recognized as a complex number, a Complex128 instance is returned. - - Throws - ------ - Error - If the string is not recognized as a complex number, an error is thrown. + If the string is recognized as a complex number, a + Complex128 instance is returned. Examples -------- @@ -23,5 +19,9 @@ > var z = {{alias}}( str ) + References + ---------- + (Add any references here) + See Also -------- From 1c4e03d42e076f9a180fb23fabfd5905ce6daf3f Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 06:17:35 +0000 Subject: [PATCH 05/19] fix: workflow lint errors Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../parse-float64/docs/types/index.d.ts | 10 +++--- .../complex/parse-float64/examples/index.js | 31 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts index 907863f5b7b1..ed3c3c582957 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts @@ -18,13 +18,13 @@ // TypeScript Version: 4.1 -// MODULES // - -import Complex128 from '@stdlib/complex/float64'; - +interface Complex128 { + re: number; + im: number; +} /** -* Parses a string representation of a 128-bit complex number. +* Parse a string representation of a 128-bit complex number. * * @param str - string representation of a complex number * @returns Complex128 instance diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js index 348d7b19d884..90c5275cc55e 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -21,23 +21,20 @@ var Complex128 = require( '@stdlib/complex/float64' ); var parseComplex128 = require( './../lib' ); -var str = '5 + 3j'; -try { - var z = parseComplex128( str ); - console.log( z ); - // => +var str = '1 + 7j'; - var bool = ( z instanceof Complex128 ); - console.log( bool ); - // => true +var z = parseComplex128( str ); +console.log( z ); +// => - bool = ( z.re === 5.0 ); - console.log( bool ); - // => true +var bool = ( z instanceof Complex128 ); +console.log( bool ); +// => true - bool = ( z.im === 3.0 ); - console.log( bool ); - // => true -} catch (error) { - console.error('An error occurred:', error.message); -} +bool = ( z.re === 1.0 ); +console.log( bool ); +// => true + +bool = ( z.im === 7.0 ); +console.log( bool ); +// => true From a4fe19f20b83c8fd15bf050b3020ff65829a8dcc Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 07:21:36 +0000 Subject: [PATCH 06/19] test: add more varied test cases & examples Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/README.md | 4 ++-- .../complex/parse-float64/examples/index.js | 6 +++--- .../@stdlib/complex/parse-float64/test/test.js | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index f97c99f04f27..282b2a530dd6 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -86,7 +86,7 @@ For details on the string format, see [Complex128][@stdlib/complex/float64]. var Complex128 = require( '@stdlib/complex/float64' ); var parseComplex128 = require( '@stdlib/complex/parse-float64' ); -var str = '8 - 5j'; +var str = '8 - 2.75j'; var z = parseComplex128( str ); var bool = ( z instanceof Complex128 ); @@ -95,7 +95,7 @@ var bool = ( z instanceof Complex128 ); bool = ( z.re === 8.0 ); // returns true -bool = ( z.im === -5.0 ); +bool = ( z.im === -2.75 ); // returns true ``` diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js index 90c5275cc55e..746f5b7aa684 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -21,7 +21,7 @@ var Complex128 = require( '@stdlib/complex/float64' ); var parseComplex128 = require( './../lib' ); -var str = '1 + 7j'; +var str = '-0.5 + 1.25j'; var z = parseComplex128( str ); console.log( z ); @@ -31,10 +31,10 @@ var bool = ( z instanceof Complex128 ); console.log( bool ); // => true -bool = ( z.re === 1.0 ); +bool = ( z.re === -0.5 ); console.log( bool ); // => true -bool = ( z.im === 7.0 ); +bool = ( z.im === 1.25 ); console.log( bool ); // => true diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js index 9b86b71e66f1..18e33e82f64c 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -59,8 +59,8 @@ tape( 'the function will parse a string representation of a complex number (nega var z; var w; - z = new Complex128( -2.0, -4.0 ); - w = parseComplex128( '-2 - 4j' ); + z = new Complex128( -2.5, -4.0 ); + w = parseComplex128( '-2.5 - 4j' ); t.strictEqual( w instanceof Complex128, true, 'is an instance' ); t.strictEqual( w.re, z.re, true, 'has expected property value' ); @@ -87,8 +87,8 @@ tape( 'the function will parse a string representation of a complex number (only var z; var w; - z = new Complex128( 0.0, 8.0 ); - w = parseComplex128( '8j' ); + z = new Complex128( 0.0, 8.5 ); + w = parseComplex128( '8.5j' ); t.strictEqual( w instanceof Complex128, true, 'is an instance' ); t.strictEqual( w.re, z.re, true, 'has expected property value' ); @@ -101,8 +101,8 @@ tape( 'the function will parse a string representation of a complex number (only var z; var w; - z = new Complex128( -3.0, 0.0 ); - w = parseComplex128( '-3' ); + z = new Complex128( -3.75, 0.0 ); + w = parseComplex128( '-3.75' ); t.strictEqual( w instanceof Complex128, true, 'is an instance' ); t.strictEqual( w.re, z.re, 'has expected real part' ); @@ -115,8 +115,8 @@ tape( 'the function will parse a string representation of a complex number (only var z; var w; - z = new Complex128( 0.0, -3.0 ); - w = parseComplex128( '-3j' ); + z = new Complex128( 0.0, -0.3 ); + w = parseComplex128( '-0.3j' ); t.strictEqual( w instanceof Complex128, true, 'is an instance' ); t.strictEqual( w.re, z.re, 'has expected real part' ); From 72d223aaf855efa132dac82c19e293faa2c25743 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:59:11 +0000 Subject: [PATCH 07/19] fix: suggested changes and add more test cases Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/README.md | 4 +- .../complex/parse-float64/examples/index.js | 4 +- .../complex/parse-float64/test/test.js | 41 ++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index 282b2a530dd6..97c5d684a83d 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -83,13 +83,13 @@ For details on the string format, see [Complex128][@stdlib/complex/float64]. ```javascript -var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); var parseComplex128 = require( '@stdlib/complex/parse-float64' ); var str = '8 - 2.75j'; var z = parseComplex128( str ); -var bool = ( z instanceof Complex128 ); +var bool = ( isComplex128(z) ); // returns true bool = ( z.re === 8.0 ); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js index 746f5b7aa684..6e8cce61203b 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -18,7 +18,7 @@ 'use strict'; -var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); var parseComplex128 = require( './../lib' ); var str = '-0.5 + 1.25j'; @@ -27,7 +27,7 @@ var z = parseComplex128( str ); console.log( z ); // => -var bool = ( z instanceof Complex128 ); +var bool = ( isComplex128(z) ); console.log( bool ); // => true diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js index 18e33e82f64c..5301786066f4 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require('@stdlib/assert/is-complex128'); var parseComplex128 = require( './../lib' ); @@ -48,7 +49,7 @@ tape( 'the function will parse a string representation of a complex number', fun z = new Complex128( 5.0, 3.0 ); w = parseComplex128( '5 + 3j' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), 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' ); @@ -62,7 +63,7 @@ tape( 'the function will parse a string representation of a complex number (nega z = new Complex128( -2.5, -4.0 ); w = parseComplex128( '-2.5 - 4j' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), 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' ); @@ -76,7 +77,7 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( 3.0, 0.0 ); w = parseComplex128( '3' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), 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' ); @@ -90,7 +91,7 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( 0.0, 8.5 ); w = parseComplex128( '8.5j' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), 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' ); @@ -104,7 +105,7 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( -3.75, 0.0 ); w = parseComplex128( '-3.75' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), true, 'is an instance' ); t.strictEqual( w.re, z.re, 'has expected real part' ); t.strictEqual( w.im, z.im, 'has expected imaginary part' ); @@ -118,7 +119,35 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( 0.0, -0.3 ); w = parseComplex128( '-0.3j' ); - t.strictEqual( w instanceof Complex128, true, 'is an instance' ); + t.strictEqual( isComplex128(w), true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected real part' ); + t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + + t.end(); +}); + +tape( 'the function correctly parses a string representation of a complex number with no space (a+ib format)', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 3.0 ); + w = parseComplex128( '5+3j' ); + + t.strictEqual( isComplex128(w), true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected real part' ); + t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + + t.end(); +}); + +tape( 'the function correctly parses a string representation of a complex number with unconventional spacing (a+ ib format)', function test( t ) { + var z; + var w; + + z = new Complex128( 5.0, 3.0 ); + w = parseComplex128( '5+ 3j' ); + + t.strictEqual( isComplex128(w), true, 'is an instance' ); t.strictEqual( w.re, z.re, 'has expected real part' ); t.strictEqual( w.im, z.im, 'has expected imaginary part' ); From 1ee982ff301c81793d6b4304815f5078c752b211 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:31:14 +0530 Subject: [PATCH 08/19] fix lint errors Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- lib/node_modules/@stdlib/complex/parse-float64/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js index 397b7c75c3df..04180390ceef 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Parses a string representation of a complex number and returns a Complex128 instance. +* Parse a string representation of a complex number and returns a Complex128 instance. * * @module @stdlib/complex/parse-float64 * From a932f62f9364a1071534e293796bb59c07a40bbd Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:31:52 +0530 Subject: [PATCH 09/19] Update lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt index b34a872f96aa..8ada12eb5157 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -19,9 +19,6 @@ > var z = {{alias}}( str ) - References - ---------- - (Add any references here) See Also -------- From 26956a44e54379661936bb3261113f478595491d Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:09:54 +0000 Subject: [PATCH 10/19] fix: lint suggestions Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- lib/node_modules/@stdlib/complex/parse-float64/README.md | 2 +- lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt | 2 +- lib/node_modules/@stdlib/complex/parse-float64/lib/main.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index 97c5d684a83d..cb3e44419d19 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -42,7 +42,7 @@ var parseComplex128 = require( '@stdlib/complex/parse-float64' ); #### parseComplex128( str ) -Parses a string representation of a 128-bit [complex number][@stdlib/complex/float64]. +Parse a string representation of a 128-bit [complex number][@stdlib/complex/float64]. ```javascript var real = require( '@stdlib/complex/real' ); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt index 8ada12eb5157..0f21493af695 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -1,6 +1,6 @@ {{alias}}( str ) - Parses a string representation of a 128-bit complex number. + Parse a string representation of a 128-bit complex number. Parameters ---------- diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index 4691dba80b38..aa626e2724a5 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -26,7 +26,7 @@ var Complex128 = require( '@stdlib/complex/float64' ); // MAIN // /** -* Parses a string representation of a complex number and returns a Complex128 instance. +* Parse a string representation of a complex number and returns a Complex128 instance. * * @param {string} str - string representation of a complex number * @throws {TypeError} must provide a string From 9fe2a41bebf8024ed67783bd5125541a6c40d67b Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 00:29:16 +0000 Subject: [PATCH 11/19] feat: extend to include exp notation, NaN & Infinity Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/README.md | 4 +- .../complex/parse-float64/docs/types/test.ts | 28 +++++++---- .../@stdlib/complex/parse-float64/lib/main.js | 13 ++++-- .../complex/parse-float64/test/test.js | 46 +++++++++++++++++-- 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index cb3e44419d19..3de31e50c339 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -86,13 +86,13 @@ For details on the string format, see [Complex128][@stdlib/complex/float64]. var isComplex128 = require( '@stdlib/assert/is-complex128' ); var parseComplex128 = require( '@stdlib/complex/parse-float64' ); -var str = '8 - 2.75j'; +var str = '1e3 - 2.75j'; var z = parseComplex128( str ); var bool = ( isComplex128(z) ); // returns true -bool = ( z.re === 8.0 ); +bool = ( z.re === 1e3 ); // returns true bool = ( z.im === -2.75 ); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts index a5b393bc1b3c..b8ee28f74ae6 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts @@ -23,26 +23,38 @@ import parseComplex128 = require( './index' ); // The function parses a string representation of a complex number... { - const str = '5 + 3j'; + const str = '5 + 3.5j'; parseComplex128( str ); // $ExpectType Complex128 } // The function parses a string representation of a complex number (only real part)... { - const str = '3'; - parseComplex128( str ); // $ExpectType Complex128 + const str = '3'; + parseComplex128( str ); // $ExpectType Complex128 } // The function parses a string representation of a complex number (only imaginary part)... { - const str = '8j'; - parseComplex128( str ); // $ExpectType Complex128 + const str = '-8j'; + parseComplex128( str ); // $ExpectType Complex128 +} + +// The function parses a string representation of a complex number (NaN)... +{ + const str = 'NaN + NaNj'; + parseComplex128( str ); // $ExpectType Complex128 } -// The function throws an error if the input is not recognized as a complex number... +// The function parses a string representation of a complex number (Infinity)... { - const str = 'beep boop'; - parseComplex128( str ); // $ExpectError + const str = 'Infinity - Infinityj'; + parseComplex128( str ); // $ExpectType Complex128 +} + +// The function parses a string representation of a complex number (scientific notation)... +{ + const str = '1e3 + 1e-3j'; + parseComplex128( str ); // $ExpectType Complex128 } // The compiler throws an error if the function is provided a first argument that is not a string... diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index aa626e2724a5..c75b71754246 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -50,15 +50,22 @@ function parseComplex128(str) { } // Regular expression to match a complex number in the form 'a + bj', 'a - bj', 'bj', or 'a' - regex = /^([-+]?\d+(?:\.\d+)?)?([-+])?(\d+(?:\.\d+)?j)?$/; + regex = /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)j?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)j)?$/; match = str.replace(/\s/g, '').match(regex); if (!match) { throw new Error('Invalid input: expected a string representation of a complex number in the form "a + bj", "a - bj", "bj", or "a".'); } - re = ( match[1] ) ? parseFloat(match[1]) : 0; // real part - im = ( match[3] ) ? (( match[2] === '-' ) ? -1 : 1) * parseFloat(match[3].slice(0, -1)) : 0; // imaginary part + re = ( match[1] && !match[1].endsWith('j') ) ? parseFloat(match[1]) : 0; // Real part + + // Imaginary part + im = 0; + if (match[4]) { + im = ((match[3] === '-') ? -1 : 1) * parseFloat(match[4].replace('j', '')); + } else if (match[1] && match[1].endsWith('j')) { + im = parseFloat(match[1].replace('j', '')); + } return new Complex128(re, im); } diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js index 5301786066f4..17e882e2781d 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -46,8 +46,8 @@ tape( 'the function will parse a string representation of a complex number', fun var z; var w; - z = new Complex128( 5.0, 3.0 ); - w = parseComplex128( '5 + 3j' ); + z = new Complex128( 5.0, 12.0 ); + w = parseComplex128( '5 + 12j' ); t.strictEqual( isComplex128(w), true, 'is an instance' ); t.strictEqual( w.re, z.re, true, 'has expected property value' ); @@ -102,8 +102,8 @@ tape( 'the function will parse a string representation of a complex number (only var z; var w; - z = new Complex128( -3.75, 0.0 ); - w = parseComplex128( '-3.75' ); + z = new Complex128( -3.753, 0.0 ); + w = parseComplex128( '-3.753' ); t.strictEqual( isComplex128(w), true, 'is an instance' ); t.strictEqual( w.re, z.re, 'has expected real part' ); @@ -153,3 +153,41 @@ tape( 'the function correctly parses a string representation of a complex number t.end(); }); + +tape( 'the function will parse a string representation of a complex number with NaNs', function test( t ) { + var w = parseComplex128( 'NaN + NaNj' ); + + t.strictEqual( isComplex128(w), true, 'is an instance' ); + t.ok( isNaN(w.re), 'has expected property value' ); + t.ok( isNaN(w.im), 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number with Infinity', function test( t ) { + var z; + var w; + + z = new Complex128( Infinity, -Infinity ); + w = parseComplex128( 'Infinity - Infinityj' ); + + t.strictEqual( isComplex128(w), true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected property value' ); + t.strictEqual( w.im, z.im, 'has expected property value' ); + + t.end(); +}); + +tape( 'the function will parse a string representation of a complex number in scientific notation', function test( t ) { + var z; + var w; + + z = new Complex128( 1e3, 4.1e-3 ); + w = parseComplex128( '1E3 + 4.1e-3j' ); + + t.strictEqual( isComplex128(w), true, 'is an instance' ); + t.strictEqual( w.re, z.re, 'has expected property value' ); + t.strictEqual( w.im, z.im, 'has expected property value' ); + + t.end(); +}); From 744d46b69058b2615e43dcfa8440dfffa2a90991 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 08:20:09 +0530 Subject: [PATCH 12/19] Update lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts Co-authored-by: Athan Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/docs/types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts index b8ee28f74ae6..af890f2ee245 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts @@ -21,7 +21,7 @@ import parseComplex128 = require( './index' ); // TESTS // -// The function parses a string representation of a complex number... +// The function returns a complex number... { const str = '5 + 3.5j'; parseComplex128( str ); // $ExpectType Complex128 From 9a407581c8c1cd5d678b2625e77b4a3b9987e47d Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 08:22:54 +0530 Subject: [PATCH 13/19] Update lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt Co-authored-by: Athan Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt index 0f21493af695..c1f79f047185 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -10,8 +10,7 @@ Returns ------- out: Complex128 - If the string is recognized as a complex number, a - Complex128 instance is returned. + 128-bit complex number. Examples -------- From c92f7d7916243ab202a083d94f20d11bd8e644fc Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 06:11:50 +0000 Subject: [PATCH 14/19] fix: suggestions Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- .../@stdlib/complex/parse-float64/README.md | 14 +-- .../parse-float64/benchmark/benchmark.js | 2 +- .../complex/parse-float64/docs/repl.txt | 2 +- .../complex/parse-float64/examples/index.js | 8 +- .../complex/parse-float64/lib/index.js | 2 +- .../@stdlib/complex/parse-float64/lib/main.js | 38 +++++--- .../complex/parse-float64/test/test.js | 91 +++++++++---------- 7 files changed, 87 insertions(+), 70 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/README.md b/lib/node_modules/@stdlib/complex/parse-float64/README.md index 3de31e50c339..1ab736461a49 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/README.md +++ b/lib/node_modules/@stdlib/complex/parse-float64/README.md @@ -48,7 +48,7 @@ Parse a string representation of a 128-bit [complex number][@stdlib/complex/floa var real = require( '@stdlib/complex/real' ); var imag = require( '@stdlib/complex/imag' ); -var str = '5 + 3j'; +var str = '5 + 3i'; var z = parseComplex128( str ); // returns @@ -83,19 +83,21 @@ For details on the string format, see [Complex128][@stdlib/complex/float64]. ```javascript -var isComplex128 = require( '@stdlib/assert/is-complex128' ); var parseComplex128 = require( '@stdlib/complex/parse-float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); -var str = '1e3 - 2.75j'; +var str = '1e3 - 2.75i'; var z = parseComplex128( str ); -var bool = ( isComplex128(z) ); +var bool = isComplex128( z ); // returns true -bool = ( z.re === 1e3 ); +bool = ( real( z ) === 1e3 ); // returns true -bool = ( z.im === -2.75 ); +bool = ( imag( z ) === -2.75 ); // returns true ``` diff --git a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js index 303cf53b4389..ab0e8cf7744d 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/benchmark/benchmark.js @@ -35,7 +35,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - str = i + ' + ' + (i+1) + 'j'; + str = i + ' + ' + (i+1) + 'i'; z = parse( str ); if ( !(isComplex128(z)) ) { b.fail( 'should return a Complex128' ); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt index c1f79f047185..6605e96de411 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/repl.txt @@ -14,7 +14,7 @@ Examples -------- - > var str = '5 + 3j'; + > var str = '5 + 3i'; > var z = {{alias}}( str ) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js index 6e8cce61203b..ca7b110b603c 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -19,9 +19,11 @@ 'use strict'; var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); var parseComplex128 = require( './../lib' ); -var str = '-0.5 + 1.25j'; +var str = '-0.5 + 1.25i'; var z = parseComplex128( str ); console.log( z ); @@ -31,10 +33,10 @@ var bool = ( isComplex128(z) ); console.log( bool ); // => true -bool = ( z.re === -0.5 ); +bool = ( real( z ) === -0.5 ); console.log( bool ); // => true -bool = ( z.im === 1.25 ); +bool = ( imag( z ) === 1.25 ); console.log( bool ); // => true diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js index 04180390ceef..0234c6a86bf7 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/index.js @@ -26,7 +26,7 @@ * @example * var parseComplex128 = require( '@stdlib/complex/parse-float64' ); * -* var str = '1 + 2j'; +* var str = '1 + 2i'; * * var z = parseComplex128( str ); * // returns diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index c75b71754246..5a5a54d40439 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -21,6 +21,24 @@ // MODULES // var Complex128 = require( '@stdlib/complex/float64' ); +var replace = require( '@stdlib/string/base/replace' ); + + +// FUNCTIONS // + +/** +* Matches a complex number string. +* +* @private +* @returns {RegExp} regular expression +* +* @example +* var re = regexp(); +* // returns /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/ +*/ +function regexp() { + return /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/; +} // MAIN // @@ -34,37 +52,33 @@ var Complex128 = require( '@stdlib/complex/float64' ); * @returns {Complex128} 128-bit complex number * * @example -* var str = '1 + 2j'; +* var str = '1 + 2i'; * * var z = parseComplex128( str ); * // returns */ function parseComplex128(str) { - var regex; var match; var re; - var im; + var im = 0; if (typeof str !== 'string') { throw new TypeError('Invalid input: expected a string.'); } - // Regular expression to match a complex number in the form 'a + bj', 'a - bj', 'bj', or 'a' - regex = /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)j?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)j)?$/; - match = str.replace(/\s/g, '').match(regex); + match = replace(str, /\s/g, '').match( regexp() ); if (!match) { - throw new Error('Invalid input: expected a string representation of a complex number in the form "a + bj", "a - bj", "bj", or "a".'); + throw new Error('Invalid argument. Unable to parse input string as a complex number. Value: ' + str); } - re = ( match[1] && !match[1].endsWith('j') ) ? parseFloat(match[1]) : 0; // Real part + re = ( match[1] && !match[1].endsWith('i') ) ? parseFloat(match[1]) : 0; // Real part // Imaginary part - im = 0; if (match[4]) { - im = ((match[3] === '-') ? -1 : 1) * parseFloat(match[4].replace('j', '')); - } else if (match[1] && match[1].endsWith('j')) { - im = parseFloat(match[1].replace('j', '')); + im = ((match[3] === '-') ? -1 : 1) * parseFloat(replace(match[4], /i$/, '')); + } else if (match[1] && match[1].endsWith('i')) { + im = parseFloat(replace(match[1], /i$/, '')); } return new Complex128(re, im); diff --git a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js index 17e882e2781d..865c9dc59ab6 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/test/test.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/test/test.js @@ -21,8 +21,14 @@ // MODULES // var tape = require( 'tape' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); var Complex128 = require( '@stdlib/complex/float64' ); -var isComplex128 = require('@stdlib/assert/is-complex128'); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +var isSameComplex128 = require( '@stdlib/assert/is-same-complex128' ); +var isNan = require( '@stdlib/math/base/assert/is-nan' ); +var real = require( '@stdlib/complex/real' ); +var imag = require( '@stdlib/complex/imag' ); var parseComplex128 = require( './../lib' ); @@ -35,9 +41,12 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function throws an error for inputs not recognized as complex numbers', function test( t ) { - t.throws(function invalidInput() { + t.throws( function invalidInput() { parseComplex128( 'beep boop' ); - }, Error, 'throws an error for invalid input' ); + }, Error, 'throws an Error for invalid input' ); + t.throws( function invalidType() { + parseComplex128( true ); + }, TypeError, 'throws TypeError for non string input type' ); t.end(); }); @@ -47,11 +56,10 @@ tape( 'the function will parse a string representation of a complex number', fun var w; z = new Complex128( 5.0, 12.0 ); - w = parseComplex128( '5 + 12j' ); + w = parseComplex128( '5 + 12i' ); - t.strictEqual( isComplex128(w), 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.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value'); t.end(); }); @@ -61,11 +69,10 @@ tape( 'the function will parse a string representation of a complex number (nega var w; z = new Complex128( -2.5, -4.0 ); - w = parseComplex128( '-2.5 - 4j' ); + w = parseComplex128( '-2.5 - 4i' ); - t.strictEqual( isComplex128(w), 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.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -77,9 +84,8 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( 3.0, 0.0 ); w = parseComplex128( '3' ); - t.strictEqual( isComplex128(w), 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.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -89,11 +95,10 @@ tape( 'the function will parse a string representation of a complex number (only var w; z = new Complex128( 0.0, 8.5 ); - w = parseComplex128( '8.5j' ); + w = parseComplex128( '8.5i' ); - t.strictEqual( isComplex128(w), 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.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -105,9 +110,8 @@ tape( 'the function will parse a string representation of a complex number (only z = new Complex128( -3.753, 0.0 ); w = parseComplex128( '-3.753' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected real part' ); - t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -117,11 +121,10 @@ tape( 'the function will parse a string representation of a complex number (only var w; z = new Complex128( 0.0, -0.3 ); - w = parseComplex128( '-0.3j' ); + w = parseComplex128( '-0.3i' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected real part' ); - t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -131,11 +134,10 @@ tape( 'the function correctly parses a string representation of a complex number var w; z = new Complex128( 5.0, 3.0 ); - w = parseComplex128( '5+3j' ); + w = parseComplex128( '5+3i' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected real part' ); - t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -145,21 +147,20 @@ tape( 'the function correctly parses a string representation of a complex number var w; z = new Complex128( 5.0, 3.0 ); - w = parseComplex128( '5+ 3j' ); + w = parseComplex128( '5+ 3i' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected real part' ); - t.strictEqual( w.im, z.im, 'has expected imaginary part' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); tape( 'the function will parse a string representation of a complex number with NaNs', function test( t ) { - var w = parseComplex128( 'NaN + NaNj' ); + var w = parseComplex128( 'NaN + NaNi' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.ok( isNaN(w.re), 'has expected property value' ); - t.ok( isNaN(w.im), 'has expected property value' ); + t.ok( isComplex128(w), 'is an instance' ); + t.ok( isNan(real( w )), 'has expected property value' ); + t.ok( isNan(imag( w )), 'has expected property value' ); t.end(); }); @@ -168,12 +169,11 @@ tape( 'the function will parse a string representation of a complex number with var z; var w; - z = new Complex128( Infinity, -Infinity ); - w = parseComplex128( 'Infinity - Infinityj' ); + z = new Complex128( PINF, NINF ); + w = parseComplex128( 'Infinity - Infinityi' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected property value' ); - t.strictEqual( w.im, z.im, 'has expected property value' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); @@ -183,11 +183,10 @@ tape( 'the function will parse a string representation of a complex number in sc var w; z = new Complex128( 1e3, 4.1e-3 ); - w = parseComplex128( '1E3 + 4.1e-3j' ); + w = parseComplex128( '1E3 + 4.1e-3i' ); - t.strictEqual( isComplex128(w), true, 'is an instance' ); - t.strictEqual( w.re, z.re, 'has expected property value' ); - t.strictEqual( w.im, z.im, 'has expected property value' ); + t.ok( isComplex128( w ), 'is an instance' ); + t.ok( isSameComplex128( w, z ), 'has expected property value' ); t.end(); }); From cf467cad797af5fbd8916bebd19c435b191af591 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:19:56 +0000 Subject: [PATCH 15/19] fix: ts tests --- .../parse-float64/docs/types/index.d.ts | 12 +++--- .../complex/parse-float64/docs/types/test.ts | 38 +++---------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts index ed3c3c582957..18b5777bb912 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts @@ -18,10 +18,12 @@ // TypeScript Version: 4.1 -interface Complex128 { - re: number; - im: number; -} +// MODULES // + +/// + +import { Complex128 } from '@stdlib/types/complex'; + /** * Parse a string representation of a 128-bit complex number. @@ -31,7 +33,7 @@ interface Complex128 { * @throws Will throw an error if the string is not recognized as a complex number * * @example -* var str = '5 + 3j'; +* var str = '5 + 3i'; * * var z = parseComplex128( str ); * // returns diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts index af890f2ee245..ad8968439b18 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/test.ts @@ -23,38 +23,12 @@ import parseComplex128 = require( './index' ); // The function returns a complex number... { - const str = '5 + 3.5j'; - parseComplex128( str ); // $ExpectType Complex128 -} - -// The function parses a string representation of a complex number (only real part)... -{ - const str = '3'; - parseComplex128( str ); // $ExpectType Complex128 -} - -// The function parses a string representation of a complex number (only imaginary part)... -{ - const str = '-8j'; - parseComplex128( str ); // $ExpectType Complex128 -} - -// The function parses a string representation of a complex number (NaN)... -{ - const str = 'NaN + NaNj'; - parseComplex128( str ); // $ExpectType Complex128 -} - -// The function parses a string representation of a complex number (Infinity)... -{ - const str = 'Infinity - Infinityj'; - parseComplex128( str ); // $ExpectType Complex128 -} - -// The function parses a string representation of a complex number (scientific notation)... -{ - const str = '1e3 + 1e-3j'; - parseComplex128( str ); // $ExpectType Complex128 + parseComplex128( '5 + 3.5i' ); // $ExpectType Complex128 + parseComplex128( '3' ); // $ExpectType Complex128 + parseComplex128( '-8i' ); // $ExpectType Complex128 + parseComplex128( '1e3 + 1e-3i' ); // $ExpectType Complex128 + parseComplex128( 'NaN + NaNi' ); // $ExpectType Complex128 + parseComplex128( 'Infinity - Infinityi' ); // $ExpectType Complex128 } // The compiler throws an error if the function is provided a first argument that is not a string... From 879f1a5380388ce0e980caaf8b7e809447d74a32 Mon Sep 17 00:00:00 2001 From: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> Date: Sun, 25 Feb 2024 23:02:24 +0000 Subject: [PATCH 16/19] fix: conventional error messages Signed-off-by: Snehil Shah <130062020+Snehil-Shah@users.noreply.github.com> --- lib/node_modules/@stdlib/complex/parse-float64/lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index 5a5a54d40439..623f9e377f50 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -57,19 +57,19 @@ function regexp() { * var z = parseComplex128( str ); * // returns */ -function parseComplex128(str) { +function parseComplex128( str ) { var match; var re; var im = 0; if (typeof str !== 'string') { - throw new TypeError('Invalid input: expected a string.'); + throw new TypeError('invalid argument: input must be a string. Value: `' + str + '`.'); } match = replace(str, /\s/g, '').match( regexp() ); if (!match) { - throw new Error('Invalid argument. Unable to parse input string as a complex number. Value: ' + str); + throw new Error('invalid argument. Unable to parse input string as a complex number. Value: `' + str + '`.'); } re = ( match[1] && !match[1].endsWith('i') ) ? parseFloat(match[1]) : 0; // Real part From 37e3f55bbd142ac3084ec1f010999f64e4ecc3c8 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 28 Feb 2024 09:05:09 -0500 Subject: [PATCH 17/19] refactor: use assertion package and format for error messages Signed-off-by: Philipp Burckhardt --- .../@stdlib/complex/parse-float64/lib/main.js | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js index 623f9e377f50..8a2193e7d21f 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/lib/main.js @@ -21,7 +21,9 @@ // MODULES // var Complex128 = require( '@stdlib/complex/float64' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var replace = require( '@stdlib/string/base/replace' ); +var format = require( '@stdlib/string/format' ); // FUNCTIONS // @@ -53,7 +55,6 @@ function regexp() { * * @example * var str = '1 + 2i'; -* * var z = parseComplex128( str ); * // returns */ @@ -62,26 +63,25 @@ function parseComplex128( str ) { var re; var im = 0; - if (typeof str !== 'string') { - throw new TypeError('invalid argument: input must be a string. Value: `' + str + '`.'); + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); } - match = replace(str, /\s/g, '').match( regexp() ); - - if (!match) { - throw new Error('invalid argument. Unable to parse input string as a complex number. Value: `' + str + '`.'); + match = replace( str, /\s/g, '' ).match( regexp() ); + if ( !match ) { + throw new Error( format( 'invalid argument. Unable to parse input string as a complex number. Value: `%s`.', str ) ); } - re = ( match[1] && !match[1].endsWith('i') ) ? parseFloat(match[1]) : 0; // Real part + // Real part: + re = ( match[1] && !match[1].endsWith( 'i' ) ) ? parseFloat( match[1] ) : 0; - // Imaginary part - if (match[4]) { - im = ((match[3] === '-') ? -1 : 1) * parseFloat(replace(match[4], /i$/, '')); - } else if (match[1] && match[1].endsWith('i')) { - im = parseFloat(replace(match[1], /i$/, '')); + // Imaginary part: + if ( match[4] ) { + im = ( ( match[3] === '-' ) ? -1 : 1 ) * parseFloat( replace( match[4], /i$/, '' ) ); + } else if ( match[1] && match[1].endsWith( 'i' ) ) { + im = parseFloat( replace( match[1], /i$/, '' ) ); } - - return new Complex128(re, im); + return new Complex128( re, im ); } From 074005e445c615ddc1e5a00978ba710127313fa7 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 28 Feb 2024 09:07:48 -0500 Subject: [PATCH 18/19] Update lib/node_modules/@stdlib/complex/parse-float64/examples/index.js Signed-off-by: Philipp Burckhardt --- .../@stdlib/complex/parse-float64/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js index ca7b110b603c..88ac8218ecfe 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js +++ b/lib/node_modules/@stdlib/complex/parse-float64/examples/index.js @@ -29,7 +29,7 @@ var z = parseComplex128( str ); console.log( z ); // => -var bool = ( isComplex128(z) ); +var bool = ( isComplex128( z ) ); console.log( bool ); // => true From 429dd824b2b9831ffb56816c5a66716cea000aa9 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 28 Feb 2024 09:08:30 -0500 Subject: [PATCH 19/19] Update lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts Signed-off-by: Philipp Burckhardt --- .../@stdlib/complex/parse-float64/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts index 18b5777bb912..0458b0e9cfbf 100644 --- a/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/complex/parse-float64/docs/types/index.d.ts @@ -30,7 +30,7 @@ import { Complex128 } from '@stdlib/types/complex'; * * @param str - string representation of a complex number * @returns Complex128 instance -* @throws Will throw an error if the string is not recognized as a complex number +* @throws must provide a string recognized as a complex number * * @example * var str = '5 + 3i';