Compare commits
No commits in common. "be09bf9dd31672c22ddc263668d07a6e1f5f72b5" and "aecfdcec99eea24d62b2a551b09d6fbaa361f8e1" have entirely different histories.
be09bf9dd3
...
aecfdcec99
@ -1,8 +1,3 @@
|
|||||||
Unmaintained Mirror
|
|
||||||
===============
|
|
||||||
|
|
||||||
Please go to the real repo at <https://git.coolaj86.com/coolaj86/json2yaml.js>
|
|
||||||
|
|
||||||
json2yaml
|
json2yaml
|
||||||
===
|
===
|
||||||
|
|
||||||
|
66
index.js
66
index.js
@ -1,12 +1,13 @@
|
|||||||
(function () {
|
(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var typeOf = require('remedial').typeOf,
|
var typeOf = require('remedial').typeOf
|
||||||
maxText = 60,
|
;
|
||||||
wrap = require('wordwrap')(maxText);
|
|
||||||
|
|
||||||
function stringify(data) {
|
function stringify(data) {
|
||||||
var handlers, indentLevel = '';
|
var handlers
|
||||||
|
, indentLevel = ''
|
||||||
|
;
|
||||||
|
|
||||||
handlers = {
|
handlers = {
|
||||||
"undefined": function () {
|
"undefined": function () {
|
||||||
@ -14,36 +15,24 @@
|
|||||||
// as this may have unintended consequences
|
// as this may have unintended consequences
|
||||||
// For arrays, however, this behavior seems appropriate
|
// For arrays, however, this behavior seems appropriate
|
||||||
return 'null';
|
return 'null';
|
||||||
},
|
}
|
||||||
"null": function () {
|
, "null": function () {
|
||||||
return 'null';
|
return 'null';
|
||||||
},
|
}
|
||||||
"number": function (x) {
|
, "number": function (x) {
|
||||||
return x;
|
return x;
|
||||||
},
|
}
|
||||||
"boolean": function (x) {
|
, "boolean": function (x) {
|
||||||
return x ? 'true' : 'false';
|
return x ? 'true' : 'false';
|
||||||
},
|
}
|
||||||
"string": function (x) {
|
, "string": function (x) {
|
||||||
var output = '|';
|
// to avoid the string "true" being confused with the
|
||||||
if (x.length <= maxText && x.indexOf('\n') === -1) {
|
// the literal `true`, we always wrap strings in quotes
|
||||||
return JSON.stringify(x);
|
return JSON.stringify(x);
|
||||||
}
|
}
|
||||||
var text = wrap(x).split(/\\n|\n/);
|
, "array": function (x) {
|
||||||
indentLevel = indentLevel.replace(/$/, ' ');
|
var output = ''
|
||||||
text.forEach(function (y) {
|
;
|
||||||
output += '\n' + indentLevel + y;
|
|
||||||
|
|
||||||
});
|
|
||||||
indentLevel = indentLevel.replace(/ /, '');
|
|
||||||
|
|
||||||
return output;
|
|
||||||
},
|
|
||||||
"date": function (x) {
|
|
||||||
return x.toJSON();
|
|
||||||
},
|
|
||||||
"array": function (x) {
|
|
||||||
var output = '';
|
|
||||||
|
|
||||||
if (0 === x.length) {
|
if (0 === x.length) {
|
||||||
output += '[]';
|
output += '[]';
|
||||||
@ -53,7 +42,8 @@
|
|||||||
indentLevel = indentLevel.replace(/$/, ' ');
|
indentLevel = indentLevel.replace(/$/, ' ');
|
||||||
x.forEach(function (y) {
|
x.forEach(function (y) {
|
||||||
// TODO how should `undefined` be handled?
|
// TODO how should `undefined` be handled?
|
||||||
var handler = handlers[typeOf(y)];
|
var handler = handlers[typeOf(y)]
|
||||||
|
;
|
||||||
|
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
throw new Error('what the crap: ' + typeOf(y));
|
throw new Error('what the crap: ' + typeOf(y));
|
||||||
@ -65,9 +55,10 @@
|
|||||||
indentLevel = indentLevel.replace(/ /, '');
|
indentLevel = indentLevel.replace(/ /, '');
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
},
|
}
|
||||||
"object": function (x) {
|
, "object": function (x) {
|
||||||
var output = '';
|
var output = ''
|
||||||
|
;
|
||||||
|
|
||||||
if (0 === Object.keys(x).length) {
|
if (0 === Object.keys(x).length) {
|
||||||
output += '{}';
|
output += '{}';
|
||||||
@ -76,8 +67,9 @@
|
|||||||
|
|
||||||
indentLevel = indentLevel.replace(/$/, ' ');
|
indentLevel = indentLevel.replace(/$/, ' ');
|
||||||
Object.keys(x).forEach(function (k) {
|
Object.keys(x).forEach(function (k) {
|
||||||
var val = x[k],
|
var val = x[k]
|
||||||
handler = handlers[typeOf(val)];
|
, handler = handlers[typeOf(val)]
|
||||||
|
;
|
||||||
|
|
||||||
if ('undefined' === typeof val) {
|
if ('undefined' === typeof val) {
|
||||||
// the user should do
|
// the user should do
|
||||||
@ -97,8 +89,8 @@
|
|||||||
indentLevel = indentLevel.replace(/ /, '');
|
indentLevel = indentLevel.replace(/ /, '');
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
},
|
}
|
||||||
"function": function () {
|
, "function": function () {
|
||||||
// TODO this should throw or otherwise be ignored
|
// TODO this should throw or otherwise be ignored
|
||||||
return '[object Function]';
|
return '[object Function]';
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"cli",
|
"cli",
|
||||||
"util"
|
"util"
|
||||||
],
|
],
|
||||||
"version": "1.2.0",
|
"version": "1.1.0",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -27,8 +27,7 @@
|
|||||||
},
|
},
|
||||||
"test": "echo 'error no test'; exit 1",
|
"test": "echo 'error no test'; exit 1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"remedial": "1.x",
|
"remedial": "1.x"
|
||||||
"wordwrap": "^1.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
"devDependencies": {},
|
||||||
"preferGlobal": true
|
"preferGlobal": true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user