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 (源链)。感谢您的支持!

您可能感兴趣的

基于python+appium+yaml安卓UI自动化测试分享... 结构介绍 之前分享过一篇安卓UI测试,但是没有实现数据与代码分离,后期维护成本较高,所以最近抽空优化了一下。 不想看文章得可以直接去 Github ,欢迎拍砖 大致结构如下: 结构.png testyaml管理用例,实现数据与...
Plait.py – a fake data modeler plait.py plait.py is a program for generating fake data from composable yaml templates. The idea behind plait.py is that it should be easy to mod...
Declarative Configuration for kubeadm — #HeptioPro... In this particular protip, we’ll be focusing on kubeadm , a bootstrapping tool for Kubernetes clusters. Photo Credit: David C...
CVE-2017-6920:Drupal远程代码执行漏洞分析及POC构造... 作者: 启明星辰ADLab 0x01 漏洞描述 2017年6月21日,Drupal官方发布了一个编号为CVE-2017- 6920 的漏洞,影响为Critical。这是Drupal Core的YAML解析器处理不当所导致的一个远程代码执行漏洞,影响8.x的Drupal Core。 ...
RC、RS 使用方法 前面 我们的课程 中学习了 Pod 的一些基本使用方法,而且前面我们都是直接来操作的 Pod ,假如我们现在有一个 Pod 正在提供线上的服务,我们来想想一下我们可能会遇到的一些场景: 某次运营活动非常成功,网站访问量突然暴增 运行当前 Pod 的节点发生故障了, Po...