json-format
Advanced tools
Comparing version 0.0.1 to 0.1.1
30
index.js
@@ -12,7 +12,14 @@ /* | ||
var p = [], | ||
indentConfig = { | ||
tab: { char: '\t', size: 1 }, | ||
space: { char: ' ', size: 4 } | ||
}, | ||
configDefault = { | ||
type: 'tab' | ||
}, | ||
push = function( m ) { return '\\' + p.push( m ) + '\\'; }, | ||
pop = function( m, i ) { return p[i-1] }, | ||
tabs = function( count ) { return new Array( count + 1 ).join( '\t' ); }; | ||
tabs = function( count, indentType) { return new Array( count + 1 ).join( indentType ); }; | ||
function JSONFormat ( json ) { | ||
function JSONFormat ( json, indentType ) { | ||
p = []; | ||
@@ -35,10 +42,10 @@ var out = "", | ||
case '[': | ||
out += c + "\n" + tabs(++indent); | ||
out += c + "\n" + tabs(++indent, indentType); | ||
break; | ||
case '}': | ||
case ']': | ||
out += "\n" + tabs(--indent) + c; | ||
out += "\n" + tabs(--indent, indentType) + c; | ||
break; | ||
case ',': | ||
out += ",\n" + tabs(indent); | ||
out += ",\n" + tabs(indent, indentType); | ||
break; | ||
@@ -64,4 +71,13 @@ case ':': | ||
module.exports = function(json){ | ||
return JSONFormat(JSON.stringify(json)); | ||
module.exports = function(json, config){ | ||
config = config || configDefault; | ||
var indent = indentConfig[config.type]; | ||
if ( indent == null ) { | ||
throw('[' + config.type + '] is not compatible!'); | ||
} else { | ||
indentType = new Array((config.size || indent.size) + 1).join(indent.char); | ||
} | ||
return JSONFormat(JSON.stringify(json), indentType); | ||
} |
{ | ||
"name": "json-format", | ||
"version": "0.0.1", | ||
"version": "0.1.1", | ||
"description": "JSON format for good presentation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
json-format | ||
========== | ||
Change for using with npm modules. [original version](https://github.com/phoboslab/json-format). | ||
Parse JavaScript Object to a JSON String indented. | ||
#### Instaling #### | ||
#### Instaling | ||
``` | ||
@@ -11,5 +11,5 @@ npm install json-format | ||
#### Usage ### | ||
#### Usage | ||
``` | ||
var jsonFormat = require('./'); | ||
var jsonFormat = require('json-format'); | ||
var fs = require('fs'); | ||
@@ -21,14 +21,51 @@ var obj = { | ||
fs.writeFile('example.json', jsonFormat(obj), function(err){ | ||
/* using config default, indent with tabs */ | ||
fs.writeFile('example_tabs.json', jsonFormat(obj), function(err){ | ||
if (err) throw err; | ||
console.log('saved'); | ||
}); | ||
/* using indent with spaces */ | ||
var config = { | ||
type: 'space', | ||
size: 2 | ||
} | ||
fs.writeFile('example_spaces.json', jsonFormat(obj, config), function(err){ | ||
if (err) throw err; | ||
console.log('saved'); | ||
}); | ||
``` | ||
#### Result #### | ||
##### Result `example_tabs.json` | ||
``` | ||
{ | ||
"a": 1, | ||
"b": 2 | ||
} | ||
``` | ||
##### Result `example_spaces.json` | ||
``` | ||
{ | ||
"a": 1, | ||
"b": 2 | ||
} | ||
``` | ||
``` | ||
#### Default sizes | ||
``` | ||
{ | ||
tab: { size: 1 }, | ||
space: { size: 4 } | ||
} | ||
``` | ||
#### Config default | ||
``` | ||
{ | ||
type: 'tab' | ||
} | ||
``` | ||
[Based in this project](https://github.com/phoboslab/json-format). |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
4021
4
87
69
1