3.0.6 • Published 3 years ago

escaper v3.0.6

Weekly downloads
15,447
License
MIT
Repository
github
Last release
3 years ago

Escaper

Escaper is a small JavaScript library to replace string literals, regular expressions, and comments from the 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?, store?): string

Replaces all found blocks ' ... ', " ... ", ` ... `, / ... /, // ..., /* ... */ to escape blocks from the specified 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. If the value is set to boolean it will be replaced/passed.

// Label of 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
"'"
'"'
'`'
'/'
'//'
'//*'
'//!'
'//#'
'//@'
'//$'
'/*'
'/**'
'/*!'
'/*#'
'/*@'
'/*$'

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

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

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

Replaces all found escape blocks to real content from the specified string and returns a new string.

Arguments

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

License

The MIT License.

3.0.6

3 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

6 years ago

2.5.3

6 years ago

2.5.2

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.34

7 years ago

2.4.33

7 years ago

2.4.32

8 years ago

2.4.31

8 years ago

2.4.30

8 years ago

2.4.29

8 years ago

2.4.28

8 years ago

2.4.27

8 years ago

2.4.26

8 years ago

2.4.25

8 years ago

2.4.24

8 years ago

2.4.23

8 years ago

2.4.22

8 years ago

2.4.21

8 years ago

2.4.20

8 years ago

2.4.19

8 years ago

2.4.18

8 years ago

2.4.17

8 years ago

2.4.16

8 years ago

2.4.15

8 years ago

2.4.14

8 years ago

2.4.13

8 years ago

2.4.12

9 years ago

2.4.11

9 years ago

2.4.10

9 years ago

2.4.9

9 years ago

2.4.8

9 years ago

2.4.7

9 years ago

2.4.6

9 years ago

2.4.5

9 years ago

2.4.4

9 years ago

2.4.3

9 years ago

2.4.2

9 years ago

2.4.1

9 years ago

2.4.0

9 years ago

2.3.9

9 years ago

2.3.8

9 years ago

2.3.7

9 years ago

2.3.6

9 years ago

2.3.5

9 years ago

2.3.4

9 years ago

2.3.3

9 years ago

2.3.2

9 years ago

2.3.1

9 years ago

2.3.0

9 years ago

2.2.25

9 years ago

2.2.24

9 years ago

2.2.23

9 years ago

2.2.22

9 years ago

2.2.21

9 years ago

2.2.20

9 years ago

2.2.19

9 years ago

2.2.18

9 years ago

2.2.17

9 years ago

2.2.16

9 years ago

2.2.15

9 years ago

2.2.14

9 years ago

2.2.13

9 years ago

2.2.12

9 years ago

2.2.11

9 years ago

2.2.10

9 years ago

2.2.9

9 years ago

2.2.8

9 years ago

2.2.7

9 years ago

2.2.6

9 years ago

2.2.5

9 years ago

2.2.4

9 years ago

2.2.3

9 years ago

2.2.2

9 years ago

2.2.1

9 years ago

2.2.0

9 years ago

2.1.18

9 years ago

2.1.17

9 years ago

2.1.16

9 years ago

2.1.15

9 years ago

2.1.14

9 years ago

2.1.13

9 years ago

2.1.11

9 years ago

2.1.10

9 years ago

2.1.9

9 years ago

2.1.8

9 years ago

2.1.7

9 years ago

2.1.6

9 years ago

2.1.5

9 years ago

2.1.4

9 years ago

2.1.3

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.8

9 years ago

2.0.7

9 years ago

2.0.6

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.4.19

9 years ago

1.4.18

9 years ago

1.4.17

9 years ago

1.4.16

9 years ago

1.4.15

9 years ago

1.4.14

10 years ago

1.4.13

10 years ago

1.4.12

10 years ago

1.4.11

10 years ago

1.4.10

10 years ago

1.4.9

10 years ago

1.4.8

10 years ago

1.4.7

10 years ago

1.4.6

10 years ago

1.4.5

10 years ago

1.4.4

10 years ago

1.4.3

10 years ago

1.4.2

10 years ago

1.4.1

10 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.6

10 years ago

1.2.5

10 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.6

10 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago