An open API service indexing awesome lists of open source software.

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. ๐Ÿ

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! ๐Ÿ™๐Ÿ˜Š





Donate to igorskyflyer




@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/)).