Merge pull request #15 from sielay/master

Support dates
This commit is contained in:
AJ ONeal 2018-09-22 13:54:48 -06:00 committed by GitHub
commit e7e44e6c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 75 deletions

0
cli.js Normal file → Executable file
View File

View File

@ -1,13 +1,12 @@
(function () {
"use strict";
var typeOf = require('remedial').typeOf
;
var typeOf = require('remedial').typeOf,
maxText = 60,
wrap = require('wordwrap')(maxText);
function stringify(data) {
var handlers
, indentLevel = ''
;
var handlers, indentLevel = '';
handlers = {
"undefined": function () {
@ -15,24 +14,36 @@
// as this may have unintended consequences
// For arrays, however, this behavior seems appropriate
return 'null';
}
, "null": function () {
},
"null": function () {
return 'null';
}
, "number": function (x) {
},
"number": function (x) {
return x;
}
, "boolean": function (x) {
},
"boolean": function (x) {
return x ? 'true' : 'false';
}
, "string": function (x) {
// to avoid the string "true" being confused with the
// the literal `true`, we always wrap strings in quotes
},
"string": function (x) {
var output = '|';
if (x.length <= maxText && x.indexOf('\n') === -1) {
return JSON.stringify(x);
}
, "array": function (x) {
var output = ''
;
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();
},
"array": function (x) {
var output = '';
if (0 === x.length) {
output += '[]';
@ -42,8 +53,7 @@
indentLevel = indentLevel.replace(/$/, ' ');
x.forEach(function (y) {
// TODO how should `undefined` be handled?
var handler = handlers[typeOf(y)]
;
var handler = handlers[typeOf(y)];
if (!handler) {
throw new Error('what the crap: ' + typeOf(y));
@ -55,10 +65,9 @@
indentLevel = indentLevel.replace(/ /, '');
return output;
}
, "object": function (x) {
var output = ''
;
},
"object": function (x) {
var output = '';
if (0 === Object.keys(x).length) {
output += '{}';
@ -67,9 +76,8 @@
indentLevel = indentLevel.replace(/$/, ' ');
Object.keys(x).forEach(function (k) {
var val = x[k]
, handler = handlers[typeOf(val)]
;
var val = x[k],
handler = handlers[typeOf(val)];
if ('undefined' === typeof val) {
// the user should do
@ -89,8 +97,8 @@
indentLevel = indentLevel.replace(/ /, '');
return output;
}
, "function": function () {
},
"function": function () {
// TODO this should throw or otherwise be ignored
return '[object Function]';
}

View File

@ -27,7 +27,8 @@
},
"test": "echo 'error no test'; exit 1",
"dependencies": {
"remedial": "1.x"
"remedial": "1.x",
"wordwrap": "^1.0.0"
},
"devDependencies": {},
"preferGlobal": true