npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

@igor.dvlpr/mapped-replacer

v2.2.0

Published

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

Downloads

38

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