string-kit
Advanced tools
Comparing version 0.0.5 to 0.0.6
149
bdd-spec.md
@@ -1,49 +0,5 @@ | ||
inspect: <Object> <object> { | ||
a : "A" <string> | ||
b : 2 <number> | ||
sub : <Object> <object> { | ||
u : <undefined> | ||
n : <null> | ||
t : <true> | ||
f : <false> | ||
circular : <Object> <object> [circular] | ||
} | ||
empty : <Object> <object> {} | ||
list : <Array> <object> { | ||
[0] : "one" <string> | ||
[1] : "two" <string> | ||
[2] : "three" <string> | ||
length< (-conf -enum)> : 3 <number> | ||
} | ||
emptyList : <Array> <object> { | ||
length< (-conf -enum)> : 0 <number> | ||
} | ||
} | ||
inspect: [35mObject[39m [3m[90mobject[39m[23m { | ||
[32ma[39m : "[34mA[39m" [3m[90mstring[39m[23m | ||
[32mb[39m : [36m2[39m [3m[90mnumber[39m[23m | ||
[32msub[39m : [35mObject[39m [3m[90mobject[39m[23m { | ||
[32mu[39m : [3m[90mundefined[39m[23m | ||
[32mn[39m : [3m[90mnull[39m[23m | ||
[32mt[39m : [3m[90mtrue[39m[23m | ||
[32mf[39m : [3m[90mfalse[39m[23m | ||
[32mcircular[39m : [35mObject[39m [3m[90mobject[39m[23m [circular] | ||
} | ||
[32mempty[39m : [35mObject[39m [3m[90mobject[39m[23m {} | ||
[32mlist[39m : [35mArray[39m [3m[90mobject[39m[23m { | ||
[[34m0[39m] : "[34mone[39m" [3m[90mstring[39m[23m | ||
[[34m1[39m] : "[34mtwo[39m" [3m[90mstring[39m[23m | ||
[[34m2[39m] : "[34mthree[39m" [3m[90mstring[39m[23m | ||
[32mlength[39m[3m[90m (-conf -enum)[39m[23m : [36m3[39m [3m[90mnumber[39m[23m | ||
} | ||
[32memptyList[39m : [35mArray[39m [3m[90mobject[39m[23m { | ||
[32mlength[39m[3m[90m (-conf -enum)[39m[23m : [36m0[39m [3m[90mnumber[39m[23m | ||
} | ||
} | ||
# TOC | ||
- [format()](#format) | ||
- [Escape collection](#escape-collection) | ||
- [inspect()](#inspect) | ||
- [Escape](#escape) | ||
<a name=""></a> | ||
@@ -109,12 +65,105 @@ | ||
<a name="escape-collection"></a> | ||
# Escape collection | ||
escape.control() should escape control characters. | ||
```js | ||
expect( string.escape.control( 'Hello\n\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\n\\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\n\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\\n\\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Nasty\x00chars\x1bhere\x7f!' ) ).to.be( 'Nasty\\x00chars\\x1bhere\\x7f!' ) ; | ||
``` | ||
escape.shellArg() should escape a string so that it will be suitable as a shell command's argument. | ||
```js | ||
//console.log( 'Shell arg:' , string.escape.shellArg( "Here's my shell's argument" ) ) ; | ||
expect( string.escape.shellArg( "Here's my shell's argument" ) ).to.be( "'Here'\\''s my shell'\\''s argument'" ) ; | ||
``` | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression. | ||
```js | ||
//console.log( 'String in RegExp:' , string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) ; | ||
expect( string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) | ||
.to.be( "\\(This\\) \\{is\\} \\[my\\] \\^\\$tring\\^\\.\\.\\. \\+doesn't\\+ \\*it\\*\\? \\|yes\\| \\\\no\\\\ \\/maybe\\/" ) ; | ||
``` | ||
escape.html() should escape a string so that it will be suitable as HTML content. | ||
```js | ||
//console.log( string.escape.html( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.html( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't \"R&D\"" ) ; | ||
``` | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute. | ||
```js | ||
//console.log( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
``` | ||
escape.htmlSpecialChars() should escape all HTML special characters. | ||
```js | ||
//console.log( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
``` | ||
<a name="inspect"></a> | ||
# inspect() | ||
should. | ||
should inspect a variable with default options accordingly. | ||
```js | ||
console.log( 'inspect: ' , inspect( { proto: true, depth: 3 } , object ) ) ; | ||
console.log( 'inspect: ' , inspect( { style: 'color', proto: true, depth: 3 } , object ) ) ; | ||
var MyClass = function MyClass() { | ||
this.variable = 1 ; | ||
} ; | ||
MyClass.prototype.report = function report() { console.log( 'Variable value:' , this.variable ) ; } ; | ||
MyClass.staticFunc = function staticFunc() { console.log( 'Static function.' ) ; } ; | ||
var sparseArray = [] ; | ||
sparseArray[ 3 ] = 'three' ; | ||
sparseArray[ 10 ] = 'ten' ; | ||
sparseArray[ 20 ] = 'twenty' ; | ||
sparseArray.customProperty = 'customProperty' ; | ||
var object = { | ||
a: 'A' , | ||
b: 2 , | ||
str: 'Woot\nWoot\rWoot\tWoot' , | ||
sub: { | ||
u: undefined , | ||
n: null , | ||
t: true , | ||
f: false | ||
} , | ||
emptyString: '' , | ||
emptyObject: {} , | ||
list: [ 'one','two','three' ] , | ||
emptyList: [] , | ||
sparseArray: sparseArray , | ||
hello: function hello() { console.log( 'Hello!' ) ; } , | ||
anonymous: function() { console.log( 'anonymous...' ) ; } , | ||
class: MyClass , | ||
instance: new MyClass() , | ||
buf: new Buffer( 'This is a buffer!' ) | ||
} ; | ||
object.sub.circular = object ; | ||
Object.defineProperties( object , { | ||
c: { value: '3' } , | ||
d: { | ||
get: function() { throw new Error( 'Should not be called by the test' ) ; } , | ||
set: function( value ) {} | ||
} | ||
} ) ; | ||
//console.log( '>>>>>' , string.escape.control( string.inspect( object ) ) ) ; | ||
//console.log( string.inspect( { style: 'color' } , object ) ) ; | ||
expect( string.inspect( object ) ).to.be( '<Object> <object> {\n a: "A" <string>(1)\n b: 2 <number>\n str: "Woot\\nWoot\\rWoot\\tWoot" <string>(19)\n sub: <Object> <object> {\n u: undefined\n n: null\n t: true\n f: false\n circular: <Object> <object> [circular]\n }\n emptyString: "" <string>(0)\n emptyObject: <Object> <object> {}\n list: <Array>(3) <object> {\n [0] "one" <string>(3)\n [1] "two" <string>(3)\n [2] "three" <string>(5)\n length: 3 <number> <-conf -enum>\n }\n emptyList: <Array>(0) <object> {\n length: 0 <number> <-conf -enum>\n }\n sparseArray: <Array>(21) <object> {\n [3] "three" <string>(5)\n [10] "ten" <string>(3)\n [20] "twenty" <string>(6)\n length: 21 <number> <-conf -enum>\n customProperty: "customProperty" <string>(14)\n }\n hello: <Function> hello(0) <function>\n anonymous: <Function> (anonymous)(0) <function>\n class: <Function> MyClass(0) <function>\n instance: <MyClass> <object> {\n variable: 1 <number>\n }\n buf: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 21> <Buffer>(17)\n c: "3" <string>(1) <-conf -enum -w>\n d: <getter/setter> {\n get: <Function> (anonymous)(0) <function>\n set: <Function> (anonymous)(1) <function>\n }\n}\n' ) ; | ||
``` | ||
<a name="escape"></a> | ||
# Escape |
@@ -219,2 +219,6 @@ /* | ||
/* Escape collection */ | ||
string.escape = {} ; | ||
@@ -228,2 +232,4 @@ | ||
string.escape.shellArg = function escapeShellArg( str ) { | ||
@@ -235,70 +241,61 @@ return '\'' + str.replace( /\'/g , "'\\''" ) + '\'' ; | ||
var escapeControlMap = { '\r': '\\r', '\n': '\\n', '\t': '\\t', '\x7f': '\\x7f' } ; | ||
// Escape \r \n \t so they become readable again | ||
string.escape.control = function escapeControl( str ) { | ||
return str.replace( /\r|\n|\t|[\x00-\x1f\x7f]/g , function( match ) { | ||
if ( escapeControlMap[ match ] !== undefined ) { return escapeControlMap[ match ] ; } | ||
var hex = match.charCodeAt( 0 ).toString( 16 ) ; | ||
if ( hex.length % 2 ) { hex = '0' + hex ; } | ||
return '\\x' + hex ; | ||
} ) ; | ||
} ; | ||
var escapeHtmlMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' } ; | ||
/* | ||
TODO: | ||
*/ | ||
// Only escape & < > so this is suited for content outside tags | ||
string.escape.html = function escapeHtml( str ) { | ||
return str.replace( /[&<>]/g , function( match ) { return escapeHtmlMap[ match ] ; } ) ; | ||
} ; | ||
/* | ||
options: | ||
* depth: depth limit, default: 3 | ||
* proto: display prototype of function | ||
*/ | ||
// Escape & < > " so this is suited for content inside a double-quoted attribute | ||
string.escape.htmlAttr = function escapeHtmlAttr( str ) { | ||
return str.replace( /[&<>"]/g , function( match ) { return escapeHtmlMap[ match ] ; } ) ; | ||
} ; | ||
var inspectStyleNoop = function( str ) { return str ; } ; | ||
// Escape all html special characters & < > " ' | ||
string.escape.htmlSpecialChars = function escapeHtmlSpecialChars( str ) { | ||
return str.replace( /[&<>"']/g , function( match ) { return escapeHtmlMap[ match ] ; } ) ; | ||
} ; | ||
var inspectStyle = {} ; | ||
inspectStyle.none = { | ||
tab: ' ' , | ||
nl: '\n' , | ||
type: function( str ) { return '<' + str + '>' ; } , | ||
constructor: function( str ) { return '<' + str + '>' ; } , | ||
key: inspectStyleNoop , | ||
index: inspectStyleNoop , | ||
number: inspectStyleNoop , | ||
string: function( str ) { | ||
str = str.replace( "\r" , "\\r" ) ; | ||
return str ; | ||
} | ||
} ; | ||
inspectStyle.color = tree.extend( null , {} , inspectStyle.none , { | ||
type: term.str.italic.brightBlack , | ||
constructor: term.str.magenta , | ||
key: term.str.green , | ||
index: term.str.blue , | ||
number: term.str.cyan , | ||
string: function( str ) { | ||
str = str.replace( "\r" , "\\r" ) ; | ||
return term.str.blue( str ) ; | ||
} | ||
} ) ; | ||
inspectStyle.html = tree.extend( null , {} , inspectStyle.none , { | ||
tab: ' ' , | ||
nl: '<br />' , | ||
type: function( str ) { return '<i style="color:gray">' + str + '</i>' ; } , | ||
constructor: function( str ) { return '<span style="color:magenta">' + str + '</span>' ; } , | ||
key: function( str ) { return '<span style="color:green">' + str + '</span>' ; } , | ||
index: function( str ) { return '<span style="color:blue">' + str + '</span>' ; } , | ||
number: function( str ) { return '<span style="color:cyan">' + str + '</span>' ; } , | ||
string: function( str ) { | ||
// escape( 'html_content' , variable ) + // need to backport this later | ||
return '<span style="color:blue">' + str + '</span>' ; | ||
} | ||
} ) ; | ||
/* | ||
Inspect a variable, return a string ready to be displayed with console.log(), or even as an HTML output. | ||
Options: | ||
* style: | ||
* 'none': (default) normal output suitable for console.log() or writing in a file | ||
* 'color': colorful output suitable for terminal | ||
* 'html': html output | ||
* depth: depth limit, default: 3 | ||
* nofunc: do not display functions | ||
* funcDetails: display function's details | ||
* proto: display object's prototype | ||
* useInspect? use .inspect() methode when available on an object | ||
*/ | ||
function inspect( runtime , options , variable ) | ||
{ | ||
var i , funcName , propertyList , constructor , keyIsProperty , | ||
type , pre , nextIndent , nextIndent2 , isArray , | ||
var i , funcName , length , propertyList , constructor , keyIsProperty , | ||
type , pre , nextIndent , nextIndent2 , isArray , isFunc , | ||
str = '' , key = '' , descriptorStr = '' , descriptor ; | ||
// Things applied only for the first call, not for recursive call | ||
if ( ! runtime ) | ||
@@ -317,8 +314,10 @@ { | ||
type = typeof variable ; | ||
// Prepare things (indentation, key, descriptor, ... ) | ||
type = typeof variable ; | ||
nextIndent = runtime.indent + options.style.tab ; | ||
nextIndent2 = nextIndent + options.style.tab ; | ||
if ( type === 'function' && options.nofunc ) { return '' ; } | ||
@@ -334,6 +333,8 @@ if ( runtime.key !== undefined ) | ||
if ( runtime.descriptor.get || runtime.descriptor.set ) { descriptorStr.push( 'getter/setter' ) ; } | ||
else if ( ! runtime.descriptor.writable ) { descriptorStr.push( '-w' ) ; } | ||
// Already displayed by runtime.forceType | ||
//if ( runtime.descriptor.get || runtime.descriptor.set ) { descriptorStr.push( 'getter/setter' ) ; } else | ||
if ( ! runtime.descriptor.writable ) { descriptorStr.push( '-w' ) ; } | ||
if ( descriptorStr.length ) { descriptorStr = ' (' + descriptorStr.join( ' ' ) + ')' ; } | ||
//if ( descriptorStr.length ) { descriptorStr = '(' + descriptorStr.join( ' ' ) + ')' ; } | ||
if ( descriptorStr.length ) { descriptorStr = descriptorStr.join( ' ' ) ; } | ||
else { descriptorStr = '' ; } | ||
@@ -343,90 +344,92 @@ } | ||
key = runtime.keyIsProperty ? | ||
options.style.key( runtime.key ) + ( descriptorStr ? options.style.type( descriptorStr ) : '' ) + ' : ' : | ||
'[' + options.style.index( runtime.key ) + '] : ' ; | ||
options.style.key( runtime.key ) + ': ' : | ||
'[' + options.style.index( runtime.key ) + '] ' ; | ||
if ( descriptorStr ) { descriptorStr = ' ' + options.style.type( descriptorStr ) ; } | ||
} | ||
pre = runtime.indent + key ; | ||
// Describe the current variable | ||
if ( variable === undefined ) | ||
{ | ||
str += pre + options.style.type( "undefined" ) + options.style.nl ; | ||
str += pre + options.style.constant( 'undefined' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( variable === null ) | ||
{ | ||
str += pre + options.style.type( "null" ) + options.style.nl ; | ||
str += pre + options.style.constant( 'null' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( variable === false ) | ||
{ | ||
str += pre + options.style.type( "false" ) + options.style.nl ; | ||
str += pre + options.style.constant( 'false' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( variable === true ) | ||
{ | ||
str += pre + options.style.type( "true" ) + options.style.nl ; | ||
str += pre + options.style.constant( 'true' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( type === 'number' ) | ||
{ | ||
str += pre + options.style.number( variable.toString() ) + ' ' + options.style.type( "number" ) + options.style.nl ; | ||
str += pre + options.style.number( variable.toString() ) + ' ' + options.style.type( 'number' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( type === 'string' ) | ||
{ | ||
str += pre + '"' + options.style.string( variable ) + '" ' + options.style.type( "string" ) + options.style.nl ; | ||
str += pre + '"' + options.style.string( string.escape.control( variable ) ) + '" ' + | ||
options.style.type( 'string' ) + options.style.length( '(' + variable.length + ')' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( type === 'function' ) | ||
else if ( Buffer.isBuffer( variable ) ) | ||
{ | ||
funcName = variable.name ? variable.name : '<anonymous>' ; | ||
str += pre + options.style.inspect( variable.inspect() ) + ' ' + | ||
options.style.type( 'Buffer' ) + options.style.length( '(' + variable.length + ')' ) + descriptorStr + options.style.nl ; | ||
} | ||
else if ( type === 'object' || type === 'function' ) | ||
{ | ||
funcName = length = '' ; | ||
str += pre + options.style.constructor( funcName ) + options.style.type( " function (" + variable.length + ")" ) ; | ||
isFunc = false ; | ||
if ( type === 'function' ) | ||
{ | ||
isFunc = true ; | ||
funcName = ' ' + options.style.funcName( ( variable.name ? variable.name : '(anonymous)' ) ) ; | ||
length = options.style.length( '(' + variable.length + ')' ) ; | ||
} | ||
if ( runtime.depth < options.depth && options.proto ) | ||
isArray = false ; | ||
if ( Array.isArray( variable ) ) | ||
{ | ||
propertyList = Object.getOwnPropertyNames( variable.prototype ) ; | ||
str += options.style.nl + runtime.indent + "--> prototype {" + options.style.nl ; | ||
for ( i = 0 ; i < propertyList.length ; i ++ ) | ||
{ | ||
str += inspect( { | ||
depth: runtime.depth + 1 , | ||
ancestors: runtime.ancestors.concat( variable ) , | ||
indent: nextIndent , | ||
key: propertyList[ i ] , | ||
keyIsProperty: true | ||
} , | ||
options , | ||
variable.prototype[ propertyList[ i ] ] | ||
) ; | ||
} | ||
str += runtime.indent + "}" ; | ||
isArray = true ; | ||
length = options.style.length( '(' + variable.length + ')' ) ; | ||
} | ||
str += options.style.nl ; | ||
} | ||
else if ( type === 'object' ) | ||
{ | ||
isArray = Array.isArray( variable ) ; | ||
constructor = variable.constructor.name ? variable.constructor.name : '<constructorless>' ; | ||
if ( ! variable.constructor ) { constructor = '(no constructor)' ; } | ||
else if ( ! variable.constructor.name ) { constructor = '(anonymous)' ; } | ||
else { constructor = variable.constructor.name ; } | ||
str += pre + options.style.constructor( constructor ) + ' ' + options.style.type( "object" ) ; | ||
constructor = options.style.constructorName( constructor ) ; | ||
if ( runtime.forceType ) { str += pre + options.style.type( runtime.forceType ) ; } | ||
else { str += pre + constructor + funcName + length + ' ' + options.style.type( type ) + descriptorStr ; } | ||
propertyList = Object.getOwnPropertyNames( variable ) ; | ||
if ( ! propertyList.length ) | ||
if ( isFunc && ! options.funcDetails ) | ||
{ | ||
str += " {}" + options.style.nl ; | ||
str += options.style.nl ; | ||
} | ||
else if ( ! propertyList.length ) | ||
{ | ||
str += ' {}' + options.style.nl ; | ||
} | ||
else if ( runtime.depth >= options.depth ) | ||
{ | ||
str += " [depth limit]" + options.style.nl ; | ||
str += ' ' + options.style.limit( '[depth limit]' ) + options.style.nl ; | ||
} | ||
else if ( runtime.ancestors.indexOf( variable ) !== -1 ) | ||
{ | ||
str += " [circular]" + options.style.nl ; | ||
str += ' ' + options.style.limit( '[circular]' ) + options.style.nl ; | ||
} | ||
else | ||
{ | ||
//str += options.style.nl + runtime.indent + "{" + options.style.nl ; | ||
str += " {" + options.style.nl ; | ||
str += ' {' + options.style.nl ; | ||
@@ -437,3 +440,3 @@ for ( i = 0 ; i < propertyList.length ; i ++ ) | ||
keyIsProperty = descriptor.enumerable && isArray ? false : true ; | ||
keyIsProperty = ! isArray || ! descriptor.enumerable || isNaN( propertyList[ i ] ) ; | ||
@@ -448,3 +451,4 @@ if ( descriptor.get || descriptor.set ) | ||
keyIsProperty: keyIsProperty , | ||
descriptor: descriptor | ||
descriptor: descriptor , | ||
forceType: 'getter/setter' | ||
} , | ||
@@ -471,20 +475,93 @@ options , | ||
str += runtime.indent + "}" + options.style.nl ; | ||
if ( options.proto ) | ||
{ | ||
str += inspect( { | ||
depth: runtime.depth + 1 , | ||
ancestors: runtime.ancestors.concat( variable ) , | ||
indent: nextIndent , | ||
key: '__proto__' , | ||
keyIsProperty: true | ||
} , | ||
options , | ||
variable.__proto__ // jshint ignore:line | ||
) ; | ||
} | ||
str += runtime.indent + '}' + options.style.nl ; | ||
} | ||
} | ||
// Finalizing | ||
if ( runtime.depth === 0 ) | ||
{ | ||
if ( options.style === 'html' ) { str = string.escape.html( str ) ; } | ||
} | ||
return str ; | ||
} | ||
string.inspect = inspect.bind( undefined , null ) ; | ||
string.inspect = inspect.bind( undefined , null ) ; | ||
// Inspect's styles | ||
var inspectStyle = {} ; | ||
var inspectStyleNoop = function( str ) { return str ; } ; | ||
inspectStyle.none = { | ||
tab: ' ' , | ||
nl: '\n' , | ||
limit: inspectStyleNoop , | ||
type: function( str ) { return '<' + str + '>' ; } , | ||
constant: inspectStyleNoop , | ||
funcName: inspectStyleNoop , | ||
constructorName: function( str ) { return '<' + str + '>' ; } , | ||
length: inspectStyleNoop , | ||
key: inspectStyleNoop , | ||
index: inspectStyleNoop , | ||
number: inspectStyleNoop , | ||
inspect: inspectStyleNoop , | ||
string: inspectStyleNoop | ||
} ; | ||
inspectStyle.color = tree.extend( null , {} , inspectStyle.none , { | ||
limit: term.str.bold.brightRed , | ||
type: term.str.italic.brightBlack , | ||
constant: term.str.cyan , | ||
funcName: term.str.italic.magenta , | ||
constructorName: term.str.magenta , | ||
length: term.str.italic.brightBlack , | ||
key: term.str.green , | ||
index: term.str.blue , | ||
number: term.str.cyan , | ||
inspect: term.str.cyan , | ||
string: term.str.blue | ||
} ) ; | ||
inspectStyle.html = tree.extend( null , {} , inspectStyle.none , { | ||
tab: ' ' , | ||
nl: '<br />' , | ||
limit: function( str ) { return '<span style="color:red">' + str + '</span>' ; } , | ||
type: function( str ) { return '<i style="color:gray">' + str + '</i>' ; } , | ||
constant: function( str ) { return '<span style="color:cyan">' + str + '</span>' ; } , | ||
funcName: function( str ) { return '<i style="color:magenta">' + str + '</i>' ; } , | ||
constructorName: function( str ) { return '<span style="color:magenta">' + str + '</span>' ; } , | ||
length: function( str ) { return '<i style="color:gray">' + str + '</i>' ; } , | ||
key: function( str ) { return '<span style="color:green">' + str + '</span>' ; } , | ||
index: function( str ) { return '<span style="color:blue">' + str + '</span>' ; } , | ||
number: function( str ) { return '<span style="color:cyan">' + str + '</span>' ; } , | ||
inspect: function( str ) { return '<span style="color:cyan">' + str + '</span>' ; } , | ||
string: function( str ) { return '<span style="color:blue">' + str + '</span>' ; } | ||
} ) ; | ||
/* | ||
@@ -491,0 +568,0 @@ |
{ | ||
"name": "string-kit", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A string manipulation toolbox.", | ||
@@ -5,0 +5,0 @@ "main": "lib/string.js", |
149
README.md
@@ -26,50 +26,6 @@ | ||
inspect: <Object> <object> { | ||
a : "A" <string> | ||
b : 2 <number> | ||
sub : <Object> <object> { | ||
u : <undefined> | ||
n : <null> | ||
t : <true> | ||
f : <false> | ||
circular : <Object> <object> [circular] | ||
} | ||
empty : <Object> <object> {} | ||
list : <Array> <object> { | ||
[0] : "one" <string> | ||
[1] : "two" <string> | ||
[2] : "three" <string> | ||
length< (-conf -enum)> : 3 <number> | ||
} | ||
emptyList : <Array> <object> { | ||
length< (-conf -enum)> : 0 <number> | ||
} | ||
} | ||
inspect: [35mObject[39m [3m[90mobject[39m[23m { | ||
[32ma[39m : "[34mA[39m" [3m[90mstring[39m[23m | ||
[32mb[39m : [36m2[39m [3m[90mnumber[39m[23m | ||
[32msub[39m : [35mObject[39m [3m[90mobject[39m[23m { | ||
[32mu[39m : [3m[90mundefined[39m[23m | ||
[32mn[39m : [3m[90mnull[39m[23m | ||
[32mt[39m : [3m[90mtrue[39m[23m | ||
[32mf[39m : [3m[90mfalse[39m[23m | ||
[32mcircular[39m : [35mObject[39m [3m[90mobject[39m[23m [circular] | ||
} | ||
[32mempty[39m : [35mObject[39m [3m[90mobject[39m[23m {} | ||
[32mlist[39m : [35mArray[39m [3m[90mobject[39m[23m { | ||
[[34m0[39m] : "[34mone[39m" [3m[90mstring[39m[23m | ||
[[34m1[39m] : "[34mtwo[39m" [3m[90mstring[39m[23m | ||
[[34m2[39m] : "[34mthree[39m" [3m[90mstring[39m[23m | ||
[32mlength[39m[3m[90m (-conf -enum)[39m[23m : [36m3[39m [3m[90mnumber[39m[23m | ||
} | ||
[32memptyList[39m : [35mArray[39m [3m[90mobject[39m[23m { | ||
[32mlength[39m[3m[90m (-conf -enum)[39m[23m : [36m0[39m [3m[90mnumber[39m[23m | ||
} | ||
} | ||
# TOC | ||
- [format()](#format) | ||
- [Escape collection](#escape-collection) | ||
- [inspect()](#inspect) | ||
- [Escape](#escape) | ||
<a name=""></a> | ||
@@ -135,12 +91,105 @@ | ||
<a name="escape-collection"></a> | ||
# Escape collection | ||
escape.control() should escape control characters. | ||
```js | ||
expect( string.escape.control( 'Hello\n\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\n\\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\n\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\\n\\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Nasty\x00chars\x1bhere\x7f!' ) ).to.be( 'Nasty\\x00chars\\x1bhere\\x7f!' ) ; | ||
``` | ||
escape.shellArg() should escape a string so that it will be suitable as a shell command's argument. | ||
```js | ||
//console.log( 'Shell arg:' , string.escape.shellArg( "Here's my shell's argument" ) ) ; | ||
expect( string.escape.shellArg( "Here's my shell's argument" ) ).to.be( "'Here'\\''s my shell'\\''s argument'" ) ; | ||
``` | ||
escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression. | ||
```js | ||
//console.log( 'String in RegExp:' , string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) ; | ||
expect( string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) | ||
.to.be( "\\(This\\) \\{is\\} \\[my\\] \\^\\$tring\\^\\.\\.\\. \\+doesn't\\+ \\*it\\*\\? \\|yes\\| \\\\no\\\\ \\/maybe\\/" ) ; | ||
``` | ||
escape.html() should escape a string so that it will be suitable as HTML content. | ||
```js | ||
//console.log( string.escape.html( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.html( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't \"R&D\"" ) ; | ||
``` | ||
escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute. | ||
```js | ||
//console.log( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
``` | ||
escape.htmlSpecialChars() should escape all HTML special characters. | ||
```js | ||
//console.log( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
``` | ||
<a name="inspect"></a> | ||
# inspect() | ||
should. | ||
should inspect a variable with default options accordingly. | ||
```js | ||
console.log( 'inspect: ' , inspect( { proto: true, depth: 3 } , object ) ) ; | ||
console.log( 'inspect: ' , inspect( { style: 'color', proto: true, depth: 3 } , object ) ) ; | ||
var MyClass = function MyClass() { | ||
this.variable = 1 ; | ||
} ; | ||
MyClass.prototype.report = function report() { console.log( 'Variable value:' , this.variable ) ; } ; | ||
MyClass.staticFunc = function staticFunc() { console.log( 'Static function.' ) ; } ; | ||
var sparseArray = [] ; | ||
sparseArray[ 3 ] = 'three' ; | ||
sparseArray[ 10 ] = 'ten' ; | ||
sparseArray[ 20 ] = 'twenty' ; | ||
sparseArray.customProperty = 'customProperty' ; | ||
var object = { | ||
a: 'A' , | ||
b: 2 , | ||
str: 'Woot\nWoot\rWoot\tWoot' , | ||
sub: { | ||
u: undefined , | ||
n: null , | ||
t: true , | ||
f: false | ||
} , | ||
emptyString: '' , | ||
emptyObject: {} , | ||
list: [ 'one','two','three' ] , | ||
emptyList: [] , | ||
sparseArray: sparseArray , | ||
hello: function hello() { console.log( 'Hello!' ) ; } , | ||
anonymous: function() { console.log( 'anonymous...' ) ; } , | ||
class: MyClass , | ||
instance: new MyClass() , | ||
buf: new Buffer( 'This is a buffer!' ) | ||
} ; | ||
object.sub.circular = object ; | ||
Object.defineProperties( object , { | ||
c: { value: '3' } , | ||
d: { | ||
get: function() { throw new Error( 'Should not be called by the test' ) ; } , | ||
set: function( value ) {} | ||
} | ||
} ) ; | ||
//console.log( '>>>>>' , string.escape.control( string.inspect( object ) ) ) ; | ||
//console.log( string.inspect( { style: 'color' } , object ) ) ; | ||
expect( string.inspect( object ) ).to.be( '<Object> <object> {\n a: "A" <string>(1)\n b: 2 <number>\n str: "Woot\\nWoot\\rWoot\\tWoot" <string>(19)\n sub: <Object> <object> {\n u: undefined\n n: null\n t: true\n f: false\n circular: <Object> <object> [circular]\n }\n emptyString: "" <string>(0)\n emptyObject: <Object> <object> {}\n list: <Array>(3) <object> {\n [0] "one" <string>(3)\n [1] "two" <string>(3)\n [2] "three" <string>(5)\n length: 3 <number> <-conf -enum>\n }\n emptyList: <Array>(0) <object> {\n length: 0 <number> <-conf -enum>\n }\n sparseArray: <Array>(21) <object> {\n [3] "three" <string>(5)\n [10] "ten" <string>(3)\n [20] "twenty" <string>(6)\n length: 21 <number> <-conf -enum>\n customProperty: "customProperty" <string>(14)\n }\n hello: <Function> hello(0) <function>\n anonymous: <Function> (anonymous)(0) <function>\n class: <Function> MyClass(0) <function>\n instance: <MyClass> <object> {\n variable: 1 <number>\n }\n buf: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 21> <Buffer>(17)\n c: "3" <string>(1) <-conf -enum -w>\n d: <getter/setter> {\n get: <Function> (anonymous)(0) <function>\n set: <Function> (anonymous)(1) <function>\n }\n}\n' ) ; | ||
``` | ||
<a name="escape"></a> | ||
# Escape |
@@ -99,46 +99,38 @@ /* | ||
describe( "inspect()" , function() { | ||
describe( "Escape collection" , function() { | ||
var inspect = string.inspect ; | ||
it( "escape.control() should escape control characters" , function() { | ||
expect( string.escape.control( 'Hello\n\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\n\\t... world!' ) ).to.be( 'Hello\\n\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\n\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Hello\\\\n\\\\t... world!' ) ).to.be( 'Hello\\\\n\\\\t... world!' ) ; | ||
expect( string.escape.control( 'Nasty\x00chars\x1bhere\x7f!' ) ).to.be( 'Nasty\\x00chars\\x1bhere\\x7f!' ) ; | ||
} ) ; | ||
var object = { | ||
a: 'A' , | ||
b: 2 , | ||
sub: { | ||
u: undefined , | ||
n: null , | ||
t: true , | ||
f: false | ||
} , | ||
empty: {} , | ||
list: [ 'one','two','three' ] , | ||
emptyList: [] | ||
} ; | ||
it( "escape.shellArg() should escape a string so that it will be suitable as a shell command's argument" , function() { | ||
//console.log( 'Shell arg:' , string.escape.shellArg( "Here's my shell's argument" ) ) ; | ||
expect( string.escape.shellArg( "Here's my shell's argument" ) ).to.be( "'Here'\\''s my shell'\\''s argument'" ) ; | ||
} ) ; | ||
object.sub.circular = object ; | ||
it( "escape.regExp() should escape a string so that it will be suitable as a literal string into a regular expression" , function() { | ||
//console.log( 'String in RegExp:' , string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) ; | ||
expect( string.escape.regExp( "(This) {is} [my] ^$tring^... +doesn't+ *it*? |yes| \\no\\ /maybe/" ) ) | ||
.to.be( "\\(This\\) \\{is\\} \\[my\\] \\^\\$tring\\^\\.\\.\\. \\+doesn't\\+ \\*it\\*\\? \\|yes\\| \\\\no\\\\ \\/maybe\\/" ) ; | ||
} ) ; | ||
/* | ||
Object.defineProperties( object , { | ||
c: { value: '3' } , | ||
d: { | ||
get: function() { return 'Dee' ; } , | ||
set: function( value ) {} | ||
} | ||
it( "escape.html() should escape a string so that it will be suitable as HTML content" , function() { | ||
//console.log( string.escape.html( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.html( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't \"R&D\"" ) ; | ||
} ) ; | ||
//*/ | ||
/* | ||
it( "should" , function() { | ||
console.log( 'inspect: ' , inspect( true ) ) ; | ||
console.log( 'inspect: ' , inspect( { color: true } , true ) ) ; | ||
//console.log( 'inspect: ' , inspect( { html: true } , true ) ) ; | ||
it( "escape.htmlAttr() should escape a string so that it will be suitable as an HTML tag attribute" , function() { | ||
//console.log( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlAttr( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
} ) ; | ||
//*/ | ||
//* | ||
it( "should" , function() { | ||
console.log( 'inspect: ' , inspect( { proto: true, depth: 3 } , object ) ) ; | ||
console.log( 'inspect: ' , inspect( { style: 'color', proto: true, depth: 3 } , object ) ) ; | ||
it( "escape.htmlSpecialChars() should escape all HTML special characters" , function() { | ||
//console.log( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ) ; | ||
expect( string.escape.htmlSpecialChars( "<This> isn't \"R&D\"" ) ).to.be( "<This> isn't "R&D"" ) ; | ||
} ) ; | ||
//*/ | ||
} ) ; | ||
@@ -148,5 +140,55 @@ | ||
describe( "Escape" , function() { | ||
it( "escape.regExp" ) ; | ||
it( "escape.shellArg" ) ; | ||
describe( "inspect()" , function() { | ||
it( "should inspect a variable with default options accordingly" , function() { | ||
var MyClass = function MyClass() { | ||
this.variable = 1 ; | ||
} ; | ||
MyClass.prototype.report = function report() { console.log( 'Variable value:' , this.variable ) ; } ; | ||
MyClass.staticFunc = function staticFunc() { console.log( 'Static function.' ) ; } ; | ||
var sparseArray = [] ; | ||
sparseArray[ 3 ] = 'three' ; | ||
sparseArray[ 10 ] = 'ten' ; | ||
sparseArray[ 20 ] = 'twenty' ; | ||
sparseArray.customProperty = 'customProperty' ; | ||
var object = { | ||
a: 'A' , | ||
b: 2 , | ||
str: 'Woot\nWoot\rWoot\tWoot' , | ||
sub: { | ||
u: undefined , | ||
n: null , | ||
t: true , | ||
f: false | ||
} , | ||
emptyString: '' , | ||
emptyObject: {} , | ||
list: [ 'one','two','three' ] , | ||
emptyList: [] , | ||
sparseArray: sparseArray , | ||
hello: function hello() { console.log( 'Hello!' ) ; } , | ||
anonymous: function() { console.log( 'anonymous...' ) ; } , | ||
class: MyClass , | ||
instance: new MyClass() , | ||
buf: new Buffer( 'This is a buffer!' ) | ||
} ; | ||
object.sub.circular = object ; | ||
Object.defineProperties( object , { | ||
c: { value: '3' } , | ||
d: { | ||
get: function() { throw new Error( 'Should not be called by the test' ) ; } , | ||
set: function( value ) {} | ||
} | ||
} ) ; | ||
//console.log( '>>>>>' , string.escape.control( string.inspect( object ) ) ) ; | ||
//console.log( string.inspect( { style: 'color' } , object ) ) ; | ||
expect( string.inspect( object ) ).to.be( '<Object> <object> {\n a: "A" <string>(1)\n b: 2 <number>\n str: "Woot\\nWoot\\rWoot\\tWoot" <string>(19)\n sub: <Object> <object> {\n u: undefined\n n: null\n t: true\n f: false\n circular: <Object> <object> [circular]\n }\n emptyString: "" <string>(0)\n emptyObject: <Object> <object> {}\n list: <Array>(3) <object> {\n [0] "one" <string>(3)\n [1] "two" <string>(3)\n [2] "three" <string>(5)\n length: 3 <number> <-conf -enum>\n }\n emptyList: <Array>(0) <object> {\n length: 0 <number> <-conf -enum>\n }\n sparseArray: <Array>(21) <object> {\n [3] "three" <string>(5)\n [10] "ten" <string>(3)\n [20] "twenty" <string>(6)\n length: 21 <number> <-conf -enum>\n customProperty: "customProperty" <string>(14)\n }\n hello: <Function> hello(0) <function>\n anonymous: <Function> (anonymous)(0) <function>\n class: <Function> MyClass(0) <function>\n instance: <MyClass> <object> {\n variable: 1 <number>\n }\n buf: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 21> <Buffer>(17)\n c: "3" <string>(1) <-conf -enum -w>\n d: <getter/setter> {\n get: <Function> (anonymous)(0) <function>\n set: <Function> (anonymous)(1) <function>\n }\n}\n' ) ; | ||
} ) ; | ||
} ) ; | ||
@@ -157,1 +199,2 @@ | ||
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
46021
602
194
1