Socket
Socket
Sign inDemoInstall

escaper

Package Overview
Dependencies
0
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    escaper

Small JavaScript library for replacing string literals, regular expressions and comments in JavaScript syntax.


Version published
Weekly downloads
4.3K
increased by55.78%
Maintainers
1
Install size
40.2 kB
Created
Weekly downloads
 

Readme

Source

Escaper

Escaper is a small JavaScript library for replacing string literals, regular expressions and comments in JavaScript syntax.

Russian documentation

NPM version NPM devDependencies Build Status Coverage Status

Supports:

  • ' ... '
  • " ... "
  • ` ... `, ` ... ${...} `
  • / ... /
  • // ..., //* ..., //! ..., //# ..., //@ ..., //$ ...
  • /* ... */, /** ... */, /*! ... */, /*# ... */, /*@ ... */, /*$ ... */

Install

https://raw.githubusercontent.com/kobezzza/Escaper/master/dist/escaper.min.js

or

npm install escaper

or

bower install escaper

or

git clone https://github.com/kobezzza/Escaper

Usage

const
  str = 'Hello "world" and \'friends\'',
  content = [];

// Replaces all found matches
// 'Hello __ESCAPER_QUOT__0_ and __ESCAPER_QUOT__1_'
Escaper.replace(str, content)

// Replaces only single quotes
// 'Hello "world" and __ESCAPER_QUOT__0_'
Escaper.replace(str, ["'"])

// Cuts all found matches
// 'Hello and '
Escaper.replace(str, -1)

// Replaces all and cuts single quotes
// 'Hello __ESCAPER_QUOT__0_ and '
Escaper.replace(str, {"'": -1})

// Replaces all but strings
// 'Hello __ESCAPER_QUOT__0_ and \'friends\''
Escaper.replace(str, {strings: false})

// Replaces all, but strings can be only single quotes
// 'Hello "world" and __ESCAPER_QUOT__0_'
Escaper.replace(str, {strings: ["'"]})

// Replaces all, but strings can be only single quotes and it will be cut
// 'Hello "world" and '
Escaper.replace(str, {strings: {"'": -1}})

// Replaces all found escape blocks to a real content
// 'Hello "world" and \'friends\''
Escaper.paste(str, content);

API

Escaper.replace(str, how?, content?): string

Replaces all found blocks ' ... ', " ... ", ` ... `, / ... /, // ..., /* ... */ to escape blocks in a string and returns a new string.

Arguments

  • string str — source string;
  • string[] | Record<string, string[] | Record<string, boolean | -1> | false | -1> | false | -1 how? — parameters:

Possible values

If a value is set to -1, then all found matches will be removed from the final string, or if the value will be set to boolean they will be included/excluded.

// Template for replacement, by default __ESCAPER_QUOT__${pos}_
'label'

// Singleline comment
'singleComments'

// Multiline comments
'multComments'

// All kinds of comments
'comments'

// All kinds of strings
'strings'

// All kinds of literals (except strings and comments)
'literals'

// Literals
"'"
'"'
'`'
'/'
'//'
'//*'
'//!'
'//#'
'//@'
'//$'
'/*'
'/**'
'/*!'
'/*#'
'/*@'
'/*$'

Parameters can be specified as an array (escapes only explicitly specified sequences) or like an object (disables/excludes by a literal or a group). Also, if you set the parameter value as -1, then all found sequences will be removed from the string.

  • string[] content = Escaper.content — array for matches.

Escaper.paste(str, content?, rgxp?): string

Replaces all found escape blocks to a real content in a string and returns a new string.

Arguments

  • string str — source string;
  • string[] content = Escaper.content — array of matches;
  • RegExp rgxp? — RegExp for searching, e.g. /__ESCAPER_QUOT__(\d+)_/g.

License

The MIT License.

Keywords

FAQs

Last updated on 14 May 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc