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
3 changes: 2 additions & 1 deletion regexp/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.1",
"exports": {
".": "./mod.ts",
"./escape": "./escape.ts"
"./escape": "./escape.ts",
"./unstable-replace-all-async": "./unstable_replace_all_async.ts"
}
}
48 changes: 48 additions & 0 deletions regexp/unstable_replace_all_async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2026 the Deno authors. MIT license.
// deno-lint-ignore-file no-explicit-any

/**
* Asynchronously replaces all occurrences of a pattern in a string.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param text - The original string.
* @param searchValue - The regular expression pattern to search for.
* @param replacer - An asynchronous function that returns the replacement string.
* @returns A promise that resolves to the modified string.
*
* @example Usage
* ```ts ignore
* import { replaceAllAsync } from "@std/regexp/unstable-replace-all-async";
* import { assertEquals } from "@std/assert";
*
* const result = await replaceAllAsync(
* "https://example.com/ and https://example.com/not-found!",
* /https:\/\/([\w\-/.]+)/g,
* async (match, address) => {
* const { status } = await fetch(match, { method: "HEAD" });
* return `${address} returned status ${status}`;
* },
* );
*
* assertEquals(
* result,
* "example.com/ returned status 200 and example.com/not-found returned status 404!",
* );
* ```
*/
export async function replaceAllAsync(
text: string,
searchValue: RegExp | string,
replacer: (substring: string, ...args: any[]) => Promise<string> | string,
): Promise<string> {
const promises: (Promise<string> | string)[] = [];

text.replaceAll(searchValue, (...args) => {
promises.push(replacer(...args));
return "";
});

const results = (await Promise.all(promises))[Symbol.iterator]();
return text.replaceAll(searchValue, () => results.next().value!);
}
30 changes: 30 additions & 0 deletions regexp/unstable_replace_all_async_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018-2026 the Deno authors. MIT license.

import { replaceAllAsync } from "./unstable_replace_all_async.ts";
import { assertEquals } from "@std/assert";
import { stub } from "@std/testing/mock";

Deno.test("replaceAllAsync() replaces all occurrences of a pattern asynchronously", async () => {
using _ = stub(
globalThis,
"fetch",
(u) =>
Promise.resolve(
{ status: String(u).includes("not-found") ? 404 : 200 } as Response,
),
);

const result = await replaceAllAsync(
"https://example.com/ and https://example.com/not-found!",
/https:\/\/([\w\-/.]+)/g,
async (match, address) => {
const { status } = await fetch(match, { method: "HEAD" });
return `${address} returned status ${status}`;
},
);

assertEquals(
result,
"example.com/ returned status 200 and example.com/not-found returned status 404!",
);
});
Loading