Skip to content

Feat: Add patterns for matching URLs and URL components #79

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
74c0f48
Feat: Add URL to published patterns.
PaulJPhilp Mar 21, 2024
fbdaab5
fix: add urlFinder and urlValidator to the exports for patterns
PaulJPhilp Mar 21, 2024
86a1028
feat: adding new pattern for URLs (URL.ts) with tests
PaulJPhilp Mar 25, 2024
0fdbf6c
feat: add url pattern and tests
PaulJPhilp Mar 25, 2024
94e4fb1
feat: add URL pattern
PaulJPhilp Mar 26, 2024
c765243
Merge branch 'callstack:main' into main
PaulJPhilp Mar 27, 2024
39c8a1e
feat: adding IP addresses to the URL pattern
PaulJPhilp Mar 27, 2024
5259077
feat: add test case for IP addresses
PaulJPhilp Mar 27, 2024
1d07055
feat: new test cases for URL pattern
PaulJPhilp Mar 27, 2024
29cdbd6
feat: add ip_addr pattern
PaulJPhilp Mar 28, 2024
eaf6c63
feat: add tests for urlPath
PaulJPhilp Mar 28, 2024
200d916
feat: refactor a shared.ts building block for patterns
PaulJPhilp Mar 28, 2024
27dc60d
feat: finished test suite for URLs
PaulJPhilp Apr 1, 2024
81687b0
feat: added upper and lower hex digits
PaulJPhilp Apr 1, 2024
68f1959
feat: added URL pattern to the patterns/index.ts file
PaulJPhilp Apr 1, 2024
19198ae
fix: fixed import issues
PaulJPhilp Apr 1, 2024
3f049df
fix: fixed export syntax error
PaulJPhilp Apr 1, 2024
b60eca6
fix: renamed url.ts to url-pattern.ts
PaulJPhilp Apr 1, 2024
b5a8b8b
Merge branch 'callstack:main' into main
PaulJPhilp Apr 7, 2024
6ec28e0
feat: adding support for matching substrings
PaulJPhilp Apr 9, 2024
b9a7b83
fix: fixed bug in toMatchString
PaulJPhilp Apr 9, 2024
7b4b92d
fix: fixed toMatchString global interface definition
PaulJPhilp Apr 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/__tests__/example-currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ test('example: extracting currency values', () => {
endOfString,
]);

expect(currencyRegex).toMatchString('$10');
expect(currencyRegex).toMatchString('$ 10');
expect(currencyRegex).toMatchString('$10', { exactString: false, substring: '10' });
expect(currencyRegex).toMatchString('$ 10', { exactString: false, substring: '10' });
expect(currencyRegex).not.toMatchString('$ 10.');
expect(currencyRegex).toMatchString('$ 10');
expect(currencyRegex).toMatchString('$ 10', { exactString: false, substring: '10' });
expect(currencyRegex).not.toMatchString('$10.5');
expect(currencyRegex).toMatchString('$10.50');
expect(currencyRegex).toMatchString('$10.50', { exactString: false, substring: '10.50' });
expect(currencyRegex).not.toMatchString('$10.501');
expect(currencyRegex).toMatchString('€100');
expect(currencyRegex).toMatchString('£1,000');
expect(currencyRegex).toMatchString('$ 100000000000000000');
expect(currencyRegex).toMatchString('€ 10000');
expect(currencyRegex).toMatchString('₿ 100,000');
expect(currencyRegex).toMatchString('€100', { exactString: false, substring: '100' });
expect(currencyRegex).toMatchString('£1,000', { exactString: false, substring: '1,000' });
expect(currencyRegex).toMatchString('$ 100000000000000000', {
exactString: false,
substring: '100000000000000000',
});
expect(currencyRegex).toMatchString('€ 10000', { exactString: false, substring: '10' });
expect(currencyRegex).toMatchString('₿ 100,000', { exactString: false, substring: '100,000' });
expect(currencyRegex).not.toMatchString('10$');
expect(currencyRegex).not.toMatchString('£A000');

Expand Down
18 changes: 9 additions & 9 deletions src/__tests__/example-filename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ test('example: filename validator', () => {
endOfString,
]);

