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

feat: add basic character classes #13

Merged
merged 8 commits into from
Dec 6, 2023
Merged

Conversation

jaworek
Copy link
Contributor

@jaworek jaworek commented Dec 6, 2023

Summary

This PR implements whitespace, digit, word, and any character class.

Test plan

Passings tests

import type { Whitespace } from './types';

export function whitespace(): Whitespace {
return `\\s`;
Copy link
Member

@mdjastrzebski mdjastrzebski Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. since white space is constant, we could make it a global object instead of a function.
  2. 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";
  }

  // ...
}

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@okwasniewski
Copy link
Member

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 Character.whitespace or Character.digit WDYT?

src/types.ts Outdated Show resolved Hide resolved
@mdjastrzebski
Copy link
Member

mdjastrzebski commented Dec 6, 2023

@okwasniewski I think that common namespace enum for just character classes is not enough, as users will struggle with all other component imports (oneOrMore, buildRegex, range, etc). One way to solve it would be to recommend prefixed imports:

import * as re from "ts-regex-builder";

const regex = re.buildRegex(
  re.oneOrMore(
    re.whitespace)
  )
)

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.

@jaworek jaworek changed the title [WIP] feat: add whitespace character class [WIP] feat: add basic character class Dec 6, 2023
@jaworek jaworek force-pushed the feat/whitespace-character-class branch from 4bcc13c to b632bef Compare December 6, 2023 17:44
return `${wrapGroup(compiledChildren)}{${config.count}}`;
}

if ('min' in config && typeof config.min === 'number') {
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor Author

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.

Copy link
Member

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/__tests__/characterClasses.test.tsx Outdated Show resolved Hide resolved
src/characterClasses.ts Outdated Show resolved Hide resolved
src/compiler.ts Outdated Show resolved Hide resolved
@jaworek jaworek changed the title [WIP] feat: add basic character class feat: add basic character class Dec 6, 2023
Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! 🔥

src/characterClasses.ts Outdated Show resolved Hide resolved
src/compiler.ts Outdated Show resolved Hide resolved
src/compiler.ts Outdated Show resolved Hide resolved
return `${wrapGroup(compiledChildren)}{${config.count}}`;
}

if ('min' in config && typeof config.min === 'number') {
Copy link
Member

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/types-internal.ts Outdated Show resolved Hide resolved
@jaworek jaworek changed the title feat: add basic character class feat: add basic character classes Dec 6, 2023
src/compiler.ts Outdated
@@ -33,6 +34,16 @@ function compileSingle(elements: RegexElement): string {
return elements;
}

if (!('children' in elements)) {
Copy link
Member

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?

Copy link
Contributor Author

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];
}
image

Copy link
Member

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

@mdjastrzebski mdjastrzebski force-pushed the feat/whitespace-character-class branch from bd150db to e51c687 Compare December 6, 2023 21:37
@mdjastrzebski
Copy link
Member

I've rebased on main and did some type tweaks. Pls do reverse review @jaworek

@jaworek
Copy link
Contributor Author

jaworek commented Dec 6, 2023

I've rebased on main and did some type tweaks. Pls do reverse review @jaworek

Thanks! LGTM 🚀

@jaworek jaworek merged commit e323eeb into main Dec 6, 2023
3 checks passed
@jaworek jaworek deleted the feat/whitespace-character-class branch December 6, 2023 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants