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

jsonqueryjs

v2.0.4

Published

支持对象数据(json/array)数据的查找及格式化数据

Downloads

5

Readme

jsonQuery.js

版本更新记录

2.0.0 整体设计模式进行更改 2.0.1 添加formatIndex方法,为数组添加广度/深度索引值 2.0.2 closest方法返回值类型更改 arr->object 2.0.3 jsonQuery.toolUtil更名为jsonQuery.tool;jsonQuery.tool增加getValueWithKey方法 2.0.4 change some docs

使用说明

commonJs使用:import jsonQuery from 'jsonqueryjs/jsonQuery' amd/cmd使用: define(['jsonqueryjs/jsonQuery'], function(jsonQuery) {} ); script引用:<script src='node_modules/jsonqueryjs/jsonQuery.js'></script>

数据初始化(后续操作前提)

对象数据查询相关方法

对象数据操作相关方法

其他工具方法

基本概念描述

rule(规则),此处规则分为3类
规则一:"'a'='1'"
       "'a'='1'" = (key="a" & value="1")  a和1都被标记为字符串,此时解释为查询规则为

规则二:"'a'=1" / "0=1"
       "'a'=1" = (key='a' & value=1)
       "0=1"   = (key=0 & value=1) 此时说明是针对数组下标为0值为1的查询条件   

规则三:"=1" / "'a'="
       "=1" = (key= & value=1) 此时会忽略key值判断,不论是数组还是json对象,只要value=1都被认定为符合条件


modeType(匹配模式),此处分为2类

模式一:'contain'(常用)
      包含模式 如数据{"a":3},如果rule='"a"=3',则认为查找的对象为{"a":3},通过后续api,你会更好的理解
模式二:'strict'
      严格匹配模式

数据初始化

jsonQuery(data)

描述:数据源绑定
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     var jsonInstance = jsonQuery(data);

target(rule)

描述:目标源设定
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     var jsonInstance = jsonQuery(data).target('"c"=12');

对象数据查询相关方法

find()

描述:节点查询
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     var result = jsonQuery(data).target('"c"=12').find().val();
     console.log(result) => 结果:{a: 10, b: 11, c: 12, d: 13}

parents()

