Skip to content

Commit e2c8c7b

Browse files
committed
json-format
0 parents  commit e2c8c7b

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
json-format
2+
==========
3+
4+
Change for using with npm modules. [original version](https://github.com/phoboslab/json-format).
5+
6+
#### Instaling ####
7+
```
8+
npm install json-format
9+
```
10+
11+
#### Usage ###
12+
```
13+
var jsonFormat = require('./');
14+
var fs = require('fs');
15+
var obj = {
16+
a: 1,
17+
b: 2
18+
}
19+
20+
fs.writeFile('example.json', jsonFormat(obj), function(err){
21+
if (err) throw err;
22+
console.log('saved');
23+
});
24+
```
25+
26+
#### Result ####
27+
```
28+
{
29+
"a": 1,
30+
"b": 2
31+
}
32+
```

index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
change for npm modules.
3+
by Luiz Estácio.
4+
5+
json-format v.1.1
6+
http://github.com/phoboslab/json-format
7+
8+
Released under MIT license:
9+
http://www.opensource.org/licenses/mit-license.php
10+
*/
11+
var p = [],
12+
push = function( m ) { return '\\' + p.push( m ) + '\\'; },
13+
pop = function( m, i ) { return p[i-1] },
14+
tabs = function( count ) { return new Array( count + 1 ).join( '\t' ); };
15+
16+
function JSONFormat ( json ) {
17+
p = [];
18+
var out = "",
19+
indent = 0;
20+
21+
// Extract backslashes and strings
22+
json = json
23+
.replace( /\\./g, push )
24+
.replace( /(".*?"|'.*?')/g, push )
25+
.replace( /\s+/, '' );
26+
27+
// Indent and insert newlines
28+
for( var i = 0; i < json.length; i++ ) {
29+
var c = json.charAt(i);
30+
31+
switch(c) {
32+
case '{':
33+
case '[':
34+
out += c + "\n" + tabs(++indent);
35+
break;
36+
case '}':
37+
case ']':
38+
out += "\n" + tabs(--indent) + c;
39+
break;
40+
case ',':
41+
out += ",\n" + tabs(indent);
42+
break;
43+
case ':':
44+
out += ": ";
45+
break;
46+
default:
47+
out += c;
48+
break;
49+
}
50+
}
51+
52+
// Strip whitespace from numeric arrays and put backslashes
53+
// and strings back in
54+
out = out
55+
.replace( /\[[\d,\s]+?\]/g, function(m){ return m.replace(/\s/g,''); } )
56+
.replace( /\\(\d+)\\/g, pop ) // strings
57+
.replace( /\\(\d+)\\/g, pop ); // backslashes in strings
58+
59+
return out;
60+
};
61+
62+
module.exports = function(json){
63+
return JSONFormat(JSON.stringify(json));
64+
}

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "json-format",
3+
"version": "0.0.1",
4+
"description": "JSON format for good presentation",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "[email protected]:luizstacio/json-format.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"format",
16+
"json",
17+
"pretty",
18+
"json",
19+
"write",
20+
"identation",
21+
"json",
22+
"ident",
23+
"format"
24+
],
25+
"author": "luizstacio",
26+
"license": "MIT",
27+
"bugs": {
28+
"url": "https://github.com/luizstacio/json-format/issues"
29+
},
30+
"homepage": "https://github.com/luizstacio/json-format"
31+
}

0 commit comments

Comments
 (0)