-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: common patterns infrastructure (hex color) (#72)
- Loading branch information
1 parent
91663a7
commit 39f4cda
Showing
4 changed files
with
73 additions
and
0 deletions.
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
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,22 @@ | ||
import { hexColorFinder, hexColorValidator } from '..'; | ||
|
||
test('hexColorValidator', () => { | ||
expect(hexColorValidator).toMatchString('#ffffff'); | ||
expect(hexColorValidator).toMatchString('#000'); | ||
|
||
expect(hexColorValidator).not.toMatchString('#000 '); | ||
expect(hexColorValidator).not.toMatchString(' #000'); | ||
expect(hexColorValidator).not.toMatchString('#0'); | ||
expect(hexColorValidator).not.toMatchString('#11'); | ||
expect(hexColorValidator).not.toMatchString('#4444'); | ||
expect(hexColorValidator).not.toMatchString('#55555'); | ||
expect(hexColorValidator).not.toMatchString('#7777777'); | ||
}); | ||
|
||
test('hexColorFinder', () => { | ||
expect(hexColorFinder).toMatchAllGroups('The color is #ffffff', [['#ffffff']]); | ||
expect(hexColorFinder).toMatchAllGroups('The colors are #1, #22, #333, #4444, #55555, #666666', [ | ||
['#333'], | ||
['#666666'], | ||
]); | ||
}); |
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,38 @@ | ||
import { buildRegExp } from '../builders'; | ||
import { endOfString, startOfString, wordBoundary } from '../constructs/anchors'; | ||
import { charClass, charRange, digit } from '../constructs/character-class'; | ||
import { choiceOf } from '../constructs/choice-of'; | ||
import { repeat } from '../constructs/repeat'; | ||
|
||
const hexDigit = charClass(digit, charRange('a', 'f')); | ||
|
||
/** Find hex color strings in a text. */ | ||
export const hexColorFinder = buildRegExp( | ||
[ | ||
'#', | ||
choiceOf( | ||
repeat(hexDigit, 6), // #rrggbb | ||
repeat(hexDigit, 3), // #rgb | ||
), | ||
wordBoundary, | ||
], | ||
{ ignoreCase: true, global: true }, | ||
); | ||
|
||
/** | ||
* Check that given text is a valid hex color. | ||
* | ||
* Allows both 3 and 6 digit hex colors. | ||
* */ | ||
export const hexColorValidator = buildRegExp( | ||
[ | ||
startOfString, // Match whole string | ||
'#', | ||
choiceOf( | ||
repeat(hexDigit, 6), // #rrggbb | ||
repeat(hexDigit, 3), // #rgb | ||
), | ||
endOfString, | ||
], | ||
{ ignoreCase: true }, | ||
); |
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 @@ | ||
export { hexColorFinder, hexColorValidator } from './hex-color'; |