json-clip 实现了类似grahlql基于查询裁剪对象的功能
1.声明式写法 2.结构化查询语句 3.支持路径查找对象 4.支持数组对象裁剪 5.支持数组第n项裁剪 6.辅助工具: d2-pojo裁剪
##使用方式
var d2 = require('json-clip');
const data = d2(query, source);
//data为查询后的数据
查询支持:路径查询 如:'a.b.c',其中'.'表示当前路径,那就是当前key
路径查询,操作函数: d2.dot(path,defaultValue,queryobj);
//源数据
{
smaple: "1",
list: [
{
first: 1,
seconde: 2,
three: {
h: 1,
g: "ff"
}
},
{
first: 1,
seconde: 2,
three: {
h: 2,
g: "ffg"
}
}
],
data: {
smaple: "1",
smapleObje: {
x: "xxx",
a: {
wo: "123"
},
b: {
wo: "123"
}
},
list: [{ first: "list1", seconde: 2 }, { first: 1, seconde: 2 }],
obx: {
limit: 1,
offset: 2,
items: [
{
monther: "nilaomao",
father: "nidaye"
},
{
monther: "nilaomao",
father: "nidaye"
}
]
}
},
array: {
limit: 1,
offset: 2,
items: [
{
monther: "nilaomao",
father: "nidaye"
},
{
monther: "nilaomao2",
father: "nidaye2"
}
]
}
}
const query1 = {
smaple: "."
};
结果: { "smaple": "1" }
const query2 = {
data: {
smapleObje: "."
}
};
结果: { "data": { "smapleObje": { "x": "xxx", "a": { "wo": "123" }, "b": { "wo": "123" } } } }
const query3 = {
array: {
items: {
monther: "."
}
}
};
结果: { "array": { "items": [ { "monther": "nilaomao" }, { "monther": "nilaomao2" } ] } }
const query4 = {
array: ".items.0"
};
结果: { "array": { "monther": "nilaomao", "father": "nidaye" } }
const query5 = {
array: d2.dot(".items.1", null, {
monther: "."
})
};
结果: { "array": { "monther": "nilaomao2" } }
const query5_1 = {
array: d2.dot(".items.1", null, {
monther: value => "妈妈" + value
})
};
结果: { "array": { "monther": "妈妈nilaomao2" } }
const query6 = {
bobocustom: "data.smapleObje"
};
结果:{ "bobocustom": { "x": "xxx", "a": { "wo": "123" }, "b": { "wo": "123" } } }
const query7 = {
bobocustom: d2.dot("data.smapleObje", null, { a: "." })
};
结果:{ "bobocustom": { "a": { "wo": "123" } } }
const query8 = {
xxx: d2.dot(".", [{ four: 10 }, { four: 11 }, { four: 12 }])
};
结果:{ "xxx": [ { "four": 10 }, { "four": 11 }, { "four": 12 } ] }
const query9 = {
bobocustom: d2.dot("data.list", null, {
first: d2.any() /*d2.any()等同于 ‘.’ */
})
};
结果:{ "bobocustom": [ { "first": "list1" }, { "first": 1 } ] }
const query10 = {
smaple: value => value + "mmm"
};
结果: "smaple": "1mmm" }
const query11 = {
bobocustom: d2.dot("data.list", null, {
first: value => undefined, //和直接不写first 这节点一样
seconde: "."
})
};
结果:{ "bobocustom": [ { "seconde": 2 }, { "seconde": 2 } ] }