string-kit
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -0,3 +1,21 @@ | ||
inspect: [35mObject[39m[3m[90m object[39m[23m { | ||
[32ma[39m : "[34mA[39m" [3m[90mstring[39m[23m | ||
[32mb[39m : [34m2[39m[3m[90m number[39m[23m | ||
[32msub[39m : [35mObject[39m[3m[90m object[39m[23m { | ||
[32me[39m : "[34mee[39m" [3m[90mstring[39m[23m | ||
[32mf[39m : [34m6[39m[3m[90m number[39m[23m | ||
[32mcircular[39m : [35mObject[39m[3m[90m object[39m[23m [circular] | ||
} | ||
[32mempty[39m : [35mObject[39m[3m[90m object[39m[23m {} | ||
[32mlist[39m : [35mArray[39m[3m[90m object[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 : [34m3[39m[3m[90m number[39m[23m | ||
} | ||
} | ||
# TOC | ||
- [format()](#format) | ||
- [inspect()](#inspect) | ||
- [Escape](#escape) | ||
@@ -64,3 +82,13 @@ <a name=""></a> | ||
<a name="inspect"></a> | ||
# inspect() | ||
should. | ||
```js | ||
//console.log( 'inspect: ' , inspect( object ) ) ; | ||
console.log( 'inspect: ' , inspect( { color: true, proto: true, depth: 10 } , object ) ) ; | ||
//console.log( 'inspect: ' , inspect( { html: true } , object ) ) ; | ||
``` | ||
<a name="escape"></a> | ||
# Escape |
@@ -31,3 +31,4 @@ /* | ||
// Load modules | ||
//var util = require( 'util' ) ; | ||
var tree = require( 'tree-kit' ) ; | ||
var term = require( 'terminal-kit' ) ; | ||
@@ -233,1 +234,292 @@ | ||
/* | ||
TODO: | ||
*/ | ||
/* | ||
options: | ||
* depth: depth limit, default: 3 | ||
* proto: display prototype of function | ||
*/ | ||
var sterm = term.str ; | ||
function inspect( runtime , options , variable ) | ||
{ | ||
var i , funcName , propertyList , constructor , keyIsProperty , | ||
type , pre , nextIndent , nextIndent2 , isArray , | ||
str = '' , key = '' , descriptorStr = '' , descriptor ; | ||
// Things applied only for the first call, not for recursive call | ||
if ( ! runtime ) | ||
{ | ||
if ( arguments.length < 3 ) { variable = options ; options = {} ; } | ||
else if ( ! options || typeof options !== 'object' ) { options = {} ; } | ||
runtime = { depth: 0 , indent: '' , ancestors: [] } ; | ||
if ( options.color ) | ||
{ | ||
if ( options.colorType === undefined ) { options.colorType = term.str.italic.brightBlack ; } | ||
if ( options.colorClass === undefined ) { options.colorClass = term.str.magenta ; } | ||
if ( options.colorKey === undefined ) { options.colorKey = term.str.green ; } | ||
if ( options.colorIndex === undefined ) { options.colorIndex = term.str.blue ; } | ||
if ( options.colorNumber === undefined ) { options.colorNumber = term.str.blue ; } | ||
if ( options.colorString === undefined ) { options.colorString = term.str.blue ; } | ||
} | ||
if ( options.html ) | ||
{ | ||
if ( options.tab === undefined ) { options.tab = ' ' ; } | ||
if ( options.nl === undefined ) { options.nl = '<br />' ; } | ||
} | ||
if ( options.depth === undefined ) { options.depth = 3 ; } | ||
if ( options.tab === undefined ) { options.tab = ' ' ; } | ||
//if ( options.tab === undefined ) { options.tab = '\t' ; } | ||
if ( options.nl === undefined ) { options.nl = '\n' ; } | ||
} | ||
type = typeof variable ; | ||
nextIndent = runtime.indent + options.tab ; | ||
nextIndent2 = nextIndent + options.tab ; | ||
if ( runtime.key !== undefined ) | ||
{ | ||
if ( runtime.descriptor ) | ||
{ | ||
descriptorStr = [] ; | ||
if ( ! runtime.descriptor.configurable ) { descriptorStr.push( '-conf' ) ; } | ||
if ( ! runtime.descriptor.enumerable ) { descriptorStr.push( '-enum' ) ; } | ||
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( ' ' ) + ')' ; } | ||
else { descriptorStr = '' ; } | ||
} | ||
if ( options.html ) | ||
{ | ||
key = runtime.keyIsProperty ? | ||
'<i style="color:green;">' + runtime.key + ( descriptorStr ? '<span style="color:gray;">' + descriptorStr + '</span>' : '' ) + '</i> : ' : | ||
'[<i style="color:blue;">' + runtime.key + '</i>] : ' ; | ||
} | ||
else if ( options.color ) | ||
{ | ||
key = runtime.keyIsProperty ? | ||
options.colorKey( runtime.key ) + ( descriptorStr ? options.colorType( descriptorStr ) : '' ) + ' : ' : | ||
'[' + options.colorIndex( runtime.key ) + '] : ' ; | ||
} | ||
else | ||
{ | ||
key = runtime.keyIsProperty ? | ||
runtime.key + ( descriptorStr ? descriptorStr : '' ) + " : " : | ||
'[' + runtime.key + '] : ' ; | ||
} | ||
} | ||
pre = runtime.indent + key ; | ||
if ( variable === undefined ) | ||
{ | ||
if ( options.html ) { str += pre + '<i style="color:gray;">undefined</i>' + options.nl ; } | ||
else if ( options.color ) { str += pre + options.colorType( "undefined" ) + options.nl ; } | ||
else { str += pre + "<undefined>" + options.nl ; } | ||
} | ||
else if ( variable === null ) | ||
{ | ||
if ( options.html ) { str += pre + '<i style="color:gray;">null</i>' + options.nl ; } | ||
else if ( options.color ) { str += pre + options.colorType( "null" ) + options.nl ; } | ||
else { str += pre + "<null>" + options.nl ; } | ||
} | ||
else if ( variable === false ) | ||
{ | ||
if ( options.html ) { str += pre + '<i style="color:gray;">false</i>' + options.nl ; } | ||
else if ( options.color ) { str += pre + options.colorType( "false" ) + options.nl ; } | ||
else { str += pre + "<false>" + options.nl ; } | ||
} | ||
else if ( variable === true ) | ||
{ | ||
if ( options.html ) { str += pre + '<i style="color:gray;">true</i>' + options.nl ; } | ||
else if ( options.color ) { str += pre + options.colorType( "true" ) + options.nl ; } | ||
else { str += pre + "<true>" + options.nl ; } | ||
} | ||
else if ( type === 'number' ) | ||
{ | ||
if ( options.html ) { str += pre + '<i style="color:blue;">' + variable.toString() + '</i> <i style="color:gray;">number</i>' + options.nl ; } | ||
else if ( options.color ) { str += pre + options.colorNumber( variable.toString() ) + options.colorType( " number" ) + options.nl ; } | ||
else { str += pre + variable.toString() + " <number>" + options.nl ; } | ||
} | ||
else if ( type === 'string' ) | ||
{ | ||
if ( options.html ) | ||
{ | ||
str += pre + '"<i style="color:cyan;">' + | ||
// escape( 'html_content' , variable ) + // need to backport this later | ||
variable + | ||
'</i>" <i style="color:gray;">string</i>' + options.nl ; | ||
} | ||
else | ||
{ | ||
// \r mess up things in linux CLI, turn them into literal | ||
variable = variable.replace( "\r" , "\\r" ) ; | ||
if ( options.color ) { str += pre + '"' + options.colorString( variable ) + '" ' + options.colorType( "string" ) + options.nl ; } | ||
else { str += pre + '"' + variable + '" <string>' + options.nl ; } | ||
} | ||
} | ||
else if ( type === 'function' ) | ||
{ | ||
funcName = variable.name ? variable.name : '<anonymous>' ; | ||
if ( options.html ) { str += pre + '<i style="color:gray;">' + funcName + " function (" + variable.length + ")</i>" ; } | ||
else if ( options.color ) { str += pre + options.colorClass( funcName ) + options.colorType( " function (" + variable.length + ")" ) ; } | ||
else { str += pre + "<" + funcName + " function> (" + variable.length + ")" ; } | ||
if ( runtime.depth < options.depth && options.proto ) | ||
{ | ||
propertyList = Object.getOwnPropertyNames( variable.prototype ) ; | ||
str += options.nl + runtime.indent + "--> prototype {" + options.nl ; | ||
for ( i = 0 ; i < propertyList.length ; i ++ ) | ||
{ | ||
str += inspect( { | ||
indent: nextIndent , | ||
ancestors: runtime.ancestors.concat( variable ) , | ||
key: propertyList[ i ] , | ||
keyIsProperty: true | ||
} , | ||
options , | ||
variable.prototype[ propertyList[ i ] ] | ||
) ; | ||
} | ||
str += runtime.indent + "}" ; | ||
} | ||
str += options.nl ; | ||
} | ||
else if ( type === 'object' ) | ||
{ | ||
isArray = Array.isArray( variable ) ; | ||
constructor = variable.constructor.name ? variable.constructor.name : '<constructorless>' ; | ||
if ( options.html ) { str += pre + '<i style="color:gray;">' + constructor + " object</i>" ; } | ||
else if ( options.color ) { str += pre + options.colorClass( constructor ) + options.colorType( " object" ) ; } | ||
else { str += pre + "<" + constructor + " object>" ; } | ||
propertyList = Object.getOwnPropertyNames( variable ) ; | ||
if ( ! propertyList.length ) | ||
{ | ||
str += " {}" + options.nl ; | ||
} | ||
else if ( runtime.ancestors.indexOf( variable ) !== -1 ) | ||
{ | ||
str += " [circular]" + options.nl ; | ||
} | ||
else if ( runtime.depth >= options.depth ) | ||
{ | ||
str += " [depth limit]" + options.nl ; | ||
} | ||
else | ||
{ | ||
//str += options.nl + runtime.indent + "{" + options.nl ; | ||
str += " {" + options.nl ; | ||
for ( i = 0 ; i < propertyList.length ; i ++ ) | ||
{ | ||
descriptor = Object.getOwnPropertyDescriptor( variable , propertyList[ i ] ) ; | ||
keyIsProperty = descriptor.enumerable && isArray ? false : true ; | ||
if ( descriptor.get || descriptor.set ) | ||
{ | ||
str += inspect( { | ||
depth: runtime.depth + 1 , | ||
ancestors: runtime.ancestors.concat( variable ) , | ||
indent: nextIndent , | ||
key: propertyList[ i ] , | ||
keyIsProperty: keyIsProperty , | ||
descriptor: descriptor | ||
} , | ||
options , | ||
{ get: descriptor.get , set: descriptor.set } | ||
) ; | ||
} | ||
else | ||
{ | ||
str += inspect( { | ||
depth: runtime.depth + 1 , | ||
ancestors: runtime.ancestors.concat( variable ) , | ||
indent: nextIndent , | ||
key: propertyList[ i ] , | ||
keyIsProperty: keyIsProperty , | ||
descriptor: descriptor | ||
} , | ||
options , | ||
variable[ propertyList[ i ] ] | ||
) ; | ||
} | ||
} | ||
str += runtime.indent + "}" + options.nl ; | ||
} | ||
} | ||
return str ; | ||
} | ||
string.inspect = inspect.bind( undefined , null ) ; | ||
/* | ||
function dumpPrototypeChainStr( object , options ) | ||
{ | ||
if ( object === null || typeof object !== 'object' ) return '<bad type>' ; | ||
var str = '' ; | ||
var current = Object.getPrototypeOf( object ) ; | ||
while ( typeof current === 'object' && current !== null ) | ||
{ | ||
if ( str ) str += " --> " ; | ||
str += current.constructor.name ; | ||
current = Object.getPrototypeOf( current ) ; | ||
} | ||
return str ; | ||
} | ||
// Shortcut | ||
var dump = function( variable , options ) { console.log( dumpStr( variable , options ) ) ; } ; | ||
var dumpError = function( error , options ) { console.log( dumpErrorStr( error , options ) ) ; } ; | ||
var dumpPrototypeChain = function( object , options ) { console.log( dumpPrototypeChainStr( object , options ) ) ; } ; | ||
*/ | ||
{ | ||
"name": "string-kit", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A string manipulation toolbox.", | ||
@@ -9,3 +9,6 @@ "main": "lib/string.js", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"terminal-kit": "^0.1.27", | ||
"tree-kit": "^0.2.4" | ||
}, | ||
"devDependencies": { | ||
@@ -12,0 +15,0 @@ "expect.js": "~0.3.1", |
@@ -26,4 +26,22 @@ | ||
inspect: [35mObject[39m[3m[90m object[39m[23m { | ||
[32ma[39m : "[34mA[39m" [3m[90mstring[39m[23m | ||
[32mb[39m : [34m2[39m[3m[90m number[39m[23m | ||
[32msub[39m : [35mObject[39m[3m[90m object[39m[23m { | ||
[32me[39m : "[34mee[39m" [3m[90mstring[39m[23m | ||
[32mf[39m : [34m6[39m[3m[90m number[39m[23m | ||
[32mcircular[39m : [35mObject[39m[3m[90m object[39m[23m [circular] | ||
} | ||
[32mempty[39m : [35mObject[39m[3m[90m object[39m[23m {} | ||
[32mlist[39m : [35mArray[39m[3m[90m object[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 : [34m3[39m[3m[90m number[39m[23m | ||
} | ||
} | ||
# TOC | ||
- [format()](#format) | ||
- [inspect()](#inspect) | ||
- [Escape](#escape) | ||
@@ -90,3 +108,13 @@ <a name=""></a> | ||
<a name="inspect"></a> | ||
# inspect() | ||
should. | ||
```js | ||
//console.log( 'inspect: ' , inspect( object ) ) ; | ||
console.log( 'inspect: ' , inspect( { color: true, proto: true, depth: 10 } , object ) ) ; | ||
//console.log( 'inspect: ' , inspect( { html: true } , object ) ) ; | ||
``` | ||
<a name="escape"></a> | ||
# Escape |
@@ -99,2 +99,48 @@ /* | ||
describe( "inspect()" , function() { | ||
var inspect = string.inspect ; | ||
var object = { | ||
a: 'A' , | ||
b: 2 , | ||
sub: { | ||
e: 'ee' , | ||
f: 6 | ||
} , | ||
empty: {} , | ||
list: [ 'one','two','three' ] | ||
} ; | ||
object.sub.circular = object ; | ||
/* | ||
Object.defineProperties( object , { | ||
c: { value: '3' } , | ||
d: { | ||
get: function() { return 'Dee' ; } , | ||
set: function( value ) {} | ||
} | ||
} ) ; | ||
//*/ | ||
/* | ||
it( "should" , function() { | ||
console.log( 'inspect: ' , inspect( true ) ) ; | ||
console.log( 'inspect: ' , inspect( { color: true } , true ) ) ; | ||
//console.log( 'inspect: ' , inspect( { html: true } , true ) ) ; | ||
} ) ; | ||
//*/ | ||
//* | ||
it( "should" , function() { | ||
//console.log( 'inspect: ' , inspect( object ) ) ; | ||
console.log( 'inspect: ' , inspect( { color: true, proto: true, depth: 10 } , object ) ) ; | ||
//console.log( 'inspect: ' , inspect( { html: true } , object ) ) ; | ||
} ) ; | ||
//*/ | ||
} ) ; | ||
describe( "Escape" , function() { | ||
@@ -101,0 +147,0 @@ it( "escape.regExp" ) ; |
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
32226
519
119
2
+ Addedterminal-kit@^0.1.27
+ Addedtree-kit@^0.2.4
+ Addedasync-kit@0.4.13(transitive)
+ Addedstring-kit@0.0.3(transitive)
+ Addedterminal-kit@0.1.28(transitive)
+ Addedtree-kit@0.2.4(transitive)