Skip to content
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

feat: dotAll & sticky builder options #84

Merged
merged 3 commits into from
Apr 23, 2024
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
10 changes: 9 additions & 1 deletion src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ test('`regexBuilder` flags', () => {
expect(buildRegExp('a', { hasIndices: true }).flags).toBe('d');
expect(buildRegExp('a', { hasIndices: false }).flags).toBe('');

expect(buildRegExp('a', { dotAll: true }).flags).toBe('s');
expect(buildRegExp('a', { dotAll: false }).flags).toBe('');

expect(buildRegExp('a', { sticky: true }).flags).toBe('y');
expect(buildRegExp('a', { sticky: false }).flags).toBe('');

expect(
buildRegExp('a', {
global: true, //
ignoreCase: true,
multiline: false,
dotAll: true,
sticky: true,
}).flags,
).toBe('gi');
).toBe('gisy');
});
2 changes: 2 additions & 0 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function encodeFlags(flags: RegexFlags): string {
if (flags.ignoreCase) result += 'i';
if (flags.multiline) result += 'm';
if (flags.hasIndices) result += 'd';
if (flags.dotAll) result += 's';
if (flags.sticky) result += 'y';

return result;
}
36 changes: 32 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,44 @@ export interface RegexConstruct {
encode(): EncodeResult;
}

/**
* Flags to be passed to RegExp constructor.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#flags
*/
export interface RegexFlags {
/** Find all matches in a string, instead of just the first one. */
/**
* Find all matches in a string, instead of just the first one.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global
*/
global?: boolean;

/** Perform case-insensitive matching. */
/**
* Perform case-insensitive matching.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase
*/
ignoreCase?: boolean;

/** Treat the start and end of each line in a string as the beginning and end of the string. */
/**
* Treat the start and end of each line in a string as the beginning and end of the string.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline
*/
multiline?: boolean;

/** Penerate the start and end indices of each captured group in a match. */
/**
* Generate the start and end indices of each captured group in a match.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices
*/
hasIndices?: boolean;

/**
* MDN: _Allows . to match newlines._
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll
*/
dotAll?: boolean;

/**
* MDN: _Matches only from the index indicated by the lastIndex property of this regular expression in the target string. Does not attempt to match from any later indexes._
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
*/
sticky?: boolean;
}
4 changes: 4 additions & 0 deletions website/docs/api/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function buildRegExp(
ignoreCase?: boolean;
multiline?: boolean;
hasIndices?: boolean;
dotAll?: boolean;
sticky?: boolean;
},
): RegExp;
```
Expand All @@ -25,3 +27,5 @@ It optionally accepts a list of regex flags:
- `ignoreCase` - perform case-insensitive matching.
- `multiline` - treat the start and end of each line in a string as the beginning and end of the string.
- `hasIndices` - provide each captured group's start and end indices in a match.
- `dotAll` - allows `.` to match newlines.
- `sticky` - matches only from the index indicated by the `lastIndex` property, does not attempt to match from any later indexes.
Loading