Selectively replace multiple instances of a character within a string: Difference between revisions

Content added Content deleted
Line 237: Line 237:
// A string defined by replacements specified for
// A string defined by replacements specified for
// the nth instances of various characters.
// the nth instances of various characters.
s => {
s => mapAccumL(
const go = (a, c) =>
(a, c) => c in ruleMap ? (() => {
c in ruleMap ? (() => {
const
const
i = a[c] || 0,
i = a[c] || 0,
ds = ruleMap[c];
ds = ruleMap[c];

return [
Object.assign(a, {[c]: 1 + i}),
i < ds.length ? (
ds[i] || c
) : c
];
})() : [a, c];

return mapAccumL(go)({})([...s])[1].join("");
};


return [
Object.assign(a, {[c]: 1 + i}),
i < ds.length ? (
ds[i] || c
) : c
];
})() : [a, c]
)({})([...s])[1].join("");


// ---------------------- TEST -----------------------
// ---------------------- TEST -----------------------