【问题标题】:changing representation of Node Objects in the REPL在 REPL 中更改节点对象的表示
【发布时间】:2018-05-29 12:57:50
【问题描述】:

我想让“时刻”对象表示更短,因为我有一个包含许多对象的数据结构,我希望我可以使其表示更紧凑。

例子:

node-repl>  d = new Date()
2018-05-29T12:50:12.111Z 
node-repl> m = moment(d)
=> VERY LONG THING IN OUTPUT

现在假设您有许多矩对象“m1”、“m2”...,它们分散到一个数据结构中,如 l = ["hello", m1, "world", m2, m3...]。 阅读列表“l”变得困难。

我希望在 REPL 中默认将“时刻”对象打印为“时刻 DD-MM-YYYY”。

应该和这个差不多,但是不知道怎么弄好。

moment.prototype.toString = function() {
   var dd = this.format("DD-MM-YYYY");
   var out = "moment-" + dd;
   return out;
}

你有什么建议吗?

【问题讨论】:

标签: javascript node.js momentjs read-eval-print-loop


【解决方案1】:

我找到了解决方案,我不明白它是如何工作的所有问题,但它确实有效。

moment.prototype[util.inspect.custom] = function(depth, options) {
 var tmp = this.format("DD-MM-YYYY");
 tmp = `moment<${tmp}>`;
 var out = options.stylize(tmp, 'special');
 return out;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多