1.3.2 • Published 3 years ago

@wibetter/json-utils v1.3.2

Weekly downloads
169
License
MIT
Repository
github
Last release
3 years ago

json-utils

提供JSON相关的各类工具方法,比如schema转json、json转schema、json元数据分析等

json: JSON(JavaScript Object Notation, JS对象简谱) 是一种轻量级的数据交换格式。

schema: 一般用来描述JSON的数据格式,常用于json数据格式的校验。(json schema官网

json工具集合 / json工具方法清单

8个通用的json工具方法

  • getJsonDataByKeyRoute(使用示例): 根据key值路径获取对应的json数值对象(比如用于获取json数据中'data-user-name'对应的数据)
  • getSchemaByIndexRoute(使用示例): schema工具方法,根据index索引路径获取对应的schema数据对象(比如通过'2-1'获取schema中第3个子对象中的第2个字段对应的数据)
  • getSchemaByKeyRoute: schema工具方法,根据key路径获取对应的schema数据对象(比如通过'style-padding'获取schema数据)
  • indexRoute2keyRoute(使用示例): schema工具方法,根据index索引路径转换对应的key值路径
  • keyRoute2indexRoute:schema工具方法,根据key值路径获取对应的index索引路径
  • json2schema(使用示例): 根据json数据内容生成对应的schema数据
  • schema2json(使用示例): 根据schema结构数据生成一份对应的json数据内容
  • json2treeData(使用示例): 根据当前的json生成对应的treeData(供antd的TreeSelect等使用)
  • metaElemAnalyzer(使用示例): 根据当前的json和对应的schema,统计当前json里面用到的各类元数据情况

json数据内容相关的操作方法

  • getParentKeyRoute: 获取父元素的key路径值
  • getParentKeyRoute_CurKey: 获取父元素的key路径值和当前key

schema相关的操作方法

  • isSameParent: 判断是否是同一个父元素
  • getParentIndexRoute: 获取父元素的路径值
  • getNextIndexRoute: 获取下一个兄弟元素的路径值
  • getParentIndexRoute_CurIndex: 获取父元素的路径值和当前index
  • moveForward: 将当前路径值向前移动一位
  • moveBackward: 将当前路径值向后移动一位
  • getCurPosition: 判断当前元素在目标元素的位置 前 or 后(根据当前元素的位置和目标元素的位置)
  • isStructuredSchema: 判断是否为结构化schema数据(一级schema为object类型,其所有二级schema为object类型)

纯工具方法

  • objClone: js对象数据深拷贝,避免数据联动
  • isEqual: 对比两个json数据是否相等
  • exitPropertie: 判断当前属性是否存在(数值为false、0、null也会判断为存在的属性)

数值类型判断

  • isURL
  • isString
  • isNumber
  • isBoolean
  • isDateStr
  • isDateTimeStr
  • isTimeStr
  • isArray
  • isSelect
  • isObject
  • isQuantity
  • isColor
  • isFunction

业务相关的json工具方法

主要在JSON数据可视化组件中使用(JSONSchemaJSONEditor

  • isEmptyWidgetSchema: 判断是否为空的WidgetSchema
  • isUsedToWidgetConfig: 判断是否为用于区块配置的jsonSchema数据
  • isNewSchemaData: 判断是否是最新版的schema数据
  • oldSchemaToNewSchema: 旧版jsonSchema转新版jsonSchema
  • schemaMetaList: 目前JSON数据可视化组件(JSONSchema、JSONEditor)提供的元数据类型清单
  • dynamicDataAnalyzer: 根据当前的json和对应的schema,统计当前json里面用到的动态数据源情况
  • isFirstSchemaData: 根据format判断是否为一级类型字段(func、style、data)
  • isBoxSchemaData: 根据format判断是否为容器类型字段(func、style、data、object)
  • getCurrentFormat: 获取当前字段的类型(format)
  • isEmptySchema: 判断是否为空的Schema

快速使用

npm install --save @wibetter/json-utils
// 获取schema转json的方法
const { schema2json } = require('@wibetter/json-utils');
const jsonSchema = {
  ...
};
const curJsonData = {
  ...
}

const jsonData = schema2json(jsonSchema, curJsonData); // schema2json
``
1.3.2

3 years ago

1.3.1

3 years ago

1.2.31

3 years ago

1.2.28

3 years ago

1.2.29

3 years ago

1.2.30

3 years ago

1.2.27

3 years ago

1.2.26

3 years ago

1.2.23

3 years ago

1.2.25

3 years ago

1.2.22

3 years ago

1.2.21

3 years ago

1.2.20

3 years ago

1.2.19

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.13

4 years ago

1.2.15

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.8

4 years ago

1.2.9

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago