Multiline

This commit is contained in:
Lukasz Sielski 2016-04-28 14:50:01 +01:00
parent d1473483b2
commit a6fbbc45a9
3 changed files with 84 additions and 78 deletions

0
cli.js Normal file → Executable file
View File

View File

@ -1,13 +1,12 @@
(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 var handlers, indentLevel = '';
, indentLevel = ''
;
handlers = { handlers = {
"undefined": function () { "undefined": function () {
@ -15,27 +14,36 @@
// 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) {
// to avoid the string "true" being confused with the var output = '|';
// the literal `true`, we always wrap strings in quotes if (x.length <= maxText && x.indexOf('\n') === -1) {
return JSON.stringify(x); return JSON.stringify(x);
} }
, "date": function(x) { var text = wrap(x).split(/\\n|\n/);
indentLevel = indentLevel.replace(/$/, ' ');
text.forEach(function (y) {
output += '\n' + indentLevel + y;
});
indentLevel = indentLevel.replace(/ /, '');
return output;
},
"date": function (x) {
return x.toJSON(); return x.toJSON();
} },
, "array": function (x) { "array": function (x) {
var output = '' var output = '';
;
if (0 === x.length) { if (0 === x.length) {
output += '[]'; output += '[]';
@ -45,8 +53,7 @@
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));
@ -58,10 +65,9 @@
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 += '{}';
@ -70,9 +76,8 @@
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
@ -92,8 +97,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]';
} }

View File

@ -27,7 +27,8 @@
}, },
"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