README
πΊ Mapped Replacer π
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.
Table of contents
Install
npm i mapped-replacer
API
addRule(key: string, value: string): boolean
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 π'
addRules(rules: Object): boolean
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 '𝕋 ≈ 𝔱'
removeRule(key: string): boolean
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 'π ≈ π±'
rulesCount(): number
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
clearRules(): void
Clears all the rules.
import { MappedReplacer } from 'mapped-replacer'
const mapper = new MappedReplacer()
mapper.addRule('π', '𝕋')
mapper.clearRules()
console.log(mapper.rulesCount()) // outputs 0
replace(input: string): string
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'
Test
npm test