npm package: @igorskyflyer/mapped-replacer
Description: 🦗 Zero-dependency Map and RegExp based string replacer with Unicode support. 🍁
Author: Igor Dimitrijević
Latest version: 3.0.0

<div align="center">
  <img src="https://raw.githubusercontent.com/igorskyflyer/npm-mapped-replacer/main/media/mapped-replacer.png" alt="Icon of Mapped Replacer" width="256" height="256">
  <h1>Mapped Replacer</h1>
</div>

<br>

<h4 align="center">
  🦗 Zero-dependency Map and RegExp based string replacer with Unicode support. 🍁
</h4>

<br>
<br>

## 📃 Table of Contents

- [Features](#-features)
- [Usage](#-usage)
- [API](#-api)
- [Examples](#️-examples)
- [Changelog](#-changelog)
- [Support](#-support)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)

<br>
<br>

## 🤖 Features

- 🔄 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

<br>
<br>

## 🕵🏼 Usage

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
```

<br>
<br>

## 🤹🏼 API

> ❗ **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`.
>

<br>


### constructor

```ts
constructor(options?: IOptions): MappedReplacer
```

*Creates a new instance of `MappedReplacer`.*  

<br>

### IOptions

`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`.

---

### addRule()

```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.

<br>

```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 😀'

```

<br>

### addRule()

```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.

<br>

```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 😀 😀'
```

---

### addRules()

```ts
addRules(rules: { [replaceWith: string]: string }): boolean
```

*Adds rules for string replacement.*  

`rules` - A simple key-value object, i.e.:  

<br>

```ts
{
  '&#60;' : '<',
  '&#62;' : '>'
}
```  

Returns a Boolean whether the rules were added successfully.

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRules({
  '&#120139;' : '𝕋',
  '&#8776;' : '≈',
  '&#120113;' : '𝔱'
})

console.log(mapper.replace('𝕋 ≈ 𝔱')) // outputs '&#120139; &#8776; &#120113;'
```

<br>


### addRules()

```ts
addRules(rules: { [replaceWith: string]: string[] }): boolean
```

*Adds rules for string replacement.*  

`rules` - A simple key-value[] object, i.e.:  

<br>

```ts
{
  '😁' : [':D', ':-D'],
  '😛' : [':P', ':-P']
}
```  

Returns a Boolean whether the rules were added successfully.

<br>

```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 😛'
```

---

### updateRule

```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.

<br>

```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 😀'

```

<br>

### updateRule()

```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 😀 😀'
```

---

### hasRule()

```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.

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#120139;', '𝕋')
mapper.addRule('&#8776;', '≈')

console.log(mapper.hasRule('𝕋')) // true
```

---

### removeRule()

```ts
removeRule(searchFor: string): boolean
```

*Removes the rule that matches the provided value.*  

`searchFor` - The rule to remove.

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#120139;', '𝕋')
mapper.addRule('&#8776;', '≈')
mapper.removeRule('𝕋')

console.log(mapper.replace('𝕋 ≈ 𝔱')) // outputs '𝕋 &#8776; 𝔱'
```

<br>

### rulesCount()

```ts
rulesCount(): number
```

*Gets the number of rules for string replacing.*

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#120139;', '𝕋')

console.log(mapper.rulesCount()) // outputs 1
```

<br>

### clearRules()

```ts
clearRules(): void
```

*Clears all the rules.*  

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#120139;', '𝕋')
mapper.clearRules()

console.log(mapper.rulesCount()) // outputs 0
```

<br>

### replace(input: string)

```ts
replace(input: string): string
```

*Replaces the values in the input with the values from the Map.*  

`input` - The input string.

<br>

```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#8594;', '→')

console.log(mapper.replace('a → b')) // outputs 'a &#8594; b'
```

<br>
<br>

## 🗒️ Examples

`example.ts`
```ts
import { MappedReplacer } from '@igorskyflyer/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('&#8594;', '→')

console.log(mapper.replace('a → b')) // outputs 'a &#8594; b'
```

<br>
<br>

## 📝 Changelog

📑 The changelog is available here, [CHANGELOG.md](https://github.com/igorskyflyer/npm-mapped-replacer/blob/main/CHANGELOG.md).

<br>
<br>

## 🪪 License

Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-mapped-replacer/blob/main/LICENSE).

<br>
<br>

## 💖 Support

<div align="center">
  I work hard for every project, including this one and your support means a lot to me!
  <br>
  Consider buying me a coffee. ☕
  <br>
  <br>
  <a href="https://ko-fi.com/igorskyflyer" target="_blank"><img src="https://raw.githubusercontent.com/igorskyflyer/igorskyflyer/main/assets/ko-fi.png" alt="Donate to igorskyflyer" width="180" height="46"></a>
  <br>
  <br>
  <em>Thank you for supporting my efforts!</em> 🙏😊
</div>

<br>
<br>

## 🧬 Related

[@igorskyflyer/str-is-in](https://www.npmjs.com/package/@igorskyflyer/str-is-in)

> _🧵 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. 🔍_

<br>

[@igorskyflyer/duoscribi](https://www.npmjs.com/package/@igorskyflyer/duoscribi)

> _✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓_

<br>

[@igorskyflyer/strip-yaml-front-matter](https://www.npmjs.com/package/@igorskyflyer/strip-yaml-front-matter)

> _🦓 Strips YAML front matter from a String or a file. 👾_

<br>

[@igorskyflyer/encode-entities](https://www.npmjs.com/package/@igorskyflyer/encode-entities)

> _🏃‍♂️ Fast and simple Map and RegExp based HTML entities encoder. 🍁_

<br>

[@igorskyflyer/strip-html](https://www.npmjs.com/package/@igorskyflyer/strip-html)

> _🥞 Removes HTML code from the given string. Can even extract text-only from the given an HTML string. ✨_

<br>
<br>
<br>

## 👨🏻‍💻 Author
Created by **Igor Dimitrijević** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).