描述:父节点查询
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     var result = jsonQuery(data).target('"c"=12').parents().val();
     console.log(result) => 结果:[{a: 1, b: {a: 10, b: 11, c: 12, d: 13}, {a: 10, b: 11, c: 12, d: 13}]

closest(options)

描述:查找最近的复合指定条件的祖先元素
参数:options = {key: 'testKey', value: 'testValue'} > 即为查找祖先元素中存在条件(a:1) 的父元素
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').closest({key: 'a', value: 1}).val();
     console.log(result) => 结果:{a: 1, b: {a: 10, b: 11, c: 12, d: 13}}
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').closest({key: 'a', value: 10}).val();
     console.log(result) => 结果:{a: 10, b: 11, c: 12, d: 13}

siblings([modeType], [queryType])

描述:兄弟节点查询
参数:modeType 'strict' | 'contain' 两种查找匹配模式选择
     queryType 'all' | 'before' | 'after' | 'beforeAll' | 'afterAll' 兄弟节点查找的类型
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').siblings('strict', 'all').val();
     console.log(result) => 结果:[10, 11, 13]
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').siblings('strict', 'before').val();
     console.log(result) => 结果:[11]
     ---实例三---
     var result = jsonQuery(data).target('"c"=12').siblings('strict', 'beforeAll').val();
     console.log(result) => 结果:[10, 11]
     ---实例四---
     var result = jsonQuery(data).target('"c"=12').siblings('contain', 'all').val();
     console.log(result) => 结果:[1]

对象数据操作相关方法

insert(options, [modeType], [insertType])

描述:节点插入
参数:options = {key: 'testKey', value: 'testValue'} > 即插入的目标数据
     modeType 'strict' | 'contain' 两种查找匹配模式选择
     insertType 'before' | 'after' 插入的节点位置
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').insert({key: 'testKey', value: 'testValue'}, 'strict', 'before').val();
     console.log(result) => 结果:{a: 1, b: {a: 10, b: 11, testKey: 'testValue', c: 12, d: 13}}
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').insert({key: 'testKey', value: 'testValue'}, 'contain', 'before').val();
     console.log(result) => 结果:{a: 1, testKey: 'testValue', b: {a: 10, b: 11, c: 12, d: 13}}

delete([modeType])

描述:节点删除
参数:modeType 'strict' | 'contain' 两种查找匹配模式选择
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').delete('strict').val();
     console.log(result) => 结果:{a: 1, b: {a: 10, b: 11, d: 13}}
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').delete('contain').val();
     console.log(result) => 结果:{a: 1}

deleteSiblings([modeType], [deleteType])

描述:相关兄弟节点删除
参数:modeType 'strict' | 'contain' 两种查找匹配模式选择
     deleteType 'all' | 'beforeAll' | 'afterAll' 删除指定位置的兄弟节点
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').deleteSiblings('strict', 'beforeAll').val();
     console.log(result) => 结果:{a: 1, b: {c: 12, d: 13}}
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').deleteSiblings('contain', 'beforeAll').val();
     console.log(result) => 结果:{b: {a: 10, b: 11, c: 12, d: 13}}

replace(options, [modeType])

描述:节点替换
参数:options = {key: 'testKey', value: 'testValue'} > 即插入的目标数据
     modeType 'strict' | 'contain' 两种查找匹配模式选择
实例:
     var data = {a: 1, b: {a: 10, b: 11, c: 12, d: 13}};
     ---实例一---
     var result = jsonQuery(data).target('"c"=12').replace({key: 'testKey', value: 'testValue'}, 'strict').val();
     console.log(result) => 结果:{a: 1, b: {a: 10, b: 11, testKey: 'testValue', d: 13}}
     ---实例二---
     var result = jsonQuery(data).target('"c"=12').replace({key: 'testKey', value: 'testValue'}, 'contain').val();
     console.log(result) => 结果:{a: 1, testKey: 'testValue'}

其他工具方法

isJson(data)

描述:json对象判断
实例:
    var result = jsonQuery.tool.isJson({a: 1});
    console.log(result) => 结果:true

isArray(data)

描述:array对象判断
实例:
    var result = jsonQuery.tool.isArray([1]);
    console.log(result) => 结果:true

isInArray(arr, target)

描述:判断是否位于数组中
实例:
    var result = jsonQuery.tool.isInArray([1], 1);
    console.log(result) => 结果:true  

getJsonArrLength(data)

描述:获取对象长度
实例:
    var result = jsonQuery.tool.getJsonArrLength([1]);
    console.log(result) => 1  

deepCopy(data)

描述:对象深度拷贝
实例:
    var result = jsonQuery.tool.deepCopy([1, {a: 1}]);
    console.log(result) => 结果:[1, {a: 1}]  

formatIndex(options, yIndex)

描述:为数组添加广度/深度索引值
参数:options = {data: [a:1], xKey: 'xKey', yKey: 'yKey' }
     options.data: 格式化数据
     options.xKey: 广度索引key值
     options.yKey: 深度索引key值
     yIndex: 深度起始索引,一般为0,固定为0
实例:
     var result = jsonQuery.tool.formatIndex({data: [{a: 'a', childs: [{a: 'a'}]}]}, 0);
     console.log(result) => [{a: 'a', xIndex: 0, yIndex: 0, childs: [{a: 'a', xIndex: 0, yIndex: 1}]}]
    

getValueWithKey(options)

描述:根据key值进行对应value匹配查找
参数:options = {data: {a:1}, key: 'a' }
     options.data: 数据源
     options.key: 匹配value对应的查找key值(如果是数组,则直接传数组下标index即可)
实例:
     var result = jsonQuery.tool.getValueWithKey({data: {a:1}, key: 'a' });
     console.log(result) => [1]
    

compare(data1, data2)

描述:两组数据比较,对象不进行地址比较
实例:
    var result = jsonQuery.tool.compare([1, {a: 1}], [1, {a: 1}]);
    console.log(result) => 结果:true  

logInfo(type, info)

描述:日志信息控制台输出
实例:
    jsonQuery.tool.logInfo('error', 'msg error');