@nsnanocat/util 1.7.7 → 1.8.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/polyfill/Lodash.mjs +35 -23
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.0"
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
  }