Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/jsondiffpatch/src/clone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function cloneRegExp(re: RegExp) {
const regexMatch = /^\/(.*)\/([gimyu]*)$/.exec(re.toString());
const regexMatch = /^\/(.*)\/([dgimsuvy]*)$/.exec(re.toString());
if (!regexMatch) {
throw new Error("Invalid RegExp");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsondiffpatch/src/filters/trivial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const patchFilter: Filter<PatchContext> =
}
if (nonNestedDelta.length === 2) {
if (context.left instanceof RegExp) {
const regexArgs = /^\/(.*)\/([gimyu]+)$/.exec(
const regexArgs = /^\/(.*)\/([dgimsuvy]+)$/.exec(
nonNestedDelta[1] as string,
);
if (regexArgs?.[1]) {
Expand Down
12 changes: 12 additions & 0 deletions packages/jsondiffpatch/test/examples/diffpatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type ExampleGroup = Example[];

const exampleDate = () => new Date(2020, 10, 30, 15, 10, 3);

// Built via the constructor rather than a `/…/s` literal so these compile under
// the repo's es6 target (the `s` flag postdates es6 and a literal would error).
const regExpWithFlags = (source: string, flags: string) =>
new RegExp(source, flags);

const atomicValues: ExampleGroup = [
// undefined
{
Expand Down Expand Up @@ -464,6 +469,13 @@ const atomicValues: ExampleGroup = [
delta: ["/regex/g", "/another regex/gi"],
reverse: ["/another regex/gi", "/regex/g"],
},
{
name: "RegExp -> RegExp (dotAll flag)",
left: regExpWithFlags("regex", "s"),
right: regExpWithFlags("another regex", "s"),
delta: ["/regex/s", "/another regex/s"],
reverse: ["/another regex/s", "/regex/s"],
},

// object
{
Expand Down
10 changes: 10 additions & 0 deletions packages/jsondiffpatch/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ describe("DiffPatcher", () => {
pattern: /expr/gim,
});
});
it("clones RegExp with flags added after es2017 (s, d, v)", () => {
// `s` (es2018), `d` (es2022) and `v` (es2024) all postdate the repo's
// es6 target, so build via the constructor with a non-literal flag
// (a `/…/s` literal would not compile).
for (const flag of ["s", "d", "v"]) {
const pattern = new RegExp("expr", flag);
const cloned = jsondiffpatch.clone({ pattern });
expect(cloned).toEqual({ pattern });
}
});
});

describe("using cloneDiffValues", () => {
Expand Down