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阅读原文】。感谢您的支持!

您可能感兴趣的

About Time: Finding Solar Noon I had a code bunny a while ago for an app that gives you the time centered on “solar noon”, the time of day when the sun is directly above you. B...
Generate Kubernetes YAML Right From Your App Code A new programming language, Ballerina , can compile right into Docker and Kubernetes artifacts. Kubernetes is rapidly becoming the platform to ru...
实现自己的.NET Core配置Provider之Yaml YAML是一种更适合人阅读的文件格式,很多大型的项目像Ruby on Rails都选择YAML作为配置文件的格式。如果项目的配置很少,用JSON或YAML没有多大差别。看看rails项目中的配置文件,如果用JSON写试试什么感受吧。 在 《实现自己的.NET Core配置Provider之EF...
How did I improve latency by 700 percent using syn... We @media.net write superfast backends with at max 30-40ms turn-around time from web-service. We continuously try to reduce money spent per request. T...
LLVM构建了PDB文档,并提供了PDB与YAML的转换工具... 要使用上Windows平台提供的丰富工具,需要 LLVM 等编译器编写者可以生成PDB格式的文件。PDB,即程序调试数据库(Program Debug Database),是一种字符数据库,它描述了Windows平台上编译的代码。由于PDF文件中包含了各种类型的记录,因此它使得调试器等工具可...