-
-
Notifications
You must be signed in to change notification settings - Fork 441
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
Comments
👋 Hi there! 👋 And thank you for opening your first issue! We will get back to you shortly. 🏃 💨 |
I would like to implement this myself. |
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 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;
}
|
which one of these approaches would be preferable according to the standards of other implementations in the codebas? |
You're right to identify that we need to handle exponential notation (see your regex).
These should all be valid. Notice that we should also handle NaNs and infinities. |
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. |
@marsian83 Perhaps before actually implementing this function and inlining the implementation, you should propose implementing 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 |
Hey there! |
@marsian83 Please run |
Hi, now I am just getting different errors |
Hi, now I am just getting different errors I was getting these too. Try running |
Hi @kgryte , I have made a PR with a possible implementation for this. |
Now that the |
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
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
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
RFC:
.The text was updated successfully, but these errors were encountered: