All files / complex/base/parse/lib main.js

98.38% Statements 122/124
96.55% Branches 28/29
100% Functions 1/1
98.38% Lines 122/124

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 1251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 4x 4x 18x 18x 22x 142x 12x 12x 142x 18x 18x 18x 142x 142x 18x 22x 30x 30x 30x 30x 30x     30x 14x 14x 14x 30x 30x 30x 3x 3x 3x 27x 30x 14x 14x 13x 13x 13x 30x 18x 22x 3x 3x 15x 22x 12x 12x 22x 14x 14x 15x 15x 15x 15x 15x 22x 1x 1x 1x 1x 1x  
/**
* @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 replace = require( '@stdlib/string/replace' );
var Number = require( '@stdlib/number/ctor' );
 
 
// MAIN //
 
/**
* Parses a string representing a complex number into a complex-like object.
*
* ## Notes
*
* -   The function returns an object containing the following properties:
*
*     -   **re**: real component
*     -   **im**: imaginary component
*
* @param {string} str - input string
* @returns {ComplexLike} an object containing real and imaginary parts
*
* @example
* var str = '4 + 6i';
*
* var z = parse( str );
* // returns { 're': 4, 'im': 6 }
*/
function parse( str ) {
	var imaginaryParts = [];
	var currentToken = '';
	var isImaginary;
	var realParts = [];
	var parts = [];
	var valid = true;
	var value;
	var part;
	var re = 0;
	var im = 0;
 
	var i;
	if ( typeof str !== 'string' ) {
		return null;
	}
 
	str = replace( str, ' ', '' );
	for ( i = 0; i < str.length; i++ ) {
		if ( (str[i] === '+' || str[i] === '-') && i !== 0 && str[i - 1] !== 'e' ) {
			parts.push( currentToken );
			currentToken = '';
		} else if ( i === str.length - 1 ) {
			currentToken += str[ i ];
			parts.push( currentToken );
		}
		currentToken += str[ i ];
	}
 
	for ( i = 0; i < parts.length; i++ ) { // Check for invalid parts...
		part = parts[ i ];
		isImaginary = false;
 
		// Check for Iota on either sides:
		if ( part[0] === 'i' ) {
			part = part.slice( 1 );
			isImaginary = true;
		} else if ( part[part.length - 1] === 'i' ) {
			part = part.slice( 0, part.length - 1 );
			isImaginary = true;
		}
 
		value = Number( part );
		if ( isNaN( value ) && part !== 'NaN' && part !== '+NaN' && part !== '-NaN' ) {
			valid = false;
			break;
		}
 
		if ( isImaginary ) {
			imaginaryParts.push( value );
		}
		else {
			realParts.push( value );
		}
	}
 
	if ( !valid ) {
		return null;
	}
 
	for ( i = 0; i < realParts.length; i++ ) {
		re += realParts[i];
	}
	for ( i = 0; i < imaginaryParts.length; i++ ) {
		im += imaginaryParts[i];
	}
 
	return {
		're': re,
		'im': im
	};
}
 
 
// EXPORTS //
 
module.exports = parse;