-
Notifications
You must be signed in to change notification settings - Fork 4
feat: anyOf
component
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6febba6
feat: anyOf component
mdjastrzebski 7711299
refactor: self code review
mdjastrzebski 43007de
refactor: improve code cov
mdjastrzebski 8531994
refactor: colocate test files
mdjastrzebski 2e12801
chore: fix typos
mdjastrzebski d9212a6
chore: fix typos
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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,23 @@ | ||
import { buildPattern as p } from '../../compiler'; | ||
import { oneOrMore } from '../../quantifiers/base'; | ||
import { anyOf } from '../any-of'; | ||
|
||
test('"anyOf" base cases', () => { | ||
expect(p(anyOf(''))).toBe(''); | ||
expect(p(anyOf('a'))).toBe('a'); | ||
expect(p(anyOf('abc'))).toBe('[abc]'); | ||
}); | ||
|
||
test('"anyOf" in context', () => { | ||
expect(p('x', anyOf('a'), 'x')).toBe('xax'); | ||
expect(p('x', anyOf('abc'), 'x')).toBe('x[abc]x'); | ||
expect(p('x', oneOrMore(anyOf('abc')), 'x')).toBe('x(?:[abc])+x'); | ||
}); | ||
|
||
test('"anyOf" escapes special characters', () => { | ||
expect(p(anyOf('abc-+.'))).toBe('[-abc\\+\\.]'); | ||
}); | ||
|
||
test('"anyOf" moves hyphen to the first position', () => { | ||
expect(p(anyOf('a-bc'))).toBe('[-abc]'); | ||
}); |
6 changes: 3 additions & 3 deletions
6
src/__tests__/characterClasses.test.tsx → src/character-classes/__tests__/base.test.ts
This file contains hidden or 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 hidden or 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,9 @@ | ||
import type { CharacterClass } from '../types'; | ||
import { escapeText } from '../utils'; | ||
|
||
export function anyOf(characters: string): CharacterClass { | ||
return { | ||
type: 'characterClass', | ||
characters: characters.split('').map(escapeText), | ||
}; | ||
} |
This file contains hidden or 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,21 @@ | ||
import type { CharacterClass } from '../types'; | ||
|
||
export const whitespace: CharacterClass = { | ||
type: 'characterClass', | ||
characters: ['\\s'], | ||
}; | ||
|
||
export const digit: CharacterClass = { | ||
type: 'characterClass', | ||
characters: ['\\d'], | ||
}; | ||
|
||
export const word: CharacterClass = { | ||
type: 'characterClass', | ||
characters: ['\\w'], | ||
}; | ||
|
||
export const any: CharacterClass = { | ||
type: 'characterClass', | ||
characters: ['.'], | ||
}; |
This file contains hidden or 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,24 @@ | ||
import type { CharacterClass } from '../types'; | ||
|
||
export function compileCharacterClass({ characters }: CharacterClass): string { | ||
if (characters.length === 0) { | ||
return ''; | ||
} | ||
|
||
if (characters.length === 1) { | ||
return characters[0]!; | ||
} | ||
|
||
return `[${escapeHyphen(characters).join('')}]`; | ||
} | ||
|
||
// If passed characters includes hyphen (`-`) it need to be moved to | ||
// first (or last) place in order to treat it as hyphen character and not a range. | ||
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types | ||
function escapeHyphen(characters: string[]) { | ||
if (characters.includes('-')) { | ||
return ['-', ...characters.filter((c) => c !== '-')]; | ||
} | ||
|
||
return characters; | ||
} |
This file contains hidden or 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 hidden or 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,7 +1,9 @@ | ||
export type * from './types'; | ||
|
||
export { any, digit, whitespace, word } from './character-classes'; | ||
export { buildRegex, buildPattern } from './compiler'; | ||
|
||
export { any, digit, whitespace, word } from './character-classes/base'; | ||
export { anyOf } from './character-classes/any-of'; | ||
export { one, oneOrMore, optionally, zeroOrMore } from './quantifiers/base'; | ||
export { repeat } from './quantifiers/repeat'; | ||
export { choiceOf } from './components/choiceOf'; |
This file contains hidden or 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,5 @@ | ||
export type * from './types'; | ||
|
||
export { whitespace } from './character-classes/base'; | ||
export { buildRegex, buildPattern } from './compiler'; | ||
export { oneOrMore, optionally } from './quantifiers/base'; |
4 changes: 2 additions & 2 deletions
4
src/__tests__/quantifiers.test.tsx → src/quantifiers/__tests__/base.test.tsx
This file contains hidden or 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
6 changes: 3 additions & 3 deletions
6
src/__tests__/repeat.test.tsx → src/quantifiers/__tests__/repeat.test.tsx
This file contains hidden or 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 hidden or 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 hidden or 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.
Shouldn't this function also call
.join()
and return a string?Uh oh!
There was an error while loading. Please reload this page.
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.
Not yet, it is parsing it's input for RegexElement. Join will be done when we will compile it. We want to keep these as array of strings, as character class may hold e.g.
\\d
.\\u1234
which are two+ chars, but one conceptual char.