expect(filenameRegex).toMatchString('index.ts');
expect(filenameRegex).toMatchString('index.tsx');
expect(filenameRegex).toMatchString('ind/ex.ts');
expect(filenameRegex).not.toMatchString('index.js');
expect(filenameRegex).not.toMatchString('index.html');
expect(filenameRegex).not.toMatchString('index.css');
expect(filenameRegex).not.toMatchString('./index.js');
expect(filenameRegex).not.toMatchString('./index.html');
expect(filenameRegex).not.toMatchString('./index.css');
expect(filenameRegex).toMatchString('index.ts', { exactString: false });
expect(filenameRegex).toMatchString('index.tsx', { exactString: false });
expect(filenameRegex).toMatchString('ind/ex.ts', { exactString: false });
expect(filenameRegex).not.toMatchString('index.js', { exactString: false });
expect(filenameRegex).not.toMatchString('index.html', { exactString: false });
expect(filenameRegex).not.toMatchString('index.css', { exactString: false });
expect(filenameRegex).not.toMatchString('./index.js', { exactString: false });
expect(filenameRegex).not.toMatchString('./index.html', { exactString: false });
expect(filenameRegex).not.toMatchString('./index.css', { exactString: false });
});
18 changes: 9 additions & 9 deletions src/__tests__/example-find-suffixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ test('example: find words with suffix', () => {
wordBoundary,
]);

expect(regex).toMatchString('democracy');
expect(regex).toMatchString('Bureaucracy');
expect(regex).toMatchString('abc privacy ');
expect(regex).toMatchString('abc dynamism');
expect(regex).toMatchString('realism abc');
expect(regex).toMatchString('abc modernism abc');
expect(regex).toMatchString('democracy', { exactString: false });
expect(regex).toMatchString('Bureaucracy', { exactString: false });
expect(regex).toMatchString('abc privacy ', { exactString: false });
expect(regex).toMatchString('abc dynamism', { exactString: false });
expect(regex).toMatchString('realism abc', { exactString: false });
expect(regex).toMatchString('abc modernism abc', { exactString: false });

expect(regex).not.toMatchString('abc acy');
expect(regex).not.toMatchString('ism abc');
expect(regex).not.toMatchString('dynamisms');
expect(regex).not.toMatchString('abc acy', { exactString: false });
expect(regex).not.toMatchString('ism abc', { exactString: false });
expect(regex).not.toMatchString('dynamisms', { exactString: false });

expect(regex).toEqualRegex(/\B(?:acy|ism)\b/);
});
16 changes: 8 additions & 8 deletions src/__tests__/example-find-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ test('example: find specific words', () => {
wordBoundary,
]);

expect(regex).toMatchString('word');
expect(regex).toMatchString('some date');
expect(regex).toMatchString('date and word');
expect(regex).toMatchString('word', { exactString: false });
expect(regex).toMatchString('some date', { exactString: false });
expect(regex).toMatchString('date and word', { exactString: false });

expect(regex).not.toMatchString('sword');
expect(regex).not.toMatchString('keywords');
expect(regex).not.toMatchString('words');
expect(regex).not.toMatchString('update');
expect(regex).not.toMatchString('dates');
expect(regex).not.toMatchString('sword', { exactString: false });
expect(regex).not.toMatchString('keywords', { exactString: false });
expect(regex).not.toMatchString('words', { exactString: false });
expect(regex).not.toMatchString('update', { exactString: false });
expect(regex).not.toMatchString('dates', { exactString: false });

expect(regex).toEqualRegex(/\b(?:word|date)\b/);
});
6 changes: 4 additions & 2 deletions src/constructs/__tests__/lookahead.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ test('`lookahead` pattern', () => {
});

