Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 9, 2023
1 parent 2ec2349 commit e2f4bf1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 20
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Options} from 'function-timeout';
import {type Options} from 'function-timeout';

export type Match = {
match: string;
Expand All @@ -8,12 +8,12 @@ export type Match = {
input: string;
};

export interface MatchesOptions extends Options {
export type MatchesOptions = {
/**
The time in milliseconds to wait before timing out when searching for each match.
*/
readonly matchTimeout?: number;
}
} & Options;

/**
Returns a boolean for whether the given `regex` matches the given `string`.
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import functionTimeout, {isTimeoutError} from 'function-timeout';
import timeSpan from 'time-span';
import cloneRegexp from 'clone-regexp'; // TODO: Use `structuredClone` instead when targeting Node.js 18.

const resultToMatch = result => ({
match: result[0],
Expand All @@ -12,7 +11,7 @@ const resultToMatch = result => ({

export function isMatch(regex, string, {timeout} = {}) {
try {
return functionTimeout(() => cloneRegexp(regex).test(string), {timeout})();
return functionTimeout(() => structuredClone(regex).test(string), {timeout})();
} catch (error) {
if (isTimeoutError(error)) {
return false;
Expand All @@ -24,7 +23,7 @@ export function isMatch(regex, string, {timeout} = {}) {

export function firstMatch(regex, string, {timeout} = {}) {
try {
const result = functionTimeout(() => cloneRegexp(regex).exec(string), {timeout})();
const result = functionTimeout(() => structuredClone(regex).exec(string), {timeout})();

if (result === null) {
return;
Expand All @@ -51,7 +50,10 @@ export function matches(regex, string, {timeout = Number.POSITIVE_INFINITY, matc
const matches = string.matchAll(regex); // The regex is only executed when iterated over.

while (true) {
const nextMatch = functionTimeout(() => matches.next(), {timeout: (timeout !== Number.POSITIVE_INFINITY || matchTimeout !== Number.POSITIVE_INFINITY) ? Math.min(timeout, matchTimeout) : undefined}); // `matches.next` must be called within an arrow function so that it doesn't loose its context.
// `matches.next` must be called within an arrow function so that it doesn't loose its context.
const nextMatch = functionTimeout(() => matches.next(), {
timeout: (timeout !== Number.POSITIVE_INFINITY || matchTimeout !== Number.POSITIVE_INFINITY) ? Math.min(timeout, matchTimeout) : undefined,
});

const end = timeSpan();
const {value, done} = nextMatch();
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import {isMatch, firstMatch, matches, Match} from './index.js';
import {isMatch, firstMatch, matches, type Match} from './index.js';

expectType<boolean>(isMatch(/\d/, '1', {timeout: 1000}));
expectType<Match | undefined>(firstMatch(/\d/, '1', {timeout: 1000}));
Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -40,13 +43,12 @@
"execute"
],
"dependencies": {
"clone-regexp": "^3.0.0",
"function-timeout": "^0.1.0",
"function-timeout": "^1.0.1",
"time-span": "^5.1.0"
},
"devDependencies": {
"ava": "^4.3.0",
"tsd": "^0.20.0",
"xo": "^0.49.0"
"ava": "^5.3.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}

0 comments on commit e2f4bf1

Please sign in to comment.