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

Content added Content deleted
Line 272: Line 272:
// the nth instances of various characters.
// the nth instances of various characters.
s => mapAccumL(
s => mapAccumL(
(a, c) => c in ruleMap ? (() => {
(a, c) => c in a ? (() => {
const
const ds = a[c];
i = a[c] || 0,
ds = ruleMap[c];


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