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

string-escape-map

v2.0.0

Published

Escape a given map of special characters

Downloads

2,125

Readme

workflow Jest coverage

node-string-escape-map

Escape a given map of special characters

Installation

npm install string-escape-map

Usage

//const stringEscape = require ('string-escape-map') // v.1.x.x � CommonJS

import stringEscape from 'string-escape-map'         // v.2.x.x � ES6 module

// initialization
const MY_ESC = new stringEscape ([
  ['\t', '\\t'],
  ['\n', '\\n'],
  [ "'", "''"],
])

// possible later adjustment
MY_ESC.set ('\r', '')

// run time usage
const unsafeString = `Don't
you?`

const safeString = MY_ESC.escape (unsafeString)

Details

Constructor

The class provided by string-escape-map is derived from Map and shares its constructor argument format: if set, it must be an iterable of key-value pairs.

Additional restrictions on input are same as for the set method (see below).

Methods

set

The standard set method is overloaded to effectively store char codes to safe substring mapping. So:

  • key and value must be (primitive) strings;
  • key must be a single character string.

Under the hood, the key parameter is subject to charCodeAt.

Other than registering a map entry, it sets escape1 or escapeN as this.escape method.

escape

This method executes the module's main task: replaces all the characters in question with their safe representations

const safeString = MY_ESC.escape (unsafeString)

Parameter

unsafeString must be a primitive string.

null, undefined etc. values cause errors.

Zero length strings are allowed.

Return value

A primitive string with all occurrences of each character previously set replaced with corresponding substrings.

escapeN

This method is used as escape in case when more than one entry is set in this map. It scans through unsafeString, detect unsafe chars with charCodeAt and concatenates the result from safe slices glued with replacement strings from this Map.

escape1

This method is used as escape when only one unsafe character is known, so this works as replaceAll. The unsafeString is scanned with indexOf, the result is assembled from the slices detected.

Implementation notes

No replace nor replaceAll method is used.

No regular expression is constructed.

The given string is scanned with charCodeAt (which is significantly faster and more memory efficient than charAt).

If no unsafe character is ever found, the argument is passed through untouched, without creating any temporary object at all.

Otherwise, the resulting string is created by concatenating complete safe slices with replacement substrings for unsafe chars.