1.2.1 β€’ Published 2 years ago

mapped-replacer v1.2.1

Weekly downloads
5
License
MIT
Repository
github
Last release
2 years ago

πŸ—Ί 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({
  '𝕋': '&#120139;',
  'β‰ˆ': '&#8776;',
  '𝔱': '&#120113;',
})

console.log(mapper.replace('𝕋 β‰ˆ 𝔱')) // outputs '&#120139; &#8776; &#120113;'

removeRule(key: string): boolean

Removes the rule that matches the provided key.

import { MappedReplacer } from 'mapped-replacer'

const mapper = new MappedReplacer()

mapper.addRule('𝕋', '&#120139;')
mapper.addRule('β‰ˆ', '&#8776;')

mapper.removeRule('𝕋')

console.log(mapper.replace('𝕋 β‰ˆ 𝔱')) // outputs '𝕋 &#8776; 𝔱'

rulesCount(): number

Gets the number of rules for character replacing.

import { MappedReplacer } from 'mapped-replacer'

const mapper = new MappedReplacer()

mapper.addRule('𝕋', '&#120139;')

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

clearRules(): void

Clears all the rules.

import { MappedReplacer } from 'mapped-replacer'

const mapper = new MappedReplacer()

mapper.addRule('𝕋', '&#120139;')
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('β†’', '&#8594;')

console.log(mapper.replace('a β†’ b')) // outputs 'a &#8594; b'

Test

npm i
npm test
1.2.1

2 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.10

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago