๐ฆ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐
npm install @igorskyflyer/mapped-replacer
- Features
- Usage
- API
- Examples
- Changelog
- Support
- License
- Related
- Author
- ๐ Define or update single or multiple text replacement rules in one call
- ๐ Batchโload rules from plain objects or keyโtoโarray mappings
- ๐ Toggle caseโsensitive or caseโinsensitive matching via options
- ๐ Enable strict mode to match only whole words using compiled RegExp boundaries
- โ
Check if a specific replacement rule exists before using it
- โ Remove individual rules without affecting others
- ๐ Get the exact number of active rules at any time
- ๐งน Clear all rules instantly to start fresh
- โ๏ธ Replace all matches in a string in a single, efficient pass
- ๐ Match Unicode, emojis, and nonโLatin scripts with the u flag
- โก Zeroโdependency, lightweight, TypeScriptโready ES module
Install it by executing any of the following, depending on your preferred package manager:
``bash`
pnpm add @igorskyflyer/mapped-replacer
`bash`
yarn add @igorskyflyer/mapped-replacer
`bash`
npm i @igorskyflyer/mapped-replacer
> โ IMPORTANT
>
> Why use replaceWith โ searchFor instead of the usual searchFor โ replaceWith?
>
> Instead of looping over each search term and rescanning the input O(n ร m), all rules are preโcompiled into a single alternation regex once (O(totalKeyLength ร logโฏn) with sorting). At runtime, replacements happen in a single O(m) pass with constantโtime map lookups, reducing repeated scans and ensuring longestโmatchโfirst priority without extra cost. In practice, this can cut runtime by up to ~90% when handling dozens or hundreds of patterns on large inputs, since the search cost collapses from n full scans to just one.
>
`ts`
constructor(options?: IOptions): MappedReplacer
Creates a new instance of MappedReplacer.
options is a variable of type IOptions defined as:
- caseSensitive - A Boolean that indicates whether replacing should be case-sensitive or not. Default is true.
- strict - A Boolean that indicates whether strict mode is enabled. In strict mode, only whole matches are replaced. Default is false.
- longestMatchFirst - When true, overlapping search patterns are matched in order of longest pattern length first, rather than the order they were added. This prevents shorter patterns from "stealing" matches that should belong to longer, more specific patterns.Use false only if you need strict insertion-order precedence. Default is true.
---
`ts`
addRule(replaceWith: string, searchFor: string): boolean
Adds a new rule used in replacing a single string.
replaceWith - The string to replace the searchFor with.
searchFor - The string to be replaced.
Returns true if the rule was added successfully, false otherwise.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', ':smile:')
console.log(mapper.replace('Hello world :smile:')) // outputs 'Hello world ๐'
`
`ts`
addRule(replaceWith: string, searchFor: string[]): boolean
Adds a new rule for character replacement with multiple subjects.
replaceWith - The string to replace the searchFor with.
searchFor - The array of strings to be replaced.
Returns true if the rule was added successfully, false otherwise.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', [':smile:', ':D'])
console.log(mapper.replace('Hello world :smile: :D')) // outputs 'Hello world ๐ ๐'
`
---
`ts`
addRules(rules: { [replaceWith: string]: string }): boolean
Adds rules for string replacement.
rules - A simple key-value object, i.e.:
`ts`
{
'<' : '<',
'>' : '>'
}
Returns a Boolean whether the rules were added successfully.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRules({
'𝕋' : '๐',
'≈' : 'โ',
'𝔱' : '๐ฑ'
})
console.log(mapper.replace('๐ โ ๐ฑ')) // outputs '𝕋 ≈ 𝔱'
`
`ts`
addRules(rules: { [replaceWith: string]: string[] }): boolean
Adds rules for string replacement.
rules - A simple key-value[] object, i.e.:
`ts`
{
'๐' : [':D', ':-D'],
'๐' : [':P', ':-P']
}
Returns a Boolean whether the rules were added successfully.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRules({
'๐' : [':D', ':-D'],
'๐' : [':P', ':-P']
})
console.log(mapper.replace('Hello :D world :-D this is a :P test :-P')) // outputs 'Hello ๐ world ๐ this is a ๐ test ๐'
`
---
`ts`
updateRule(replaceWith: string, searchFor: string): boolean
Updates an existing rule used in replacing a single string.
replaceWith - The string to replace the searchFor with.
searchFor - The string to be replaced.
Returns true if the rule was updated, false otherwise.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐คจ', ':smile:')
mapper.updateRule('๐', ':smile:')
console.log(mapper.replace('Hello world :smile:')) // outputs 'Hello world ๐'
`
`ts`
updateRule(replaceWith: string, searchFor: string[]): boolean
Updates an existing rule for character replacement with multiple subjects.
replaceWith - The string to replace the searchFor with.
searchFor - The array of strings to be replaced.
Returns true if the rule was updated, false otherwise.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐คจ', [':smile:', ':D'])
mapper.addRule('๐', [':smile:', ':D'])
console.log(mapper.replace('Hello world :smile: :D')) // outputs 'Hello world ๐ ๐'
`
---
`ts`
hasRule(rule: string): boolean
Checks whether a rule is present in the Map.
rule - The rule to check for.
Returns a Boolean indicating the existence of the given rule.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('𝕋', '๐')
mapper.addRule('≈', 'โ')
console.log(mapper.hasRule('๐')) // true
`
---
`ts`
removeRule(searchFor: string): boolean
Removes the rule that matches the provided value.
searchFor - The rule to remove.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('𝕋', '๐')
mapper.addRule('≈', 'โ')
mapper.removeRule('๐')
console.log(mapper.replace('๐ โ ๐ฑ')) // outputs '๐ ≈ ๐ฑ'
`
`ts`
rulesCount(): number
Gets the number of rules for string replacing.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('𝕋', '๐')
console.log(mapper.rulesCount()) // outputs 1
`
`ts`
clearRules(): void
Clears all the rules.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('𝕋', '๐')
mapper.clearRules()
console.log(mapper.rulesCount()) // outputs 0
`
`ts`
replace(input: string): string
Replaces the values in the input with the values from the Map.
input - The input string.
`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('→', 'โ')
console.log(mapper.replace('a โ b')) // outputs 'a → b'
`
example.ts`ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'
const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('→', 'โ')
console.log(mapper.replace('a โ b')) // outputs 'a → b'
``
๐ The changelog is available here, CHANGELOG.md.
Licensed under the MIT license which is available here, MIT license.
> _๐งต Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. ๐_
> _โ DรบรถScrรญbรฎ allows you to convert letters with diacritics to regular letters. ๐ค_
@igorskyflyer/strip-yaml-front-matter
> _๐ฆ Strips YAML front matter from a String or a file. ๐พ_
> _๐โโ๏ธ Fast and simple Map and RegExp based HTML entities encoder. ๐_
> _๐ฅ Removes HTML code from the given string. Can even extract text-only from the given an HTML string. โจ_