@igor.dvlpr/mapped-replacer
v2.2.0
Published
๐บ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐
Downloads
32
Maintainers
Readme
๐ Table of contents
๐ต๐ผ Usage
Install it by executing:
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 istrue
.strict
- A Boolean that indicates whether strict mode is enabled. In strict mode, only whole matches are replaced. Default isfalse
.
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.
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.
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.:
{
'<' : '<',
'>' : '>'
}
Returns a Boolean whether the rules were added/updated successfully.
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.:
{
'๐' : [':D', ':-D'],
'๐' : [':P', ':-P']
}
Returns a Boolean whether the rules were added/updated successfully.
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.
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.
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.
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.
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.
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
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.
๐ชช License
Licensed under the MIT license which is available here, MIT license.
๐งฌ Related
๐งต 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. ๐ค
@igor.dvlpr/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. โจ
๐จ๐ปโ๐ป Author
Created by Igor Dimitrijeviฤ (@igorskyflyer).