Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC]: add @stdlib/assert/is-complex-string #1370

Open
3 tasks done
marsian83 opened this issue Feb 24, 2024 · 14 comments
Open
3 tasks done

[RFC]: add @stdlib/assert/is-complex-string #1370

marsian83 opened this issue Feb 24, 2024 · 14 comments
Labels
Accepted RFC feature request which has been accepted. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Feature Issue or pull request for adding a new feature. Good First Issue A good first issue for new contributors! JavaScript Issue involves or relates to JavaScript. priority: Normal Normal priority concern or feature request. RFC Request for comments. Feature requests and proposed changes.

Comments

@marsian83
Copy link
Contributor

Description

This RFC proposes adding package @stdlib/assert/is-complex-string.

The package should be similar in structure to @stdlib/complex/reviver-float32.

The goal is to assert if an input string is of a complex number format string(a + ib) or (a+bi)
where, a and b are numbers

 function isComplexString( str ) => boolean

Eg : isComplexString( "32+12i" ) returns true
Eg : isComplexString( "32+i12" ) returns true
Eg : isComplexString( null ) returns true
Eg : isComplexString( "" ) returns false
Eg : isComplexString( "abcd+12i" ) returns false
Eg : isComplexString( Infinity ) returns false

Package: @stdlib/assert/is-complex-string Alias: isComplexString

Related Issues

Can help with issues #1332 and #1333

Questions

This is my first issue so I'd like to know if the way I have proposed the feature is correct

Other

No response

Checklist

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.
@stdlib-bot
Copy link
Contributor

👋 Hi there! 👋

And thank you for opening your first issue! We will get back to you shortly. 🏃 💨

@marsian83
Copy link
Contributor Author

I would like to implement this myself.
as a supporting assertion to #1332

@marsian83
Copy link
Contributor Author

ok, so I took the initiative to start implementing this and I figured there would be 2 ways to approach this

one, using a regex

/^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?[-+]\d*\.?\d*(?:[eE][-+]?\d+)?i$/

This regex is quite popular as I have seen it everywhere on the internet while trying to look for a regex

secondly, I could implement a function to manually parse the string
something like this

function isComplexString( str ) {
	if ( !isString( str ) ) {
		return false;
	}
	
    let re = '';
    let im = '';
    let hasIota = false;
    let i = 0;

    while (str[i] === ' ') {
        i++;
    }

    while (i < str.length && (str[i] >= '0' && str[i] <= '9' || str[i] === '.'
			|| str[i] === '+' || str[i] === '-')) {
        if (str[i] === 'i') {
            return false; // 'i' must not be in the real part
        }
        re += str[i++];
    }

    if (re.length === 0) {
        return false; // No real part
    }

    if (str[i] === '+' || str[i] === '-') {
        im += str[i++];
    }

    while (i < str.length && (str[i] >= '0' && str[i] <= '9' || str[i] === '.'
			|| str[i] === 'i')) {
        if (str[i] === 'i') {
            if (hasIota) {
                return false; // Duplicate 'i'
            }
            hasIota = true;
        }
        im += str[i++];
    }

    if (!hasIota) {
        return false; // No 'i' in the imaginary part
    }

    while (i < str.length) {
        if (str[i++] !== ' ') {
            return false; // non whitespace character after end of the string
        }
    }

    return true;
}

@marsian83
Copy link
Contributor Author

which one of these approaches would be preferable according to the standards of other implementations in the codebas?
@Planeshifter @kgryte

@kgryte
Copy link
Member

kgryte commented Feb 24, 2024

You're right to identify that we need to handle exponential notation (see your regex).

1 + 2i
1
2i
-2i
2i
1.523 - 4.234i
-1.5 + 4.2i
1.0e10 -2.0e-10i
1.0E10 + 4.1e+10i
1+2i
1-0i
0+1i
-0.0 + 2.0i
NaN + NaNi
Infinity-Infinityi

These should all be valid. Notice that we should also handle NaNs and infinities.

@kgryte
Copy link
Member

kgryte commented Feb 24, 2024

My initial sense is that I suggest implementing a manual tokenizer/parser, as a regexp is more likely to be fiddly and given the variety of values, signs, etc, likely to become inscrutable.

@kgryte
Copy link
Member

kgryte commented Feb 24, 2024

@marsian83 Perhaps before actually implementing this function and inlining the implementation, you should propose implementing @stdlib/complex/base/parse-string, which implements the tokenizer/parser and, if the string is valid, returns a complex-like object (e.g., { 're': 3.0, 'im': 5.0 }) or, if the string is not valid, returns null.

Then in this package, the implementation is simply

function isComplexString( value ) {
	return (
		isString( value ) &&
		parse( value ) !== null
	);
}

@marsian83
Copy link
Contributor Author

@marsian83 Perhaps before actually implementing this function and inlining the implementation, you should propose implementing @stdlib/complex/base/parse-string, which implements the tokenizer/parser and, if the string is valid, returns a complex-like object (e.g., { 're': 3.0, 'im': 5.0 }) or, if the string is not valid, returns null.

Then in this package, the implementation is simply

function isComplexString( value ) {
	return (
		isString( value ) &&
		parse( value ) !== null
	);
}

Hi @kgryte , I have implemented the parse function but when trying to lint I have been facing this issue since my first issue

please Can you help me resolve this
image

@Codesmith28
Copy link

Hey there!
Can you assign me this issue?

@Planeshifter
Copy link
Member

@marsian83 Please run make init, which will initialize the missing ESLint plugin.

@marsian83
Copy link
Contributor Author

@marsian83 Please run make init, which will initialize the missing ESLint plugin.

image

Hi, now I am just getting different errors

@Snehil-Shah
Copy link
Member

@marsian83 Please run make init, which will initialize the missing ESLint plugin.

image Hi, now I am just getting different errors

I was getting these too. Try running make install-node-modules (after restoring the package.json that you might have lost). In my case, I was running it on WSL and setting up was really slow. If you're on windows, I suggest spinning up a Github Codespace instead of developing locally, it takes like 2 minutes to set up with no problems or errors.

@marsian83
Copy link
Contributor Author

@marsian83 Perhaps before actually implementing this function and inlining the implementation, you should propose implementing @stdlib/complex/base/parse-string, which implements the tokenizer/parser and, if the string is valid, returns a complex-like object (e.g., { 're': 3.0, 'im': 5.0 }) or, if the string is not valid, returns null.

Then in this package, the implementation is simply

function isComplexString( value ) {
	return (
		isString( value ) &&
		parse( value ) !== null
	);
}

Hi @kgryte , I have made a PR with a possible implementation for this.

@marsian83
Copy link
Contributor Author

Now that the @stdlib/complex/base/parse has been implemented
This can now be implemented as well. I will get to working on this.

@kgryte kgryte added RFC Request for comments. Feature requests and proposed changes. Feature Issue or pull request for adding a new feature. Accepted RFC feature request which has been accepted. Good First Issue A good first issue for new contributors! priority: Normal Normal priority concern or feature request. JavaScript Issue involves or relates to JavaScript. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted RFC feature request which has been accepted. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Feature Issue or pull request for adding a new feature. Good First Issue A good first issue for new contributors! JavaScript Issue involves or relates to JavaScript. priority: Normal Normal priority concern or feature request. RFC Request for comments. Feature requests and proposed changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants