From 7175119f670f664df457852e0069682fee6af1ce Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Tue, 21 Oct 2025 23:01:11 +0200 Subject: [PATCH 1/5] Refactor zero charaters --- soundchanger.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/soundchanger.js b/soundchanger.js index be8dcce..204a03f 100644 --- a/soundchanger.js +++ b/soundchanger.js @@ -11,7 +11,7 @@ function handleFormSubmit(event) { const categories = { "V": ["i", "y", "ɨ", "ʉ", "ɯ", "u", "ɪ", "ʏ", "ʊ", "e", "ø", "ɘ", "ɵ", "ɤ", "o", "ə", "ɛ", "œ", "ɜ", "ɞ", "ʌ", "ɔ", "æ", "ɐ", "a", "ɶ", "ä", "ɑ", "ɒ"] } - const zeroCharacters = '∅-' + const zeroCharacters = ["∅", "-"] const result = apply(changes, strings, categories, zeroCharacters) displayResult(result) @@ -21,10 +21,7 @@ function handleFormSubmit(event) { } } -export function apply(changes, strings, categories={}, zeroCharacters='∅-') { - changes = Array.isArray(changes) ? changes : [changes] - strings = Array.isArray(strings) ? [...strings] : [strings] - +export function apply(changes, strings, categories={}, zeroCharacters=["∅", "-"]) { for (const change of changes) { let reformattedChange = reformatChangeToRegex(change, categories, zeroCharacters) const [original, changeTo, before, after] = splitChange(reformattedChange) @@ -47,8 +44,7 @@ function displayResult(result) { resultElement.innerText = result.join('\n') } -export function reformatChangeToRegex(change, categories = {}, zeroCharacters = '∅-') { - const regexEscape = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +export function reformatChangeToRegex(change, categories = {}, zeroCharacters = ["∅", "-"]) { const replacements = { ' ': '', '{': '(', '}': ')', ',': '|' } for (const [key, value] of Object.entries(replacements)) { @@ -61,9 +57,7 @@ export function reformatChangeToRegex(change, categories = {}, zeroCharacters = change = change.replace(new RegExp(`((?<=,)${regexEscape(category)})|(${regexEscape(category)}(?=,))`, 'g'), categoryValues.join('|')) } - for (const char of zeroCharacters) { - change = change.replace(new RegExp(regexEscape(char), 'g'), '') - } + change = change.replace(new RegExp(`(${zeroCharacters.join('|')})`, 'g'), '') return change } From 13b31ad5e8ff671b5b4e42445c89a5947e3873cc Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Tue, 21 Oct 2025 23:03:11 +0200 Subject: [PATCH 2/5] Refactor application of hashes --- soundchanger.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/soundchanger.js b/soundchanger.js index 204a03f..07bf941 100644 --- a/soundchanger.js +++ b/soundchanger.js @@ -30,11 +30,13 @@ export function apply(changes, strings, categories={}, zeroCharacters=["∅", "- console.log(`Failed to apply change "${change}".`) continue } - const pattern = `(?<=${before})(${original})(?=${after})` + let pattern = before == "#" ? "^" : `(?<=${before})` + pattern += `(${original})` + pattern += after == "#" ? "$" : `(?=${after})` - strings = strings.map(string => { - return `#${string}#`.replace(new RegExp(pattern, 'g'), changeTo).replace(/#/g, '').trim() - }) + strings = strings.map( + string => string.replace(new RegExp(pattern, 'g'), changeTo) + ) } return strings } From 3adee7925b8b8d1b3e62fb296354b5a2f7edba62 Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Tue, 21 Oct 2025 23:03:51 +0200 Subject: [PATCH 3/5] Refactor not needed RegExp --- soundchanger.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/soundchanger.js b/soundchanger.js index 07bf941..ccb236a 100644 --- a/soundchanger.js +++ b/soundchanger.js @@ -50,13 +50,11 @@ export function reformatChangeToRegex(change, categories = {}, zeroCharacters = const replacements = { ' ': '', '{': '(', '}': ')', ',': '|' } for (const [key, value] of Object.entries(replacements)) { - change = change.replace(new RegExp(regexEscape(key), 'g'), value) + change = change.replace(key, value) } for (const [category, categoryValues] of Object.entries(categories)) { - const categoryPattern = new RegExp(regexEscape(category), 'g') - change = change.replace(categoryPattern, `(${categoryValues.join('|')})`) - change = change.replace(new RegExp(`((?<=,)${regexEscape(category)})|(${regexEscape(category)}(?=,))`, 'g'), categoryValues.join('|')) + change = change.replace(category, `(${categoryValues.join('|')})`) } change = change.replace(new RegExp(`(${zeroCharacters.join('|')})`, 'g'), '') From 62b361c80cca6d1f60b1ec941c0a6ca56435ca71 Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Tue, 21 Oct 2025 23:04:23 +0200 Subject: [PATCH 4/5] Add new test for changing to space character --- tests/apply.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/apply.test.js b/tests/apply.test.js index 1af55ff..f14782a 100644 --- a/tests/apply.test.js +++ b/tests/apply.test.js @@ -23,7 +23,7 @@ describe('apply', () => { it('Simple change to zero', () => { const strings = ["a", "ba", "aba"] const result = ["", "b", "b"] - const changes = ["a>∅", "a>-", "a>"] + const changes = ["a>∅", "a>-", "a>", "a> "] for (const change of changes) { expect(apply( From fcca5c9509e875d302a0a7305fb9220f89946278 Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Tue, 21 Oct 2025 23:05:23 +0200 Subject: [PATCH 5/5] Add test for curly brace categories --- tests/apply.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/apply.test.js b/tests/apply.test.js index f14782a..b23a9ef 100644 --- a/tests/apply.test.js +++ b/tests/apply.test.js @@ -38,4 +38,19 @@ describe('apply', () => { {"A": ["a", "b"]}, )).toEqual(["b", "bb", "bbc"]) }) + it('Change with curly braces', () => { + expect(apply( + ["{a,b}>c"], + ["abc", "bcd"], + )).toEqual(["ccc", "ccd"]) + expect(apply( + ["a>b/{c, d}_"], + ["aa", "ca", "da"], + )).toEqual(["aa", "cb", "db"]) + expect(apply( + ["d>e/{A}_"], + ["ad", "bd", "cd"], + {"A": ["a", "B"], "B": ["b", "c"]}, + )).toEqual(["ae", "be", "ce"]) + }) })