Zero-dependency Map and RegExp based string replacer with Unicode support.
Requires ES6 or later, for compatibility check this Can I Use link.
β¨Since v.1.1.0
Mapped Replacer
is a hybrid module that supports both CommonJS (legacy) and ES modules, thanks to Modern Module.
npm i mapped-replacer
Adds a new rule or updates the existing rule for character replacing.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule(':smile:', 'π')
console.log(mapper.replace('Hello world :smile:')) // outputs 'Hello world π'
Adds rules or updates the existing rules for character replacing.
Passed object is a simple key-value object, i.e. { '<': '<', '>': '>'}
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRules({
'π': '𝕋',
'β': '≈',
'π±': '𝔱',
})
console.log(mapper.replace('π β π±')) // outputs '𝕋 ≈ 𝔱'
Removes the rule that matches the provided key.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule('π', '𝕋')
mapper.addRule('β', '≈')
mapper.removeRule('π')
console.log(mapper.replace('π β π±')) // outputs 'π ≈ π±'
Gets the number of rules for character replacing.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule('π', '𝕋')
console.log(mapper.rulesCount()) // outputs 1
Clears all the rules.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule('π', '𝕋')
mapper.clearRules()
console.log(mapper.rulesCount()) // outputs 0
Replaces the values in the input that match the keys in the Map object.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule('β', '→')
console.log(mapper.replace('a β b')) // outputs 'a → b'
npm i
npm test