-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5bfe40d
feat: add whitespace character class
jaworek c5b0d29
chore: update based on the PR suggestions
jaworek f26f35e
chore: remove not needed check
jaworek 1386a50
chore: apply suggestions
jaworek 54f9693
chore: throw runtime error when character is not available
jaworek 92676be
chore: change functions to constants
jaworek 725e4d9
chore: refactor
jaworek e51c687
refactor: cleanup and optimizing types
mdjastrzebski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ buck-out/ | |
|
||
# generated by bob | ||
lib/ | ||
|
||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { any, digit, whitespace, word } from '../character-classes'; | ||
import { buildPattern } from '../compiler'; | ||
import { one } from '../quantifiers/base'; | ||
|
||
test('"whitespace" character class', () => { | ||
expect(buildPattern(whitespace)).toEqual(`\\s`); | ||
|
||
expect(buildPattern(one('ab'), whitespace)).toEqual(`ab\\s`); | ||
|
||
expect(buildPattern(one('ab'), whitespace, one('c'))).toEqual(`ab\\sc`); | ||
}); | ||
|
||
test('"digit" character class', () => { | ||
expect(buildPattern(digit)).toEqual(`\\d`); | ||
|
||
expect(buildPattern(one('ab'), digit)).toEqual(`ab\\d`); | ||
|
||
expect(buildPattern(one('ab'), digit, one('c'))).toEqual(`ab\\dc`); | ||
}); | ||
|
||
test('"word" character class', () => { | ||
expect(buildPattern(word)).toEqual(`\\w`); | ||
|
||
expect(buildPattern(one('ab'), word)).toEqual(`ab\\w`); | ||
|
||
expect(buildPattern(one('ab'), word, one('c'))).toEqual(`ab\\wc`); | ||
}); | ||
|
||
test('"any" character class', () => { | ||
expect(buildPattern(any)).toEqual(`.`); | ||
|
||
expect(buildPattern(one('ab'), any)).toEqual(`ab.`); | ||
|
||
expect(buildPattern(one('ab'), any, one('c'))).toEqual(`ab.c`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { | ||
Any, | ||
CharacterClass, | ||
Digit, | ||
RegexElement, | ||
Whitespace, | ||
Word, | ||
} from './types'; | ||
|
||
export const whitespace: Whitespace = { type: 'whitespace' }; | ||
export const digit: Digit = { type: 'digit' }; | ||
export const word: Word = { type: 'word' }; | ||
export const any: Any = { type: 'any' }; | ||
|
||
export const characterClasses = { | ||
whitespace: '\\s', | ||
digit: '\\d', | ||
word: '\\w', | ||
any: '.', | ||
} as const satisfies Record<string, string>; | ||
|
||
export function isCharacterClass( | ||
element: Exclude<RegexElement, string> | ||
): element is CharacterClass { | ||
return element.type in characterClasses; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export type * from './types'; | ||
|
||
export { whitespace } from './character-classes'; | ||
export { buildRegex, buildPattern } from './compiler'; | ||
export { oneOrMore, optionally } from './quantifiers'; | ||
export { oneOrMore, optionally } from './quantifiers/base'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
import type { RepeatConfig } from '../types'; | ||
import type { RegexElement, Repeat, RepeatConfig } from '../types'; | ||
import { wrapGroup } from '../utils'; | ||
|
||
export function repeat( | ||
config: RepeatConfig, | ||
...children: RegexElement[] | ||
): Repeat { | ||
return { | ||
type: 'repeat', | ||
children, | ||
config, | ||
}; | ||
} | ||
|
||
export function compileRepeat( | ||
config: RepeatConfig, | ||
compiledChildren: string | ||
): string { | ||
if ('count' in config && typeof config.count === 'number') { | ||
if ('count' in config) { | ||
return `${wrapGroup(compiledChildren)}{${config.count}}`; | ||
} | ||
|
||
if ('min' in config && typeof config.min === 'number') { | ||
return `${wrapGroup(compiledChildren)}{${config.min},${config?.max ?? ''}}`; | ||
} | ||
|
||
return `${wrapGroup(compiledChildren)}`; | ||
return `${wrapGroup(compiledChildren)}{${config.min},${config?.max ?? ''}}`; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.