https://github.com/igorskyflyer/npm-mapped-replacer
๐บ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐
https://github.com/igorskyflyer/npm-mapped-replacer
back-end biome decoder encoder expression igorskyflyer javascript js map module node npm package regexp replacer string typescript vitest
Last synced: 20 days ago
JSON representation
๐บ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐
- Host: GitHub
- URL: https://github.com/igorskyflyer/npm-mapped-replacer
- Owner: igorskyflyer
- License: mit
- Created: 2019-10-07T17:43:58.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-28T15:22:59.000Z (9 months ago)
- Last Synced: 2025-03-22T13:23:16.656Z (about 1 month ago)
- Topics: back-end, biome, decoder, encoder, expression, igorskyflyer, javascript, js, map, module, node, npm, package, regexp, replacer, string, typescript, vitest
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@igor.dvlpr/mapped-replacer
- Size: 543 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
๐บ Mapped Replacer ๐
๐บ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐
๐ Support further development
I work hard for every project, including this one
and your support means a lot to me!
Consider buying me a coffee. โ
Thank you for supporting my efforts! ๐๐
![]()
@igorskyflyer
## ๐ Table of contents
- [Usage](#-usage)
- [API](#-api)
- [constructor()](#constructoroptions-ioptions-mappedreplacer)
- [addRule(replaceWith: string, searchFor: string)](#addrulereplacewith-string-searchfor-string-boolean)
- [addRule(replaceWith: string, searchFor: string[])](#addrulereplacewith-string-searchfor-string-boolean-1)
- [addRules(rules: { [key: string]: string })](#addrulesrules--key-string-string--boolean)
- [addRules(rules: { [key: string]: string[] })](#addrulesrules--key-string-string--boolean-1)
- [hasRule(rules: string)](#hasrulerule-string-boolean)
- [removeRule()](#removerulesearchfor-string-boolean)
- [rulesCount()](#rulescount-number)
- [clearRules()](#clearrules-void)
- [replace()](#replaceinput-string-string)
- [Examples](#-examples)
- [Changelog](#-changelog)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)
## ๐ต๐ผ Usage
Install it by executing:
```shell
npm i "@igor.dvlpr/mapped-replacer"
```
## ๐คน๐ผ API
### `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`.
---
### `addRule(replaceWith: string, searchFor: string): boolean`
*Adds a new rule or 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 added or updated successfully, false otherwise.
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', ':smile:')
console.log(mapper.replace('Hello world :smile:')) // outputs 'Hello world ๐'
```
### `addRule(replaceWith: string, searchFor: string[]): boolean`
*Adds a new rule or 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 added or updated successfully, false otherwise.
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', [':smile:', ':D'])
console.log(mapper.replace('Hello world :smile: :D')) // outputs 'Hello world ๐ ๐'
```---
### `addRules(rules: { [key: string]: string }): boolean`
*Adds or updates the rules for string replacement.*
`rules` - A simple key-value object, i.e.:
```ts
{
'<' : '<',
'>' : '>'
}
```Returns a Boolean whether the rules were added/updated successfully.
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRules({
'๐' : '๐',
'โ' : 'โ',
'๐ฑ' : '๐ฑ'
})console.log(mapper.replace('๐ โ ๐ฑ')) // outputs '๐ โ ๐ฑ'
```
### `addRules(rules: { [key: string]: string[] }): boolean`
*Adds or updates the 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/updated successfully.
```ts
import { MappedReplacer } from '@igor.dvlpr/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 ๐'
```---
### `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 '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', '๐')
mapper.addRule('โ', 'โ')console.log(mapper.hasRule('๐')) // true
```---
### `removeRule(searchFor: string): boolean`
*Removes the rule that matches the provided value.*
`searchFor` - The rule to remove.
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', '๐')
mapper.addRule('โ', 'โ')mapper.removeRule('๐')
console.log(mapper.replace('๐ โ ๐ฑ')) // outputs '๐ โ ๐ฑ'
```
### `rulesCount(): number`
*Gets the number of rules for string replacing.*
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', '๐')
console.log(mapper.rulesCount()) // outputs 1
```
### `clearRules(): void`
*Clears all the rules.*
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('๐', '๐')
mapper.clearRules()console.log(mapper.rulesCount()) // outputs 0
```
### `replace(input: string): string`
*Replaces the values in the input with the values from the Map.*
`input` - The input string.
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('โ', 'โ')
console.log(mapper.replace('a โ b')) // outputs 'a โ b'
```---
## โจ Examples
`example.ts`
```ts
import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'const mapper: MappedReplacer = new MappedReplacer()
mapper.addRule('โ', 'โ')
console.log(mapper.replace('a โ b')) // outputs 'a โ b'
```---
## ๐ Changelog
๐ The changelog is available here: [CHANGELOG.md](https://github.com/igorskyflyer/npm-mapped-replacer/blob/main/CHANGELOG.md).
---
## ๐ชช License
Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-mapped-replacer/blob/main/LICENSE).
---
## ๐งฌ Related
[@igor.dvlpr/str-is-in](https://www.npmjs.com/package/@igor.dvlpr/str-is-in)
> _๐งต Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. ๐_
[@igor.dvlpr/duoscribi](https://www.npmjs.com/package/@igor.dvlpr/duoscribi)
> _โ DรบรถScrรญbรฎ allows you to convert letters with diacritics to regular letters. ๐ค_
[@igor.dvlpr/strip-yaml-front-matter](https://www.npmjs.com/package/@igor.dvlpr/strip-yaml-front-matter)
> _๐ฆ Strips YAML front matter from a String or a file. ๐พ_
[@igor.dvlpr/encode-entities](https://www.npmjs.com/package/@igor.dvlpr/encode-entities)
> _๐โโ๏ธ Fast and simple Map and RegExp based HTML entities encoder. ๐_
[@igor.dvlpr/strip-html](https://www.npmjs.com/package/@igor.dvlpr/strip-html)
> _๐ฅ Removes HTML code from the given string. Can even extract text-only from the given an HTML string. โจ_
---
### ๐จ๐ปโ๐ป Author
Created by **Igor Dimitrijeviฤ** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).