@nsnanocat/util 1.7.7 → 1.8.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/lib/done.mjs CHANGED
@@ -32,29 +32,12 @@ export function done(object = {}) {
32
32
  $done(object);
33
33
  break;
34
34
  case "Shadowrocket":
35
- default:
36
35
  Console.log("🚩 执行结束!");
37
36
  $done(object);
38
37
  break;
39
38
  case "Quantumult X":
40
39
  if (object.policy) _.set(object, "opts.policy", object.policy);
41
- // 移除不可写字段
42
- object["auto-redirect"] = undefined;
43
- object["auto-cookie"] = undefined;
44
- object["binary-mode"] = undefined;
45
- object.charset = undefined;
46
- object.host = undefined;
47
- object.insecure = undefined;
48
- object.method = undefined; // 1.4.x 不可写
49
- object.ok = undefined; // 来自fetch()的响应对象, 不可写
50
- object.opt = undefined; // $task.fetch() 参数, 不可写
51
- object.path = undefined; // 可写, 但会与 url 冲突
52
- object.policy = undefined;
53
- object["policy-descriptor"] = undefined;
54
- object.scheme = undefined;
55
- object.sessionIndex = undefined;
56
- object.statusCode = undefined;
57
- object.timeout = undefined;
40
+ object = _.pick(object, ["status", "url", "headers", "body", "bodyBytes"]);
58
41
  switch (typeof object.status) {
59
42
  case "number":
60
43
  object.status = StatusCodes[object.status];
@@ -77,6 +60,7 @@ export function done(object = {}) {
77
60
  $done(object);
78
61
  break;
79
62
  case "Node.js":
63
+ default:
80
64
  Console.log("🚩 执行结束!");
81
65
  process.exit(1);
82
66
  break;
package/package.json CHANGED
@@ -36,5 +36,5 @@
36
36
  "devDependencies": {
37
37
  "typescript": "^5.6.3"
38
38
  },
39
- "version": "1.7.7"
39
+ "version": "1.8.1"
40
40
  }
@@ -1,5 +1,16 @@
1
1
  /* https://www.lodashjs.com */
2
2
  export class Lodash {
3
+ static escape(string) {
4
+ const map = {
5
+ "&": "&",
6
+ "<": "&lt;",
7
+ ">": "&gt;",
8
+ '"': "&quot;",
9
+ "'": "&#39;",
10
+ };
11
+ return string.replace(/[&<>"']/g, m => map[m]);
12
+ }
13
+
3
14
  static get(object = {}, path = "", defaultValue = undefined) {
4
15
  // translate array case to dot case, then split with .
5
16
  // a[0].b -> a.0.b -> ['a', '0', 'b']
@@ -11,22 +22,22 @@ export class Lodash {
11
22
  return result === undefined ? defaultValue : result;
12
23
  }
13
24
 
14
- static set(object, path, value) {
15
- if (!Array.isArray(path)) path = Lodash.toPath(path);
16
- path.slice(0, -1).reduce((previousValue, currentValue, currentIndex) => (Object(previousValue[currentValue]) === previousValue[currentValue] ? previousValue[currentValue] : (previousValue[currentValue] = /^\d+$/.test(path[currentIndex + 1]) ? [] : {})), object)[path[path.length - 1]] = value;
25
+ static omit(object = {}, paths = []) {
26
+ if (!Array.isArray(paths)) paths = [paths.toString()];
27
+ paths.forEach(path => Lodash.unset(object, path));
17
28
  return object;
18
29
  }
19
30
 
20
- static unset(object = {}, path = "") {
31
+ static pick(object = {}, paths = []) {
32
+ if (!Array.isArray(paths)) paths = [paths.toString()];
33
+ const filteredEntries = Object.entries(object).filter(([key, value]) => paths.includes(key));
34
+ return Object.fromEntries(filteredEntries);
35
+ }
36
+
37
+ static set(object, path, value) {
21
38
  if (!Array.isArray(path)) path = Lodash.toPath(path);
22
- const result = path.reduce((previousValue, currentValue, currentIndex) => {
23
- if (currentIndex === path.length - 1) {
24
- delete previousValue[currentValue];
25
- return true;
26
- }
27
- return Object(previousValue)[currentValue];
28
- }, object);
29
- return result;
39
+ path.slice(0, -1).reduce((previousValue, currentValue, currentIndex) => (Object(previousValue[currentValue]) === previousValue[currentValue] ? previousValue[currentValue] : (previousValue[currentValue] = /^\d+$/.test(path[currentIndex + 1]) ? [] : {})), object)[path[path.length - 1]] = value;
40
+ return object;
30
41
  }
31
42
 
32
43
  static toPath(value) {
@@ -36,17 +47,6 @@ export class Lodash {
36
47
  .filter(Boolean);
37
48
  }
38
49
 
39
- static escape(string) {
40
- const map = {
41
- "&": "&amp;",
42
- "<": "&lt;",
43
- ">": "&gt;",
44
- '"': "&quot;",
45
- "'": "&#39;",
46
- };
47
- return string.replace(/[&<>"']/g, m => map[m]);
48
- }
49
-
50
50
  static unescape(string) {
51
51
  const map = {
52
52
  "&amp;": "&",
@@ -57,4 +57,16 @@ export class Lodash {
57
57
  };
58
58
  return string.replace(/&amp;|&lt;|&gt;|&quot;|&#39;/g, m => map[m]);
59
59
  }
60
+
61
+ static unset(object = {}, path = "") {
62
+ if (!Array.isArray(path)) path = Lodash.toPath(path);
63
+ const result = path.reduce((previousValue, currentValue, currentIndex) => {
64
+ if (currentIndex === path.length - 1) {
65
+ delete previousValue[currentValue];
66
+ return true;
67
+ }
68
+ return Object(previousValue)[currentValue];
69
+ }, object);
70
+ return result;
71
+ }
60
72
  }