test: Add test for restoring duplicate hashes in patternHasher

This commit is contained in:
reaper
2025-11-12 14:40:30 +05:30
committed by lohit-bruno
parent 7bd6a9a915
commit f3c38400ff
2 changed files with 7 additions and 1 deletions

View File

@@ -29,4 +29,10 @@ describe('patternHasher', () => {
expect(hashed).toMatchInlineSnapshot(`"$name.example.com"`);
expect(restore(hashed)).toEqual(originalUrl);
});
it('verify restoring duplicate hashes', () => {
const originalJSON = `{"name":"{{name}}","x":"{{name}}", "y":"{{name}}"}`;
const { hashed, restore } = patternHasher(originalJSON);
expect(restore(hashed)).toEqual(originalJSON);
});
});

View File

@@ -41,7 +41,7 @@ export function patternHasher(input: string, pattern: string | RegExp = VARIABLE
let clone = current;
for (const hash in hashToOriginal) {
const value = hashToOriginal[hash];
clone = clone.replace(hash, value);
clone = clone.replace(new RegExp(`(${hash})`, 'g'), value);
}
return clone;
}