1.0.5 • Published 5 years ago

@fav/text.escape v1.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

@fav/text.escape NPM MIT License Build Status Build Status Coverage status

Escapes characters for various syntaxes and formats.

"fav" is an abbreviation of "favorite" and also the acronym of "for all versions". This package is intended to support all Node.js versions and many browsers as possible. At least, this package supports Node.js >= v0.10 and major Web browsers: Chrome, Firefox, IE11, Edge, Vivaldi and Safari.

Install

To install from npm:

$ npm install --save @fav/text.escape

NOTE: npm < 2.7.0 does not support scoped package, but old version Node.js supports it. So when you use such older npm, you should download this package from github.com, and move it in node_modules/@fav/text.escape/ directory manually.

Usage

For Node.js:

var escape = require('@fav/text.escape').RegExp;
escape('[@fav/text.escape]');  // => '\\[@fav/text\\.escape\\]'
console.log(escape('[@fav/text.escape]')); // => \[@fav/text\.escape\]

For Web browsers:

<script src="fav.text.escape.min.js"></script>
<script>
var escape = fav.text.escape.RegExp;
escape('[@fav/text.escape]');  // => '\\[@fav/text\\.escape\\]'
console.log(escape('[@fav/text.escape]'));  // => \[@fav/text\.escape\]
</script>

API

escape : object

Is a set of functins to escape characters in a string.

This function set provides escapings for following syntaxes and formats:

In addition, the factory functions for two types of escaping are provided:

NOTE: These functions doesn't check data types of the arguments, and assumes that they are given as per the specific data types.

escape.RegExp(source) : string

Escapes special characters of Regular Expression.

The special characters which are escaped are as follows: ^$\.*+?()[]{}|.

The specification of the special characters of Regular Expression comes from ECMA-262 — 21.2 RegExp (Regular Expression) Objects.

Parameter:
ParameterTypeDescription
sourcestringThe source string.
Return:
TypeDescriptionn
stringAn escaped string.

escape.RegExpCharClass(source) : string

Escapes special characters of Regular Expression Character Class.

The special characters which are escaped are as follows: -^]\.

Parameter:
ParameterTypeDescription
sourcestringThe source string.
Return:
TypeDescription
stringAn escaped string.

escape.HtmlEntity(source) : string

Escapes special characters of HTML entity to character references, etc.

The escape mapping for HTML entity is as follows:

source characterreplaced text
'<' (\u003c)'&lt;'
'>' (\u003e)'&gt;'
'&' (\u0026)'&amp;'
' ' (\u0020)'&nbsp;'
'\n' (\u000a)'<br/>'

NOTE: To reserve multiple spaces(\u0020) and EOL(\n), should use CSS's white-space: pre-wrap.

Parameter:
ParameterTypeDescription
sourcestringThe source string.
Return:
TypeDescription
stringAn escaped string.

escape.HtmlAttribute(source) : string

Escapes special characters of HTML attributes to character references.

The escape mapping for HTML attribute is as follows:

source characterreplaced text
'<' (\u003c)'&lt;'
'>' (\u003e)'&gt;'
'&' (\u0026)'&amp;'
'"' (\u0022)'&quot;'
"'" (\u0027)'&apos;'
Parameter:
ParameterTypeDescription
sourcestringThe source string.
Return:
TypeDescription
stringAn escaped string.

escape.byPreposition(escapingChar , ...escapedChars) : function

Creates an escape function which escapes special characters by preposition of an espacing character, for example:

var escape = fav.text.escape.byPreposition('\\', '"\'');
escape('escaping ", \' and \\.');
// => 'escaping \\", \\\' and \\\\.'
Parameter:
ParameterTypeDescription
escapingCharstringThe escaping character placed before special characters. This character is escaped, too.
escapedCharsstringThe special characters to be escaped.
Return:
TypeDescription
functionAn escaping function.

escape.byReplacement(escapingMap) : function

Creates an escape function which escapes special characters by replacement to corresponding strings, for examples:

var escape = fav.text.escape.byReplacement({ '"': '&quot;', "'": '&apos;' });
escape('escaping " and  \'.');
// => 'escaping &quot; and &apos;.'
Parameter:
ParameterTypeDescription
escapingMapobjectThe plain object of which keys and values are mappings of escaped characters and replaced strings.
Return:
TypeDescription
functionAn escaping function.

Checked

Node.js (4〜12)

Platform1112
macOS
Windows10
Linux
Platform45678910
macOS
Windows10
Linux

io.js (1〜3)

Platform123
macOS
Windows10
Linux

Node.js (〜0.12)

Platform0.80.90.100.110.12
macOS
Windows10
Linux

Web browsers

PlatformChromeFirefoxVivaldiSafariEdgeIE11
macOS----
Windows10--
Linux------

License

Copyright (C) 2017-2019 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.