npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-beautiful-render

v2.1.7

Published

json beautiful render

Downloads

46

Readme

中文 | English

npm

支持两个功能:

  • 将json数据美化成html,在原生javascript或react、vue等框架中使用。

image

  • 将json数据格式化,加入换行符、缩进符等,方便阅读。

image

安装

NPM

npm add json-beautiful-render

UMD

<script src="//cdn.jsdelivr.net/npm/json-beautiful-render/lib/umd/index.js"></script>

umd模式暴露了全局变量jsonRenderjsonRenderNoStyle

使用

  • 基础使用
import jsonRender from 'json-beautiful-render';

const dom = jsonRender(document.querySelector('#container'), {
    name: 'hello world',
});
  • 定制渲染的颜色
import jsonRender from 'json-beautiful-render';

jsonRender(
    document.querySelector('#container'),
    {
        name: 'hello world',
    },
    {
        valueColor: 'green',
    },
);
  • 将json数据格式化,加入换行符、缩进符等,方便阅读。
import { jsonRenderNoStyle } from 'json-beautiful-render';

const result = jsonRenderNoStyle(document.querySelector('#your_textarea'), { name: 'hello world' });
console.log(result);

API

jsonRender

(el, json, options) => HTMLElement

美化渲染,如果el是HTMLElement,则将渲染结果挂载到el上。

| 参数 | 说明 | 类型 | 默认值 | | ------- | ---------- | -------------------------------- | ------ | | el | 挂载的容器 | HTMLElement | null | undefined | - | | json | JSON数据 | Object | - | | options | 配置 | Options | - |

Options

配置项

| 参数 | 说明 | 类型 | 默认值 | | -------------------- | ------------------------------------ | ----------- | ------ | | activeBgColor | 项目激活时,整行区域的背景色 | string | - | | activeHighLightColor | 项目激活时,文字区域的背景色 | string | - | | levelHighLightColor | 项目激活时,该项目所有父级键名的颜色 | string | - | | labelColor | 键名的文本颜色 | string | - | | valueColor | 键值的文本颜色 | string | - | | valueColors | 不同变量类型的文本颜色 | ValueColors | - | | expand | 允许展开和收缩功能 | boolean | true | | showItemsLength | 显示子元素的长度,collapse表示收缩时显示,always表示始终显示,其他值则不显示 | 'collapse' | 'always' | false | 'collapse' |

ValueColors

不同变量类型的文本颜色

| 参数 | 说明 | 类型 | 默认值 | | ----------- | ----------------------- | ------ | ------ | | string | 字符串 | string | - | | number | 数值 | string | - | | specialness | 特殊值,如boolean、null | string | - |

jsonRenderNoStyle

(el, json, options) => string

无样式的格式化渲染,如果el是HTMLElement,则将渲染结果挂载到el容器上。

| 参数 | 说明 | 类型 | 默认值 | | ------- | ---------- | -------------------------------- | ------ | | el | 挂载的容器 | HTMLElement | null | undefined | - | | json | JSON数据 | Object | - | | options | 配置 | Options | - |

Options

配置项

| 参数 | 说明 | 类型 | 默认值 | | ------ | -------- | ------ | ------ | | indent | 缩进字符 | string | - |

FAQ

渲染哪些类型的值?

支持渲染标准的json值如"123"123,其余则渲染其类型如DateFunction等。

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。详情见>>