Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ See [Quantifiers API doc](https://callstack.github.io/ts-regex-builder/api/quant
| `anyOf('abc')` | `[abc]` | Any of provided characters |
| `charRange('a', 'z')` | `[a-z]` | Character in a range |
| `charClass(...)` | `[...]` | Union of multiple character classes |
| `inverted(...)` | `[^...]` | Negation of a given character class |
| `negated(...)` | `[^...]` | Negation of a given character class |

See [Character Classes API doc](https://callstack.github.io/ts-regex-builder/api/character-classes) for more info.

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/example-find-suffixes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { buildRegExp, choiceOf, notWordBoundary, wordBoundary } from '..';
import { buildRegExp, choiceOf, nonWordBoundary, wordBoundary } from '..';

test('example: find words with suffix', () => {
const suffixesToFind = ['acy', 'ism'];

const regex = buildRegExp([
notWordBoundary, // match suffixes only
nonWordBoundary, // match suffixes only
choiceOf(...suffixesToFind),
wordBoundary,
]);
Expand Down
24 changes: 12 additions & 12 deletions src/constructs/__tests__/anchors.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
buildRegExp,
digit,
endOfString,
notWhitespace,
notWordBoundary,
nonWhitespace,
nonWordBoundary,
oneOrMore,
startOfString,
wordBoundary,
Expand Down Expand Up @@ -35,28 +35,28 @@ test('`wordBoundary` pattern', () => {

test('`wordBoundary` matching', () => {
expect(
buildRegExp([wordBoundary, 'a', zeroOrMore(notWhitespace)], { global: true }),
buildRegExp([wordBoundary, 'a', zeroOrMore(nonWhitespace)], { global: true }),
).toMatchGroups('a ba ab aa', ['a', 'ab', 'aa']);

expect(
buildRegExp([zeroOrMore(notWhitespace), 'a', wordBoundary], { global: true }),
buildRegExp([zeroOrMore(nonWhitespace), 'a', wordBoundary], { global: true }),
).toMatchGroups('a ba ab aa', ['a', 'ba', 'aa']);
});

test('`notWordBoundary` pattern', () => {
expect(notWordBoundary).toEqualRegex(/\B/);
expect([notWordBoundary, 'a', 'b']).toEqualRegex(/\Bab/);
expect(['a', notWordBoundary, 'b']).toEqualRegex(/a\Bb/);
expect(['a', 'b', notWordBoundary]).toEqualRegex(/ab\B/);
test('`nonWordBoundary` pattern', () => {
expect(nonWordBoundary).toEqualRegex(/\B/);
expect([nonWordBoundary, 'a', 'b']).toEqualRegex(/\Bab/);
expect(['a', nonWordBoundary, 'b']).toEqualRegex(/a\Bb/);
expect(['a', 'b', nonWordBoundary]).toEqualRegex(/ab\B/);
});

test('`notWordBoundary` matching', () => {
expect(buildRegExp([notWordBoundary, 'abc', digit], { global: true })).toMatchGroups(
test('`nonWordBoundary` matching', () => {
expect(buildRegExp([nonWordBoundary, 'abc', digit], { global: true })).toMatchGroups(
'abc1 xabc2 xxabc3',
['abc2', 'abc3'],
);

expect(buildRegExp([digit, 'abc', notWordBoundary], { global: true })).toMatchGroups(
expect(buildRegExp([digit, 'abc', nonWordBoundary], { global: true })).toMatchGroups(
'1abc 2abcx 3abcxx',
['2abc', '3abc'],
);
Expand Down
114 changes: 57 additions & 57 deletions src/constructs/__tests__/character-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
charClass,
charRange,
digit,
inverted,
notDigit,
notWhitespace,
notWord,
negated,
nonDigit,
nonWhitespace,
nonWord,
oneOrMore,
optional,
whitespace,
Expand All @@ -30,12 +30,12 @@ test('`digit` character class', () => {
expect(digit).not.toMatchString('A');
});

test('`notDigit` character class', () => {
expect(notDigit).toEqualRegex(/\D/);
expect(['x', notDigit]).toEqualRegex(/x\D/);
expect(['x', notDigit, 'x']).toEqualRegex(/x\Dx/);
expect(notDigit).not.toMatchString('1');
expect(notDigit).toMatchString('A');
test('`nonDigit` character class', () => {
expect(nonDigit).toEqualRegex(/\D/);
expect(['x', nonDigit]).toEqualRegex(/x\D/);
expect(['x', nonDigit, 'x']).toEqualRegex(/x\Dx/);
expect(nonDigit).not.toMatchString('1');
expect(nonDigit).toMatchString('A');
});

test('`word` character class', () => {
Expand All @@ -47,13 +47,13 @@ test('`word` character class', () => {
expect(word).not.toMatchString('$');
});

test('`notWord` character class', () => {
expect(notWord).toEqualRegex(/\W/);
expect(['x', notWord]).toEqualRegex(/x\W/);
expect(['x', notWord, 'x']).toEqualRegex(/x\Wx/);
expect(notWord).not.toMatchString('A');
expect(notWord).not.toMatchString('1');
expect(notWord).toMatchString('$');
test('`nonWord` character class', () => {
expect(nonWord).toEqualRegex(/\W/);
expect(['x', nonWord]).toEqualRegex(/x\W/);
expect(['x', nonWord, 'x']).toEqualRegex(/x\Wx/);
expect(nonWord).not.toMatchString('A');
expect(nonWord).not.toMatchString('1');
expect(nonWord).toMatchString('$');
});

test('`whitespace` character class', () => {
Expand All @@ -66,14 +66,14 @@ test('`whitespace` character class', () => {
expect(whitespace).not.toMatchString('1');
});

test('`notWhitespace` character class', () => {
expect(notWhitespace).toEqualRegex(/\S/);
expect(['x', notWhitespace]).toEqualRegex(/x\S/);
expect(['x', notWhitespace, 'x']).toEqualRegex(/x\Sx/);
expect(notWhitespace).not.toMatchString(' ');
expect(notWhitespace).not.toMatchString('\t');
expect(notWhitespace).toMatchString('A');
expect(notWhitespace).toMatchString('1');
test('`nonWhitespace` character class', () => {
expect(nonWhitespace).toEqualRegex(/\S/);
expect(['x', nonWhitespace]).toEqualRegex(/x\S/);
expect(['x', nonWhitespace, 'x']).toEqualRegex(/x\Sx/);
expect(nonWhitespace).not.toMatchString(' ');
expect(nonWhitespace).not.toMatchString('\t');
expect(nonWhitespace).toMatchString('A');
expect(nonWhitespace).toMatchString('1');
});

test('`charClass` base cases', () => {
Expand All @@ -83,9 +83,9 @@ test('`charClass` base cases', () => {
expect(charClass(charRange('a', 'z'), whitespace, anyOf('05'))).toEqualRegex(/[a-z\s05]/);
});

test('`charClass` throws on inverted arguments', () => {
expect(() => charClass(inverted(whitespace))).toThrowErrorMatchingInlineSnapshot(
`"\`charClass\` should receive only non-inverted character classes"`,
test('`charClass` throws on negated arguments', () => {
expect(() => charClass(negated(whitespace))).toThrowErrorMatchingInlineSnapshot(
`"\`charClass\` should receive only non-negated character classes"`,
);
});

Expand Down Expand Up @@ -141,30 +141,30 @@ test('`anyOf` throws on empty text', () => {
);
});

test('`inverted` character class pattern', () => {
expect(inverted(anyOf('a'))).toEqualRegex(/[^a]/);
expect(inverted(anyOf('abc'))).toEqualRegex(/[^abc]/);
test('`negated` character class pattern', () => {
expect(negated(anyOf('a'))).toEqualRegex(/[^a]/);
expect(negated(anyOf('abc'))).toEqualRegex(/[^abc]/);
});

test('`inverted` character class pattern double inversion', () => {
expect(inverted(inverted(anyOf('a')))).toEqualRegex(/a/);
expect(inverted(inverted(anyOf('abc')))).toEqualRegex(/[abc]/);
test('`negated` character class pattern double inversion', () => {
expect(negated(negated(anyOf('a')))).toEqualRegex(/a/);
expect(negated(negated(anyOf('abc')))).toEqualRegex(/[abc]/);
});

test('`inverted` character class matching', () => {
expect(inverted(anyOf('a'))).not.toMatchString('aa');
expect(inverted(anyOf('a'))).toMatchGroups('aba', ['b']);
test('`negated` character class matching', () => {
expect(negated(anyOf('a'))).not.toMatchString('aa');
expect(negated(anyOf('a'))).toMatchGroups('aba', ['b']);
});

test('`encodeCharacterClass` throws on empty text', () => {
expect(() =>
buildRegExp(
// @ts-expect-error
inverted({
negated({
type: 'characterClass',
chars: [],
ranges: [],
isInverted: false,
isNegated: false,
}),
),
).toThrowErrorMatchingInlineSnapshot(
Expand All @@ -173,22 +173,22 @@ test('`encodeCharacterClass` throws on empty text', () => {
});

test('showcase: negated character classes', () => {
expect(notDigit).toEqualRegex(/\D/);
expect(notWord).toEqualRegex(/\W/);
expect(notWhitespace).toEqualRegex(/\S/);

expect(notDigit).toMatchString('A');
expect(notDigit).not.toMatchString('1');
expect(notDigit).toMatchString(' ');
expect(notDigit).toMatchString('#');

expect(notWord).not.toMatchString('A');
expect(notWord).not.toMatchString('1');
expect(notWord).toMatchString(' ');
expect(notWord).toMatchString('#');

expect(notWhitespace).toMatchString('A');
expect(notWhitespace).toMatchString('1');
expect(notWhitespace).not.toMatchString(' ');
expect(notWhitespace).toMatchString('#');
expect(nonDigit).toEqualRegex(/\D/);
expect(nonWord).toEqualRegex(/\W/);
expect(nonWhitespace).toEqualRegex(/\S/);

expect(nonDigit).toMatchString('A');
expect(nonDigit).not.toMatchString('1');
expect(nonDigit).toMatchString(' ');
expect(nonDigit).toMatchString('#');

expect(nonWord).not.toMatchString('A');
expect(nonWord).not.toMatchString('1');
expect(nonWord).toMatchString(' ');
expect(nonWord).toMatchString('#');

expect(nonWhitespace).toMatchString('A');
expect(nonWhitespace).toMatchString('1');
expect(nonWhitespace).not.toMatchString(' ');
expect(nonWhitespace).toMatchString('#');
});
7 changes: 6 additions & 1 deletion src/constructs/anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ export const wordBoundary: Anchor = {
encode: encodeAnchor,
};

export const notWordBoundary: Anchor = {
export const nonWordBoundary: Anchor = {
type: 'anchor',
symbol: '\\B',
encode: encodeAnchor,
};

/**
* @deprecated Renamed to `nonWordBoundary`.
*/
export const notWordBoundary = nonWordBoundary;

function encodeAnchor(this: Anchor): EncodeResult {
return {
precedence: 'sequence',
Expand Down
Loading