test('`lookahead` matching', () => {
expect([oneOrMore(digit), lookahead('$')]).toMatchString('1 turkey costs 30$');
expect(['q', lookahead('u')]).toMatchString('queen');
expect([oneOrMore(digit), lookahead('$')]).toMatchString('1 turkey costs 30$', {
exactString: false,
});
expect(['q', lookahead('u')]).toMatchString('queen', { exactString: false });
expect(['a', lookahead('b'), lookahead('c')]).not.toMatchString('abc');
expect(['a', lookahead(capture('bba'))]).toMatchGroups('abba', ['a', 'bba']);
});
Expand Down
6 changes: 4 additions & 2 deletions src/constructs/__tests__/lookbehind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ test('`lookbehind` pattern', () => {
test('`lookbehind` matching', () => {
expect([zeroOrMore(whitespace), word, lookbehind('s'), oneOrMore(whitespace)]).toMatchString(
'too many cats to feed.',
{ exactString: false },
);

expect([lookbehind('USD'), zeroOrMore(whitespace), oneOrMore(digit)]).toMatchString(
'The price is USD 30',
{ exactString: false },
);

expect([lookbehind('USD'), zeroOrMore(whitespace), oneOrMore(digit)]).not.toMatchString(
'The price is CDN 30',
);

expect([lookbehind('a'), 'b']).toMatchString('abba');
expect([lookbehind('a'), 'b']).toMatchString('abba', { exactString: false });

const mjsImport = [lookbehind('.mjs')];
expect(mjsImport).toMatchString("import {Person} from './person.mjs';");
expect(mjsImport).toMatchString("import {Person} from './person.mjs';", { exactString: false });
expect(mjsImport).not.toMatchString("import {Person} from './person.js';");
expect([anyOf('+-'), oneOrMore(digit), lookbehind('-')]).not.toMatchString('+123');
});
Expand Down
6 changes: 4 additions & 2 deletions src/constructs/__tests__/negative-lookahead.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ test('`negativeLookahead` pattern', () => {
});

test('`negativeLookahead` matching', () => {
expect([negativeLookahead('$'), oneOrMore(digit)]).toMatchString('1 turkey costs 30$');
expect([negativeLookahead('a'), 'b']).toMatchString('abba');
expect([negativeLookahead('$'), oneOrMore(digit)]).toMatchString('1 turkey costs 30$', {
exactString: false,
});
expect([negativeLookahead('a'), 'b']).toMatchString('abba', { exactString: false });
expect(['a', negativeLookahead(capture('bba'))]).not.toMatchGroups('abba', ['a', 'bba']);
expect([negativeLookahead('-'), anyOf('+-'), zeroOrMore(digit)]).not.toMatchString('-123');
expect([negativeLookahead('-'), anyOf('+-'), zeroOrMore(digit)]).toMatchString('+123');
Expand Down
72 changes: 72 additions & 0 deletions src/patterns/__tests__/atoms.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { hexDigit, lowerCaseHexDigit, upperCaseHexDigit } from '../atoms';

test('lowerCaseHexDigit', () => {
expect(lowerCaseHexDigit).toMatchString('0');
expect(lowerCaseHexDigit).toMatchString('1');
expect(lowerCaseHexDigit).toMatchString('2');
expect(lowerCaseHexDigit).toMatchString('3');
expect(lowerCaseHexDigit).toMatchString('4');
expect(lowerCaseHexDigit).toMatchString('5');
expect(lowerCaseHexDigit).toMatchString('6');
expect(lowerCaseHexDigit).toMatchString('7');
expect(lowerCaseHexDigit).toMatchString('8');
expect(lowerCaseHexDigit).toMatchString('9');
expect(lowerCaseHexDigit).toMatchString('a');
expect(lowerCaseHexDigit).toMatchString('b');
expect(lowerCaseHexDigit).toMatchString('c');
expect(lowerCaseHexDigit).toMatchString('d');
expect(lowerCaseHexDigit).toMatchString('e');
expect(lowerCaseHexDigit).toMatchString('f');
expect(lowerCaseHexDigit).not.toMatchString('g');
expect(lowerCaseHexDigit).not.toMatchString('h');
});

test('upperCaseHexDigit', () => {
expect(upperCaseHexDigit).toMatchString('0');
expect(upperCaseHexDigit).toMatchString('1');
expect(upperCaseHexDigit).toMatchString('2');
expect(upperCaseHexDigit).toMatchString('3');
expect(upperCaseHexDigit).toMatchString('4');
expect(upperCaseHexDigit).toMatchString('5');
expect(upperCaseHexDigit).toMatchString('6');
expect(upperCaseHexDigit).toMatchString('7');
expect(upperCaseHexDigit).toMatchString('8');
expect(upperCaseHexDigit).toMatchString('9');
expect(upperCaseHexDigit).toMatchString('A');
expect(upperCaseHexDigit).toMatchString('B');
expect(upperCaseHexDigit).toMatchString('C');
expect(upperCaseHexDigit).toMatchString('D');
expect(upperCaseHexDigit).toMatchString('E');
expect(upperCaseHexDigit).toMatchString('F');
expect(upperCaseHexDigit).not.toMatchString('G');
expect(upperCaseHexDigit).not.toMatchString('H');
});

test('hexDigit', () => {
expect(hexDigit).toMatchString('0');
expect(hexDigit).toMatchString('1');
expect(hexDigit).toMatchString('2');
expect(hexDigit).toMatchString('3');
expect(hexDigit).toMatchString('4');
expect(hexDigit).toMatchString('5');
expect(hexDigit).toMatchString('6');
expect(hexDigit).toMatchString('7');
expect(hexDigit).toMatchString('8');
expect(hexDigit).toMatchString('9');
expect(hexDigit).toMatchString('a');
expect(hexDigit).toMatchString('b');
expect(hexDigit).toMatchString('c');
expect(hexDigit).toMatchString('d');
expect(hexDigit).toMatchString('e');
expect(hexDigit).toMatchString('f');
expect(hexDigit).not.toMatchString('g');
expect(hexDigit).not.toMatchString('h');
expect(hexDigit).toMatchString('A');
expect(hexDigit).toMatchString('B');
expect(hexDigit).toMatchString('C');
expect(hexDigit).toMatchString('D');
expect(hexDigit).toMatchString('E');
expect(hexDigit).toMatchString('F');
expect(hexDigit).not.toMatchString('G');
expect(hexDigit).not.toMatchString('H');
});
68 changes: 68 additions & 0 deletions src/patterns/__tests__/ip-addr.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ipv4DigitValidator, ipv4Validator, ipv6GroupValidator } from '..';

test('ipDigit', () => {
expect(ipv4DigitValidator).toMatchString('255');
expect(ipv4DigitValidator).not.toMatchString('256');
expect(ipv4DigitValidator).toMatchString('25');
expect(ipv4DigitValidator).not.toMatchString('25.5');
expect(ipv4DigitValidator).toMatchString('249');
expect(ipv4DigitValidator).toMatchString('100');
expect(ipv4DigitValidator).toMatchString('199');
expect(ipv4DigitValidator).not.toMatchString('1000');
expect(ipv4DigitValidator).not.toMatchString('100.');
expect(ipv4DigitValidator).toMatchString('000');
expect(ipv4DigitValidator).toMatchString('00');
expect(ipv4DigitValidator).toMatchString('0');
expect(ipv4DigitValidator).not.toMatchString('000.0');
expect(ipv4DigitValidator).not.toMatchString('00.0');
expect(ipv4DigitValidator).not.toMatchString('0.0');
});

test('ipv6GroupValidator', () => {
expect(ipv6GroupValidator).toMatchString('0');
expect(ipv6GroupValidator).toMatchString('00');
expect(ipv6GroupValidator).toMatchString('000');
expect(ipv6GroupValidator).toMatchString('0000');
expect(ipv6GroupValidator).not.toMatchString('00000');
expect(ipv6GroupValidator).toMatchString('1');
expect(ipv6GroupValidator).toMatchString('01');
expect(ipv6GroupValidator).toMatchString('001');
expect(ipv6GroupValidator).toMatchString('0001');
expect(ipv6GroupValidator).not.toMatchString('00001');
expect(ipv6GroupValidator).toMatchString('f');
expect(ipv6GroupValidator).toMatchString('ff');
expect(ipv6GroupValidator).toMatchString('fff');
expect(ipv6GroupValidator).toMatchString('ffff');
expect(ipv6GroupValidator).not.toMatchString('fffff');
expect(ipv6GroupValidator).toMatchString('a');
expect(ipv6GroupValidator).toMatchString('aa');
expect(ipv6GroupValidator).toMatchString('aaa');
expect(ipv6GroupValidator).toMatchString('aaaa');
expect(ipv6GroupValidator).not.toMatchString('aaaaa');
expect(ipv6GroupValidator).toMatchString('A');
expect(ipv6GroupValidator).toMatchString('AA');
expect(ipv6GroupValidator).toMatchString('AAA');
expect(ipv6GroupValidator).toMatchString('AAAA');
expect(ipv6GroupValidator).not.toMatchString('AAAAA');
expect(ipv6GroupValidator).not.toMatchString('g');
expect(ipv6GroupValidator).not.toMatchString('gg');
expect(ipv6GroupValidator).not.toMatchString('ggg');
expect(ipv6GroupValidator).not.toMatchString('gggg');
expect(ipv6GroupValidator).toMatchString('2001');
expect(ipv6GroupValidator).toMatchString('Db8');
expect(ipv6GroupValidator).toMatchString('8');
expect(ipv6GroupValidator).toMatchString('800');
expect(ipv6GroupValidator).toMatchString('200C');
expect(ipv6GroupValidator).toMatchString('417A');
});

test('ipv4Validator', () => {
expect(ipv4Validator).toMatchString('255.255.255.255');
expect(ipv4Validator).not.toMatchString('255:255:255:255');
expect(ipv4Validator).not.toMatchString('256.256.256.256');
expect(ipv4Validator).not.toMatchString('255.255.255.255.255');
expect(ipv4Validator).not.toMatchString('255.255.255');
expect(ipv4Validator).toMatchString('10.0.0.128');
expect(ipv4Validator).not.toMatchString('10.0.0.128.');
expect(ipv4Validator).not.toMatchString('.....');
});
Loading
Loading