Skip to content

Commit c54ade6

Browse files
committed
refactor: rename inverted
1 parent 85e4b1c commit c54ade6

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ See [Quantifiers API doc](https://callstack.github.io/ts-regex-builder/api/quant
136136
| `anyOf('abc')` | `[abc]` | Any of provided characters |
137137
| `charRange('a', 'z')` | `[a-z]` | Character in a range |
138138
| `charClass(...)` | `[...]` | Union of multiple character classes |
139-
| `inverted(...)` | `[^...]` | Negation of a given character class |
139+
| `negated(...)` | `[^...]` | Negation of a given character class |
140140

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

src/constructs/__tests__/character-class.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
charClass,
66
charRange,
77
digit,
8-
inverted,
8+
negated,
99
nonDigit,
1010
nonWhitespace,
1111
nonWord,
@@ -83,9 +83,9 @@ test('`charClass` base cases', () => {
8383
expect(charClass(charRange('a', 'z'), whitespace, anyOf('05'))).toEqualRegex(/[a-z\s05]/);
8484
});
8585

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

@@ -141,30 +141,30 @@ test('`anyOf` throws on empty text', () => {
141141
);
142142
});
143143

144-
test('`inverted` character class pattern', () => {
145-
expect(inverted(anyOf('a'))).toEqualRegex(/[^a]/);
146-
expect(inverted(anyOf('abc'))).toEqualRegex(/[^abc]/);
144+
test('`negated` character class pattern', () => {
145+
expect(negated(anyOf('a'))).toEqualRegex(/[^a]/);
146+
expect(negated(anyOf('abc'))).toEqualRegex(/[^abc]/);
147147
});
148148

149-
test('`inverted` character class pattern double inversion', () => {
150-
expect(inverted(inverted(anyOf('a')))).toEqualRegex(/a/);
151-
expect(inverted(inverted(anyOf('abc')))).toEqualRegex(/[abc]/);
149+
test('`negated` character class pattern double inversion', () => {
150+
expect(negated(negated(anyOf('a')))).toEqualRegex(/a/);
151+
expect(negated(negated(anyOf('abc')))).toEqualRegex(/[abc]/);
152152
});
153153

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

159159
test('`encodeCharacterClass` throws on empty text', () => {
160160
expect(() =>
161161
buildRegExp(
162162
// @ts-expect-error
163-
inverted({
163+
negated({
164164
type: 'characterClass',
165165
chars: [],
166166
ranges: [],
167-
isInverted: false,
167+
isNegated: false,
168168
}),
169169
),
170170
).toThrowErrorMatchingInlineSnapshot(

src/constructs/character-class.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface CharacterClass extends RegexConstruct {
55
type: 'characterClass';
66
chars: string[];
77
ranges: CharacterRange[];
8-
isInverted: boolean;
8+
isNegated: boolean;
99
encode: () => EncodeResult;
1010
}
1111

@@ -21,55 +21,55 @@ export const any: CharacterClass = {
2121
type: 'characterClass',
2222
chars: ['.'],
2323
ranges: [],
24-
isInverted: false,
24+
isNegated: false,
2525
encode: encodeCharacterClass,
2626
};
2727

2828
export const digit: CharacterClass = {
2929
type: 'characterClass',
3030
chars: ['\\d'],
3131
ranges: [],
32-
isInverted: false,
32+
isNegated: false,
3333
encode: encodeCharacterClass,
3434
};
3535

3636
export const nonDigit: CharacterClass = {
3737
type: 'characterClass',
3838
chars: ['\\D'],
3939
ranges: [],
40-
isInverted: false,
40+
isNegated: false,
4141
encode: encodeCharacterClass,
4242
};
4343

4444
export const word: CharacterClass = {
4545
type: 'characterClass',
4646
chars: ['\\w'],
4747
ranges: [],
48-
isInverted: false,
48+
isNegated: false,
4949
encode: encodeCharacterClass,
5050
};
5151

5252
export const nonWord: CharacterClass = {
5353
type: 'characterClass',
5454
chars: ['\\W'],
5555
ranges: [],
56-
isInverted: false,
56+
isNegated: false,
5757
encode: encodeCharacterClass,
5858
};
5959

6060
export const whitespace: CharacterClass = {
6161
type: 'characterClass',
6262
chars: ['\\s'],
6363
ranges: [],
64-
isInverted: false,
64+
isNegated: false,
6565
encode: encodeCharacterClass,
6666
};
6767

6868
export const nonWhitespace: CharacterClass = {
6969
type: 'characterClass',
7070
chars: ['\\S'],
7171
ranges: [],
72-
isInverted: false,
72+
isNegated: false,
7373
encode: encodeCharacterClass,
7474
};
7575

@@ -90,16 +90,16 @@ export const notWhitespace = nonWhitespace;
9090

9191
export function charClass(...elements: CharacterClass[]): CharacterClass {
9292
elements.forEach((element) => {
93-
if (element.isInverted) {
94-
throw new Error('`charClass` should receive only non-inverted character classes');
93+
if (element.isNegated) {
94+
throw new Error('`charClass` should receive only non-negated character classes');
9595
}
9696
});
9797

9898
return {
9999
type: 'characterClass',
100100
chars: elements.map((c) => c.chars).flat(),
101101
ranges: elements.map((c) => c.ranges).flat(),
102-
isInverted: false,
102+
isNegated: false,
103103
encode: encodeCharacterClass,
104104
};
105105
}
@@ -121,7 +121,7 @@ export function charRange(start: string, end: string): CharacterClass {
121121
type: 'characterClass',
122122
chars: [],
123123
ranges: [{ start, end }],
124-
isInverted: false,
124+
isNegated: false,
125125
encode: encodeCharacterClass,
126126
};
127127
}
@@ -137,17 +137,17 @@ export function anyOf(characters: string): CharacterClass {
137137
type: 'characterClass',
138138
chars,
139139
ranges: [],
140-
isInverted: false,
140+
isNegated: false,
141141
encode: encodeCharacterClass,
142142
};
143143
}
144144

145-
export function inverted(element: CharacterClass): CharacterClass {
145+
export function negated(element: CharacterClass): CharacterClass {
146146
return {
147147
type: 'characterClass',
148148
chars: element.chars,
149149
ranges: element.ranges,
150-
isInverted: !element.isInverted,
150+
isNegated: !element.isNegated,
151151
encode: encodeCharacterClass,
152152
};
153153
}
@@ -158,7 +158,7 @@ function encodeCharacterClass(this: CharacterClass): EncodeResult {
158158
}
159159

160160
// Direct rendering for single-character class
161-
if (this.chars.length === 1 && this.ranges?.length === 0 && !this.isInverted) {
161+
if (this.chars.length === 1 && this.ranges?.length === 0 && !this.isNegated) {
162162
return {
163163
precedence: 'atom',
164164
pattern: this.chars[0]!,
@@ -172,9 +172,9 @@ function encodeCharacterClass(this: CharacterClass): EncodeResult {
172172
const caret = this.chars.includes('^') ? '^' : '';
173173
const otherChars = this.chars.filter((c) => c !== '-' && c !== '^').join('');
174174
const ranges = this.ranges.map(({ start, end }) => `${start}-${end}`).join('');
175-
const isInverted = this.isInverted ? '^' : '';
175+
const negation = this.isNegated ? '^' : '';
176176

177-
let pattern = `[${isInverted}${ranges}${otherChars}${caret}${hyphen}]`;
177+
let pattern = `[${negation}${ranges}${otherChars}${caret}${hyphen}]`;
178178
if (pattern === '[^-]') pattern = '[\\^-]';
179179

180180
return {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export {
1616
charClass,
1717
charRange,
1818
digit,
19-
inverted,
19+
negated,
2020
nonDigit,
2121
nonWhitespace,
2222
nonWord,

website/docs/api/character-classes.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ Examples:
6868
- `charClass(charRange('a', 'f'), digit)` will match all lowercase hex digits (`0` to `9` and `a` to `f`).
6969
- `charClass(charRange('a', 'z'), digit, anyOf("._-"))` will match any digit, lowercase Latin letter from `a` to `z`, and either of `.`, `_`, and `-` characters.
7070

71-
### `inverted()`
71+
### `negated()`
7272

7373
```ts
74-
function inverted(element: CharacterClass): CharacterClass;
74+
function negated(element: CharacterClass): CharacterClass;
7575
```
7676

7777
Regex syntax: `[^...]`.
7878

79-
The `inverted` construct creates a new character class that matches any character not present in the passed character class.
79+
The `negated` construct creates a new character class that matches any character not present in the passed character class.
8080

8181
Examples:
8282

83-
- `inverted(digit)` matches any character that is not a digit
84-
- `inverted(anyOf('aeiou'))` matches any character that is not a lowercase vowel.
83+
- `negated(digit)` matches any character that is not a digit
84+
- `negated(anyOf('aeiou'))` matches any character that is not a lowercase vowel.

website/docs/api/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ See [Quantifiers](./api/quantifiers) for more info.
8484
| `anyOf('abc')` | `[abc]` | Any of provided characters |
8585
| `charRange('a', 'z')` | `[a-z]` | Character in a range |
8686
| `charClass(...)` | `[...]` | Union of multiple character classes |
87-
| `inverted(...)` | `[^...]` | Negation of a given character class |
87+
| `negated(...)` | `[^...]` | Negation of a given character class |
8888

8989
See [Character Classes](./api/character-classes) for more info.
9090

0 commit comments

Comments
 (0)