Skip to content

Commit

Permalink
feat: dotAll & sticky options (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Apr 23, 2024
1 parent 49e8aa4 commit ac35b74
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
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.

0 comments on commit ac35b74

Please sign in to comment.