Skip to content

Commit c193574

Browse files
refactor: improve substitute array mapping
1 parent 41d9d9a commit c193574

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/helpers/substitute.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ export const substitute = (input, options = {}) => {
44
const mappingEntries = Object.entries(options.mapping)
55

66
return input.replace(/./g, x => {
7-
const transformedEntries = options.caseSensitive ? mappingEntries : mappingEntries.map(([k, v]) => isLowerCase(x) ? [k.toLowerCase(), v.toLowerCase()] : [k.toUpperCase(), v.toUpperCase()])
8-
const foundMapping = transformedEntries.find(([k, v]) => k === x || v === x)
7+
const transformedEntries = options.caseSensitive
8+
? mappingEntries
9+
: mappingEntries.map(array => array.map(c => isLowerCase(x) ? c.toLowerCase() : c.toUpperCase()))
10+
11+
const foundMapping = transformedEntries.find(kvArray => kvArray.includes(x))
12+
913
return !foundMapping ? x : foundMapping[0] === x ? foundMapping[1] : foundMapping[0]
1014
})
1115
}

0 commit comments

Comments
 (0)