-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: add basic character classes #13
Conversation
src/characterClasses.ts
Outdated
import type { Whitespace } from './types'; | ||
|
||
export function whitespace(): Whitespace { | ||
return `\\s`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- since white space is constant, we could make it a global object instead of a function.
- Just like other "components" this should be probably split into
a) element builder (here static object):
export const whitespace = {
type: `whitespace`
}
and b) a simple compile operation, placed in compileSingle
fn:
function compileSingle(element: RegexElement): string {
if (typeof element === 'string') {
return element;
}
if (element.type === 'whitespace') {
return "\\s";
}
// ...
}
You can even steamline it further by declaring something like:
const characterClasses = {
whitespace: "\\s",
digit: "\d",
word: "\w",
any: ".",
}
and then do something like
function compileSingle(element: RegexElement): string {
if (typeof element === 'string') {
return element;
}
const characterClass = characterClasses[element.type];
if (characterClass !== undefined) {
return characterClass
}
if (element.type === 'whitespace') {
return "\\s";
}
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated PR and added rest of basic character classes as it was trivial. Let me know if current solution is what you had in mind. :D
I was thinking how this api should look like, and although this looks good I think it's not the best DX experience. If someone has zero knowledge about Regex having one for example enum with Characters would be better. As user could leverage autocomplete |
@okwasniewski I think that common namespace enum for just character classes is not enough, as users will struggle with all other component imports (
Slightly ugly, but so far this is the most autocompletion-friendly thing I have figured out. @okwasniewski, @jaworek, maybe you have some alternative ideas on how we could improve autocompletion, API discovery, imports management. |
4bcc13c
to
b632bef
Compare
return `${wrapGroup(compiledChildren)}{${config.count}}`; | ||
} | ||
|
||
if ('min' in config && typeof config.min === 'number') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which case we'll hit last return? Based on the config type it seems that it will always hit first or second return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JetBrains voodoo 🤣
My take here would be to drop redundant checks if TS is happy.
However, should consider some more ergonomic util functions for verifying assumptions about received options. In another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there! 🔥
return `${wrapGroup(compiledChildren)}{${config.count}}`; | ||
} | ||
|
||
if ('min' in config && typeof config.min === 'number') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JetBrains voodoo 🤣
My take here would be to drop redundant checks if TS is happy.
However, should consider some more ergonomic util functions for verifying assumptions about received options. In another PR.
src/compiler.ts
Outdated
@@ -33,6 +34,16 @@ function compileSingle(elements: RegexElement): string { | |||
return elements; | |||
} | |||
|
|||
if (!('children' in elements)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add as const
to character compiles, would that enable you to drop the ('children' in elements)
check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately no. TS does not like when string passed as a key is wider than what object keys are, even if you handle such case by checking for undefined
.
Alternatively I can check if elements.type
is one of the correct keys, but when I tried it before it was not narrowing elements.type
after the if statement, so it required additional checks for elementCompiler
, etc.
const isCharacterClassKey = (value: string): value is keyof typeof characterClasses => {
return value in characterClasses;
};
if (isCharacterClassKey(elements.type)) {
return characterClasses[elements.type];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check current version, I've solved it
bd150db
to
e51c687
Compare
I've rebased on main and did some type tweaks. Pls do reverse review @jaworek |
Thanks! LGTM 🚀 |
Summary
This PR implements
whitespace
,digit
,word
, andany
character class.Test plan
Passings tests