Skip to content

๐Ÿ—บ Zero-dependency Map and RegExp based string replacer with Unicode support. ๐Ÿ

License

Notifications You must be signed in to change notification settings

igorskyflyer/npm-mapped-replacer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ—บ 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

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


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


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

Returns a Boolean whether the rules were added/updated successfully.


import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()

mapper.addRules({
  '&#120139;' : '๐•‹',
  '&#8776;' : 'โ‰ˆ',
  '&#120113;' : '๐”ฑ'
})

console.log(mapper.replace('๐•‹ โ‰ˆ ๐”ฑ')) // outputs '&#120139; &#8776; &#120113;'

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('&#120139;', '๐•‹')
mapper.addRule('&#8776;', 'โ‰ˆ')

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('&#120139;', '๐•‹')
mapper.addRule('&#8776;', 'โ‰ˆ')

mapper.removeRule('๐•‹')

console.log(mapper.replace('๐•‹ โ‰ˆ ๐”ฑ')) // outputs '๐•‹ &#8776; ๐”ฑ'

rulesCount(): number

Gets the number of rules for string replacing.


import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()

mapper.addRule('&#120139;', '๐•‹')

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('&#120139;', '๐•‹')
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('&#8594;', 'โ†’')

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

โœจ Examples

example.ts

import { MappedReplacer } from '@igor.dvlpr/mapped-replacer'

const mapper: MappedReplacer = new MappedReplacer()

mapper.addRule('&#8594;', 'โ†’')

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

๐Ÿ“ Changelog

๐Ÿ“‘ The changelog is available here: CHANGELOG.md.


๐Ÿชช License

Licensed under the MIT license which is available here, MIT license.


๐Ÿงฌ Related

@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

โœ’ 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. ๐Ÿ‘พ


@igor.dvlpr/encode-entities

๐Ÿƒโ€โ™‚๏ธ Fast and simple Map and RegExp based HTML entities encoder. ๐Ÿ


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