yamilfy-object: Stringify object with YAML like syntax

综合技术 2018-03-04 阅读原文

yamlify-object

Stringify object/array with yaml syntax

Install

npm install yamlify-object

Usage

const yamlifyObject = require('yamlify-object');

const obj = {
  array: [
    2,
    'two',
    {
      emptyArray: [],
    },
  ],
  error: new Error('message'),
  date: new Date(0),
  object: {
    number: 200,
    bool: false,
    null: null,
    undefined: undefined,
    emptyObject: {},
  },
};

obj.circular = obj;

const formattedString = yamlifyObject(obj, {
  indent: '  ',
  prefix: 'n',
  postfix: 'n',
});

console.log(formattedString);
/*

  array:
    - 2
    - two
    - emptyArray: []
  error: Error: message
  date: new Date(1970-01-01T00:00:00.000Z)
  object:
    number: 200
    bool: false
    null: null
    undefined: undefined
    emptyObject: {}
  circular: [Circular]

*/

API

yamlifyObject(input, [options])

input

Type: Object Array

options

indent

Type: string
Default: ' ' - single space

prefix

Type: string
Default: 'n'

postfix

Type: string
Default: ''

dateToString(date: Date)

Type: Function

Expected to return a string that stringified version of Date instance.

errorToString(error: Error)

Type: Function

Expected to return a string that stringified version of Error instance.

colors

Type: object
Default:

{
  date: function (s: string): string,
  error: function (s: string): string,
  symbol: function (s: string): string,
  string: function (s: string): string,
  number: function (s: string): string,
  boolean: function (s: string): string,
  null: function (s: string): string,
  undefined: function (s: string): string,
}

Each property of colors object expected to be a function that expected to return somehow colorified version of passed string argument.

You can specify colors only for types you need and the rest will stay colorless strings:

const obj = {
  number: 1
  string: 'str',
  bool: true,
};

const formattedString = yamlifyObject(obj, {
  colors: {
    number: (value) => `COLOR_CODE${value}COLOR_CODE`,
    boolean: (value) => `COLOR_CODE${value}COLOR_CODE`,
  },
});

console.log(formattedString);
/*
 number: COLOR_CODE1COLOR_CODE
 string: str
 bool: COLOR_CODEtrueCOLOR_CODE
*/

For terminal can be used yamlify-object-colors preset:

Github

责编内容by:Github阅读原文】。感谢您的支持!

您可能感兴趣的

Building a System for Front-End Translations Introduction As of August 2018 we’re happy to say that Squarespace’s all-in-one platform is now available to members ...
OpenAPI (Swagger 2.0) YAML Generation Using API Co... In this post, you will learn how to model and generate an OpenAPI (swagger 2.0) specification using API Connect on IBM C...
私密信息管理利器 HashiCorp Vault——验证和授权... 到目前为止,我们都是使用 vault 客户端直接访问服务器,并未进行任何登录之类的操作。这是因为在开发模式下,服务器会自动将用户登录为 root 用户,目的是为了简化测试,避免在登录问题上卡住初学者。但在生产环境中这显然是非常不安全的。再...
Hello, Dorling! (Creating Dorling Cartograms from ... NOTE: There is some iframe d content in this post and you canbust out of it if you want to see the document in a ...
Everything You Need to Know About Monitoring Mixin... There are lots of great community-built dashboards available in the Grafana website , and we’ve made it easier tha...