Skip to content

Commit

Permalink
feat: common patterns infrastructure (hex color) (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Mar 14, 2024
1 parent 91663a7 commit 39f4cda
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
"!**/__mocks__",
"!**/.*"
],
"exports": {
".": {
"require": "./lib/commonjs/index",
"import": "./lib/module/index",
"types": "./lib/typescript/src/index.d.ts"
},
"./patterns": {
"require": "./lib/commonjs/patterns/index",
"import": "./lib/module/patterns/index",
"types": "./lib/typescript/src/patterns/index.d.ts"
}
},
"scripts": {
"test": "jest",
"typecheck": "tsc --noEmit",
Expand Down
22 changes: 22 additions & 0 deletions src/patterns/__tests__/hex-color.test.ts
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'],
]);
});
38 changes: 38 additions & 0 deletions src/patterns/hex-color.ts
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 },
);
1 change: 1 addition & 0 deletions src/patterns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { hexColorFinder, hexColorValidator } from './hex-color';

0 comments on commit 39f4cda

Please sign in to comment.