@less-is-more/less-js 1.4.36-0 → 1.4.36-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +2 -2
- package/src/{json.js → json-kit.js} +23 -2
- package/test/test-json.js +15 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -8,7 +8,7 @@ var Cache = require("./cache.js");
|
|
|
8
8
|
var Service = require("./service.js");
|
|
9
9
|
var Oss = require("./oss.js");
|
|
10
10
|
var GroupMessage = require("./group-message.js");
|
|
11
|
-
var
|
|
11
|
+
var JsonKit = require("./json-kit.js");
|
|
12
12
|
module.exports = {
|
|
13
13
|
Ret,
|
|
14
14
|
Router,
|
|
@@ -20,5 +20,5 @@ module.exports = {
|
|
|
20
20
|
Service,
|
|
21
21
|
Oss,
|
|
22
22
|
GroupMessage,
|
|
23
|
-
|
|
23
|
+
JsonKit,
|
|
24
24
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
module.exports = class
|
|
1
|
+
module.exports = class JsonKit {
|
|
2
2
|
_findFirst(json, key, value, includeTree = false) {
|
|
3
3
|
if (Array.isArray(json)) {
|
|
4
4
|
return this._findFirstArray(json, key, value, includeTree);
|
|
@@ -64,7 +64,7 @@ module.exports = class Json {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
static findFirstTree(item, ...keyAndValue) {
|
|
67
|
-
return new
|
|
67
|
+
return new JsonKit()._find(item, ...keyAndValue);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
_find(item, ...keyAndValue) {
|
|
@@ -101,4 +101,25 @@ module.exports = class Json {
|
|
|
101
101
|
_checkContains(json, key, value) {
|
|
102
102
|
return JSON.stringify(json).includes(value);
|
|
103
103
|
}
|
|
104
|
+
|
|
105
|
+
static remove(item, props) {
|
|
106
|
+
// 递归删除指定属性,item为对象或数组
|
|
107
|
+
if (typeof item === "object" && item !== null) {
|
|
108
|
+
if (Array.isArray(item)) {
|
|
109
|
+
return item.map((element) => JsonKit.remove(element, props));
|
|
110
|
+
} else {
|
|
111
|
+
props.forEach((prop) => {
|
|
112
|
+
delete item[prop];
|
|
113
|
+
});
|
|
114
|
+
// 二层处理
|
|
115
|
+
Object.keys(item).forEach((key) => {
|
|
116
|
+
item[key] = JsonKit.remove(item[key], props);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return item;
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
return item;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
104
125
|
};
|
package/test/test-json.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
const assert = require("assert");
|
|
2
|
-
const
|
|
2
|
+
const JsonKit = require("../src/json-kit.js");
|
|
3
3
|
|
|
4
4
|
describe("json.js", () => {
|
|
5
5
|
const demo = `[{"a":[{"c":{"d":"1","e":2},"f":1},{"c2":{"d2":"1","e2":2},"f2":1}]}]`;
|
|
6
6
|
describe("findFirstTree()", () => {
|
|
7
7
|
it("ok", async () => {
|
|
8
8
|
try {
|
|
9
|
-
const result = await
|
|
9
|
+
const result = await JsonKit.findFirstTree(JSON.parse(demo), "e", 2);
|
|
10
|
+
console.log(JSON.stringify(result));
|
|
11
|
+
assert(true);
|
|
12
|
+
} catch (e) {
|
|
13
|
+
console.log(e);
|
|
14
|
+
assert(false);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("remove()", () => {
|
|
20
|
+
it("ok", async () => {
|
|
21
|
+
try {
|
|
22
|
+
const result = await JsonKit.remove(JSON.parse(demo), ["e", 2]);
|
|
10
23
|
console.log(JSON.stringify(result));
|
|
11
24
|
assert(true);
|
|
12
25
|
} catch (e) {
|