string-kit
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -85,3 +85,3 @@ # TOC | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression. | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression pattern. | ||
@@ -94,2 +94,15 @@ ```js | ||
escape.regExpReplacement() should escape a string so that it will be suitable as a literal string into a regular expression replacement. | ||
```js | ||
expect( string.escape.regExpReplacement( "$he love$ dollar$ $$$" ) ).to.be( "$$he love$$ dollar$$ $$$$$$" ) ; | ||
expect( | ||
'$he love$ dollar$ $$$'.replace( | ||
new RegExp( string.escape.regExp( '$' ) , 'g' ) , | ||
string.escape.regExpReplacement( '$1' ) | ||
) | ||
).to.be( "$1he love$1 dollar$1 $1$1$1" ) ; | ||
``` | ||
escape.html() should escape a string so that it will be suitable as HTML content. | ||
@@ -102,3 +115,3 @@ | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute. | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute's value. | ||
@@ -105,0 +118,0 @@ ```js |
@@ -5,6 +5,7 @@ | ||
A string manipulation toolbox. | ||
A string manipulation toolbox, featuring a string formater (inspired by printf), a variable inspector | ||
(output featuring ANSI color and HTML) and various escape function (shell argument, regexp, html, etc). | ||
* License: MIT | ||
* Current status: early alpha | ||
* Current status: beta | ||
* Platform: Node.js only (browser support is planned) | ||
@@ -22,6 +23,81 @@ | ||
# Escape functions collection | ||
## .escape.shellArg( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as a shell command's argument. | ||
## .escape.regExp( str ) , .escape.regExpPattern( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable to inject it in a regular expression's pattern as a literal string. | ||
Example of a search and replace from a user's input: | ||
```js | ||
var result = data.replace( | ||
new RegExp( stringKit.escape.regExp( userInputSearch ) , 'g' ) , | ||
stringKit.escape.regExpReplacement( userInputReplace ) | ||
) ; | ||
``` | ||
## .escape.regExpReplacement( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as a literal string for a regular expression's replacement. | ||
## .escape.html( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as HTML content. | ||
Only `< > &` are replaced by HTML entities. | ||
## .escape.htmlAttr( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as an HTML tag attribute's value. | ||
Only `< > & "` are replaced by HTML entities. | ||
It assumes valid HTML: the attribute's value should be into double quote, not in single quote. | ||
## .escape.htmlSpecialChars( str ) | ||
* str `String` the string to filter | ||
It escapes all HTML special characters, `< > & " '` are replaced by HTML entities. | ||
## .escape.control( str ) | ||
* str `String` the string to filter | ||
It escapes all ASCII control characters (code lesser than or equals to 0x1F, or *backspace*). | ||
*Carriage return*, *newline* and *tabulation* are respectively replaced by `\r`, `\n` and `\t`. | ||
Other characters are replaced by the unicode notation, e.g. `NUL` is replaced by `\x00`. | ||
Full BDD spec generated by Mocha: | ||
@@ -227,8 +227,12 @@ /* | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions | ||
string.escape.regExp = function escapeRegExp( str ) { | ||
string.escape.regExp = string.escape.regExpPattern = function escapeRegExpPattern( str ) { | ||
return str.replace( /([.*+?^${}()|\[\]\/\\])/g , '\\$1' ) ; | ||
} ; | ||
string.escape.regExpReplacement = function escapeRegExpReplacement( str ) { | ||
return str.replace( /\$/g , '$$$$' ) ; | ||
} ; | ||
string.escape.shellArg = function escapeShellArg( str ) { | ||
@@ -235,0 +239,0 @@ return '\'' + str.replace( /\'/g , "'\\''" ) + '\'' ; |
{ | ||
"name": "string-kit", | ||
"version": "0.0.6", | ||
"description": "A string manipulation toolbox.", | ||
"version": "0.0.7", | ||
"description": "A string manipulation toolbox, featuring a string formater (inspired by printf), a variable inspector (output featuring ANSI color and HTML) and various escape function (shell argument, regexp, html, etc).", | ||
"main": "lib/string.js", | ||
@@ -27,3 +27,13 @@ "directories": { | ||
"string", | ||
"manipulation" | ||
"manipulation", | ||
"format", | ||
"printf", | ||
"inspect", | ||
"color", | ||
"debug", | ||
"dump", | ||
"escape", | ||
"shell", | ||
"regexp", | ||
"html" | ||
], | ||
@@ -30,0 +40,0 @@ "author": "Cédric Ronvel", |
@@ -5,6 +5,7 @@ | ||
A string manipulation toolbox. | ||
A string manipulation toolbox, featuring a string formater (inspired by printf), a variable inspector | ||
(output featuring ANSI color and HTML) and various escape function (shell argument, regexp, html, etc). | ||
* License: MIT | ||
* Current status: early alpha | ||
* Current status: beta | ||
* Platform: Node.js only (browser support is planned) | ||
@@ -22,4 +23,79 @@ | ||
# Escape functions collection | ||
## .escape.shellArg( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as a shell command's argument. | ||
## .escape.regExp( str ) , .escape.regExpPattern( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable to inject it in a regular expression's pattern as a literal string. | ||
Example of a search and replace from a user's input: | ||
```js | ||
var result = data.replace( | ||
new RegExp( stringKit.escape.regExp( userInputSearch ) , 'g' ) , | ||
stringKit.escape.regExpReplacement( userInputReplace ) | ||
) ; | ||
``` | ||
## .escape.regExpReplacement( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as a literal string for a regular expression's replacement. | ||
## .escape.html( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as HTML content. | ||
Only `< > &` are replaced by HTML entities. | ||
## .escape.htmlAttr( str ) | ||
* str `String` the string to filter | ||
It escapes the string so that it will be suitable as an HTML tag attribute's value. | ||
Only `< > & "` are replaced by HTML entities. | ||
It assumes valid HTML: the attribute's value should be into double quote, not in single quote. | ||
## .escape.htmlSpecialChars( str ) | ||
* str `String` the string to filter | ||
It escapes all HTML special characters, `< > & " '` are replaced by HTML entities. | ||
## .escape.control( str ) | ||
* str `String` the string to filter | ||
It escapes all ASCII control characters (code lesser than or equals to 0x1F, or *backspace*). | ||
*Carriage return*, *newline* and *tabulation* are respectively replaced by `\r`, `\n` and `\t`. | ||
Other characters are replaced by the unicode notation, e.g. `NUL` is replaced by `\x00`. | ||
Full BDD spec generated by Mocha: | ||
@@ -112,3 +188,3 @@ | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression. | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression pattern. | ||
@@ -121,2 +197,15 @@ ```js | ||
escape.regExpReplacement() should escape a string so that it will be suitable as a literal string into a regular expression replacement. | ||
```js | ||
expect( string.escape.regExpReplacement( "$he love$ dollar$ $$$" ) ).to.be( "$$he love$$ dollar$$ $$$$$$" ) ; | ||
expect( | ||
'$he love$ dollar$ $$$'.replace( | ||
new RegExp( string.escape.regExp( '$' ) , 'g' ) , | ||
string.escape.regExpReplacement( '$1' ) | ||
) | ||
).to.be( "$1he love$1 dollar$1 $1$1$1" ) ; | ||
``` | ||
escape.html() should escape a string so that it will be suitable as HTML content. | ||
@@ -129,3 +218,3 @@ | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute. | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute's value. | ||
@@ -132,0 +221,0 @@ ```js |
@@ -115,3 +115,3 @@ /* | ||
it( "escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression" , function() { | ||
it( "escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression pattern" , function() { | ||
//console.log( 'String in RegExp:' , string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) ; | ||
@@ -122,2 +122,13 @@ expect( string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) | ||
it( "escape.regExpReplacement() should escape a string so that it will be suitable as a literal string into a regular expression replacement" , function() { | ||
expect( string.escape.regExpReplacement( "$he love$ dollar$ $$$" ) ).to.be( "$$he love$$ dollar$$ $$$$$$" ) ; | ||
expect( | ||
'$he love$ dollar$ $$$'.replace( | ||
new RegExp( string.escape.regExp( '$' ) , 'g' ) , | ||
string.escape.regExpReplacement( '$1' ) | ||
) | ||
).to.be( "$1he love$1 dollar$1 $1$1$1" ) ; | ||
} ) ; | ||
it( "escape.html() should escape a string so that it will be suitable as HTML content" , function() { | ||
@@ -128,3 +139,3 @@ //console.log( string.escape.html( "<This> isn't \"R&D\"" ) ) ; | ||
it( "escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute" , function() { | ||
it( "escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute's value" , function() { | ||
//console.log( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ) ; | ||
@@ -131,0 +142,0 @@ expect( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51793
614
283