@nickyzj2023/utils 1.0.17 → 1.0.18

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.
@@ -1 +1,174 @@
1
- <!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>SetTtl | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">SetTtl</a></li></ul><h1>Type Alias SetTtl</h1></div><div class="tsd-signature"><span class="tsd-kind-type-alias">SetTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=&gt;</span> <span class="tsd-signature-type">void</span></div><div class="tsd-type-declaration"><h4>Type Declaration</h4><ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature" id="__type"><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">seconds</span>: <span class="tsd-signature-type">number</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4></li></ul></li></ul></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/fc7053b0d526e6549fc80579c9997bc33c31a822/src/hoc.ts#L6">hoc.ts:6</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
1
+ <!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>SetTtl | @nickyzj2023/utils</title><meta name="description" content="Documentation for @nickyzj2023/utils"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script><style>
2
+ @media (prefers-color-scheme: dark) {
3
+ :root {
4
+ --md-sys-color-primary: #f6be46;
5
+ --md-sys-color-on-primary: #412d00;
6
+ --md-sys-color-primary-container: #5d4200;
7
+ --md-sys-color-on-primary-container: #ffdea3;
8
+ --md-sys-color-secondary: #d9c4a0;
9
+ --md-sys-color-on-secondary: #3b2f15;
10
+ --md-sys-color-secondary-container: #53452a;
11
+ --md-sys-color-on-secondary-container: #f6e0bb;
12
+ --md-sys-color-tertiary: #b2cfa8;
13
+ --md-sys-color-on-tertiary: #1e361a;
14
+ --md-sys-color-tertiary-container: #344d2f;
15
+ --md-sys-color-on-tertiary-container: #cdebc2;
16
+ --md-sys-color-error: #ffb4ab;
17
+ --md-sys-color-on-error: #690005;
18
+ --md-sys-color-error-container: #93000a;
19
+ --md-sys-color-on-error-container: #ffb4ab;
20
+ --md-sys-color-background: #1e1b16;
21
+ --md-sys-color-on-background: #e9e1d9;
22
+ --md-sys-color-surface: #1e1b16;
23
+ --md-sys-color-on-surface: #e9e1d9;
24
+ --md-sys-color-surface-variant: #4e4639;
25
+ --md-sys-color-on-surface-variant: #d1c5b4;
26
+ --md-sys-color-outline: #9a8f80;
27
+ --md-sys-color-outline-variant: #4e4639;
28
+ --md-sys-color-shadow: #000000;
29
+ --md-sys-color-scrim: #000000;
30
+ --md-sys-color-inverse-surface: #e9e1d9;
31
+ --md-sys-color-inverse-on-surface: #34302a;
32
+ --md-sys-color-inverse-primary: #7a5900;
33
+ --md-sys-color-surface-dim: #16130e;
34
+ --md-sys-color-surface-bright: #3d3933;
35
+ --md-sys-color-surface-container-lowest: #110e09;
36
+ --md-sys-color-surface-container-low: #1e1b16;
37
+ --md-sys-color-surface-container: #221f1a;
38
+ --md-sys-color-surface-container-high: #2d2924;
39
+ --md-sys-color-surface-container-highest: #38342e
40
+ }
41
+ }
42
+ :root[data-theme="dark"] {
43
+ --md-sys-color-primary: #f6be46;
44
+ --md-sys-color-on-primary: #412d00;
45
+ --md-sys-color-primary-container: #5d4200;
46
+ --md-sys-color-on-primary-container: #ffdea3;
47
+ --md-sys-color-secondary: #d9c4a0;
48
+ --md-sys-color-on-secondary: #3b2f15;
49
+ --md-sys-color-secondary-container: #53452a;
50
+ --md-sys-color-on-secondary-container: #f6e0bb;
51
+ --md-sys-color-tertiary: #b2cfa8;
52
+ --md-sys-color-on-tertiary: #1e361a;
53
+ --md-sys-color-tertiary-container: #344d2f;
54
+ --md-sys-color-on-tertiary-container: #cdebc2;
55
+ --md-sys-color-error: #ffb4ab;
56
+ --md-sys-color-on-error: #690005;
57
+ --md-sys-color-error-container: #93000a;
58
+ --md-sys-color-on-error-container: #ffb4ab;
59
+ --md-sys-color-background: #1e1b16;
60
+ --md-sys-color-on-background: #e9e1d9;
61
+ --md-sys-color-surface: #1e1b16;
62
+ --md-sys-color-on-surface: #e9e1d9;
63
+ --md-sys-color-surface-variant: #4e4639;
64
+ --md-sys-color-on-surface-variant: #d1c5b4;
65
+ --md-sys-color-outline: #9a8f80;
66
+ --md-sys-color-outline-variant: #4e4639;
67
+ --md-sys-color-shadow: #000000;
68
+ --md-sys-color-scrim: #000000;
69
+ --md-sys-color-inverse-surface: #e9e1d9;
70
+ --md-sys-color-inverse-on-surface: #34302a;
71
+ --md-sys-color-inverse-primary: #7a5900;
72
+ --md-sys-color-surface-dim: #16130e;
73
+ --md-sys-color-surface-bright: #3d3933;
74
+ --md-sys-color-surface-container-lowest: #110e09;
75
+ --md-sys-color-surface-container-low: #1e1b16;
76
+ --md-sys-color-surface-container: #221f1a;
77
+ --md-sys-color-surface-container-high: #2d2924;
78
+ --md-sys-color-surface-container-highest: #38342e
79
+ }
80
+
81
+ @media (prefers-color-scheme: light) {
82
+ :root {
83
+ --md-sys-color-primary: #7a5900;
84
+ --md-sys-color-on-primary: #ffffff;
85
+ --md-sys-color-primary-container: #ffdea3;
86
+ --md-sys-color-on-primary-container: #261900;
87
+ --md-sys-color-secondary: #6c5c3f;
88
+ --md-sys-color-on-secondary: #ffffff;
89
+ --md-sys-color-secondary-container: #f6e0bb;
90
+ --md-sys-color-on-secondary-container: #251a04;
91
+ --md-sys-color-tertiary: #4b6545;
92
+ --md-sys-color-on-tertiary: #ffffff;
93
+ --md-sys-color-tertiary-container: #cdebc2;
94
+ --md-sys-color-on-tertiary-container: #092007;
95
+ --md-sys-color-error: #ba1a1a;
96
+ --md-sys-color-on-error: #ffffff;
97
+ --md-sys-color-error-container: #ffdad6;
98
+ --md-sys-color-on-error-container: #410002;
99
+ --md-sys-color-background: #fffbff;
100
+ --md-sys-color-on-background: #1e1b16;
101
+ --md-sys-color-surface: #fffbff;
102
+ --md-sys-color-on-surface: #1e1b16;
103
+ --md-sys-color-surface-variant: #ede1cf;
104
+ --md-sys-color-on-surface-variant: #4e4639;
105
+ --md-sys-color-outline: #7f7667;
106
+ --md-sys-color-outline-variant: #d1c5b4;
107
+ --md-sys-color-shadow: #000000;
108
+ --md-sys-color-scrim: #000000;
109
+ --md-sys-color-inverse-surface: #34302a;
110
+ --md-sys-color-inverse-on-surface: #f8efe7;
111
+ --md-sys-color-inverse-primary: #f6be46;
112
+ --md-sys-color-surface-dim: #e1d9d0;
113
+ --md-sys-color-surface-bright: #fff8f2;
114
+ --md-sys-color-surface-container-lowest: #ffffff;
115
+ --md-sys-color-surface-container-low: #fbf2e9;
116
+ --md-sys-color-surface-container: #f5ede4;
117
+ --md-sys-color-surface-container-high: #efe7de;
118
+ --md-sys-color-surface-container-highest: #e9e1d9
119
+ }
120
+ }
121
+ :root[data-theme="light"] {
122
+ --md-sys-color-primary: #7a5900;
123
+ --md-sys-color-on-primary: #ffffff;
124
+ --md-sys-color-primary-container: #ffdea3;
125
+ --md-sys-color-on-primary-container: #261900;
126
+ --md-sys-color-secondary: #6c5c3f;
127
+ --md-sys-color-on-secondary: #ffffff;
128
+ --md-sys-color-secondary-container: #f6e0bb;
129
+ --md-sys-color-on-secondary-container: #251a04;
130
+ --md-sys-color-tertiary: #4b6545;
131
+ --md-sys-color-on-tertiary: #ffffff;
132
+ --md-sys-color-tertiary-container: #cdebc2;
133
+ --md-sys-color-on-tertiary-container: #092007;
134
+ --md-sys-color-error: #ba1a1a;
135
+ --md-sys-color-on-error: #ffffff;
136
+ --md-sys-color-error-container: #ffdad6;
137
+ --md-sys-color-on-error-container: #410002;
138
+ --md-sys-color-background: #fffbff;
139
+ --md-sys-color-on-background: #1e1b16;
140
+ --md-sys-color-surface: #fffbff;
141
+ --md-sys-color-on-surface: #1e1b16;
142
+ --md-sys-color-surface-variant: #ede1cf;
143
+ --md-sys-color-on-surface-variant: #4e4639;
144
+ --md-sys-color-outline: #7f7667;
145
+ --md-sys-color-outline-variant: #d1c5b4;
146
+ --md-sys-color-shadow: #000000;
147
+ --md-sys-color-scrim: #000000;
148
+ --md-sys-color-inverse-surface: #34302a;
149
+ --md-sys-color-inverse-on-surface: #f8efe7;
150
+ --md-sys-color-inverse-primary: #f6be46;
151
+ --md-sys-color-surface-dim: #e1d9d0;
152
+ --md-sys-color-surface-bright: #fff8f2;
153
+ --md-sys-color-surface-container-lowest: #ffffff;
154
+ --md-sys-color-surface-container-low: #fbf2e9;
155
+ --md-sys-color-surface-container: #f5ede4;
156
+ --md-sys-color-surface-container-high: #efe7de;
157
+ --md-sys-color-surface-container-highest: #e9e1d9
158
+ }
159
+ </style><link rel="stylesheet" href="../assets/material-style.css"/></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@nickyzj2023/utils</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">SetTtl</a></li></ul><h1>Type Alias SetTtl</h1></div><div class="tsd-signature"><span class="tsd-kind-type-alias">SetTtl</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span> <span class="tsd-signature-symbol">=&gt;</span> <span class="tsd-signature-type">void</span></div><div class="tsd-type-declaration"><h4>Type Declaration</h4><ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature" id="__type"><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">seconds</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">seconds</span>: <span class="tsd-signature-type">number</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4></li></ul></li></ul></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/f408a18bf8828de9cc8d925dac8de5602462d99c/src/hoc.ts#L6">hoc.ts:6</a></li></ul></aside></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@nickyzj2023/utils</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div><script>
160
+ try {
161
+ const generateLinkElement = document.querySelector(".tsd-generator a");
162
+ const link = document.createElement("a");
163
+ Object.assign(link, {
164
+ href: "https://github.com/dmnsgn/typedoc-material-theme",
165
+ target: "_blank",
166
+ rel: "noreferrer",
167
+ innerText: "typedoc-material-theme."
168
+ });
169
+ generateLinkElement.insertAdjacentElement("afterend", link);
170
+ generateLinkElement.insertAdjacentText("afterend", " with ");
171
+ } catch (error) {
172
+
173
+ }
174
+ </script></body></html>
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@nickyzj2023/utils",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "docs": "typedoc src/index.ts",
8
+ "docs": "typedoc src/index.ts --plugin typedoc-material-theme",
9
9
  "build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
10
10
  },
11
11
  "devDependencies": {
@@ -18,5 +18,9 @@
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "https://github.com/Nickyzj628/utils.git"
21
+ },
22
+ "dependencies": {
23
+ "es-toolkit": "^1.42.0",
24
+ "typedoc-material-theme": "^1.4.1"
21
25
  }
22
26
  }
package/src/is.ts CHANGED
@@ -1,19 +1,21 @@
1
- /**
2
- * 检测传入的值是否为**普通对象**
3
- * @returns 如果是普通对象,返回 true,否则返回 false
4
- */
5
- export const isObject = (value: any): value is object => {
6
- return value?.constructor === Object;
7
- };
8
-
9
- /**
10
- * 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
11
- * @returns 如果是原始值,返回 true,否则返回 false
12
- */
13
- export const isPrimitive = (value: any) => {
14
- return (
15
- value === null ||
16
- value === undefined ||
17
- (typeof value !== "object" && typeof value !== "function")
18
- );
19
- };
1
+ export type Primitive = number | string | boolean | symbol | bigint | undefined | null;
2
+
3
+ /**
4
+ * 检测传入的值是否为**普通对象**
5
+ * @returns 如果是普通对象,返回 true,否则返回 false
6
+ */
7
+ export const isObject = (value: any): value is object => {
8
+ return value?.constructor === Object;
9
+ };
10
+
11
+ /**
12
+ * 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
13
+ * @returns 如果是原始值,返回 true,否则返回 false
14
+ */
15
+ export const isPrimitive = (value: any): value is Primitive => {
16
+ return (
17
+ value === null ||
18
+ value === undefined ||
19
+ (typeof value !== "object" && typeof value !== "function")
20
+ );
21
+ };
package/src/object.ts CHANGED
@@ -1,52 +1,165 @@
1
- import { isObject, isPrimitive } from "./is";
2
-
3
- /**
4
- * 深度合并两个对象,规则如下:
5
- * 1. 原始值覆盖:如果两个值都是原始类型(string、number、boolean、null、undefined、symbol、bigint),则用后者覆盖;
6
- * 2. 数组拼接:如果两个值都是数组,则拼接为大数组;
7
- * 3. 对象递归合并:如果两个值都是对象,则进行递归深度合并;
8
- * 4. 类型不匹配覆盖:如果两个值的类型不同,则用后者覆盖;
9
- * 5. 独有属性保留:如果字段只存在于其中一个对象中,则保留它。
10
- *
11
- * @template T 第一个对象
12
- * @template U 第二个对象
13
- * @param {T} obj1 要合并的第一个对象,相同字段可能会被 obj2 覆盖
14
- * @param {U} obj2 要合并的第二个对象
15
- */
16
- export const mergeObjects = <
17
- T extends Record<string, any>,
18
- U extends Record<string, any>,
19
- >(
20
- obj1: T,
21
- obj2: U,
22
- ) => {
23
- const result: Record<string, any> = { ...obj1 };
24
-
25
- for (const key of Object.keys(obj2)) {
26
- const val1 = result[key];
27
- const val2 = obj2[key];
28
-
29
- // 如果都是原始值,则用后者覆盖
30
- if (isPrimitive(val1) && isPrimitive(val2)) {
31
- result[key] = val2;
32
- continue;
33
- }
34
-
35
- // 如果都是数组,则拼接为大数组
36
- if (Array.isArray(val1) && Array.isArray(val2)) {
37
- result[key] = val1.concat(val2);
38
- continue;
39
- }
40
-
41
- // 如果都是对象,则深度递归合并
42
- if (isObject(val1) && isObject(val2)) {
43
- result[key] = mergeObjects(val1, val2);
44
- continue;
45
- }
46
-
47
- // 如果类型不同,则用后者覆盖
48
- result[key] = val2;
49
- }
50
-
51
- return result as T & U;
52
- };
1
+ import { isObject, isPrimitive } from "./is";
2
+
3
+ /**
4
+ * 深度合并两个对象,规则如下:
5
+ * 1. 原始值覆盖:如果两个值都是原始类型(string、number、boolean、null、undefined、symbol、bigint),则用后者覆盖;
6
+ * 2. 数组拼接:如果两个值都是数组,则拼接为大数组;
7
+ * 3. 对象递归合并:如果两个值都是对象,则进行递归深度合并;
8
+ * 4. 类型不匹配覆盖:如果两个值的类型不同,则用后者覆盖;
9
+ * 5. 独有属性保留:如果字段只存在于其中一个对象中,则保留它。
10
+ *
11
+ * @template T 第一个对象
12
+ * @template U 第二个对象
13
+ * @param {T} obj1 要合并的第一个对象,相同字段可能会被 obj2 覆盖
14
+ * @param {U} obj2 要合并的第二个对象
15
+ */
16
+ export const mergeObjects = <
17
+ T extends Record<string, any>,
18
+ U extends Record<string, any>,
19
+ >(
20
+ obj1: T,
21
+ obj2: U,
22
+ ) => {
23
+ const result: Record<string, any> = { ...obj1 };
24
+
25
+ for (const key of Object.keys(obj2)) {
26
+ const val1 = result[key];
27
+ const val2 = obj2[key];
28
+
29
+ // 如果都是原始值,则用后者覆盖
30
+ if (isPrimitive(val1) && isPrimitive(val2)) {
31
+ result[key] = val2;
32
+ continue;
33
+ }
34
+
35
+ // 如果都是数组,则拼接为大数组
36
+ if (Array.isArray(val1) && Array.isArray(val2)) {
37
+ result[key] = val1.concat(val2);
38
+ continue;
39
+ }
40
+
41
+ // 如果都是对象,则深度递归合并
42
+ if (isObject(val1) && isObject(val2)) {
43
+ result[key] = mergeObjects(val1, val2);
44
+ continue;
45
+ }
46
+
47
+ // 如果类型不同,则用后者覆盖
48
+ result[key] = val2;
49
+ }
50
+
51
+ return result as T & U;
52
+ };
53
+
54
+ // --- mapKeys ---
55
+
56
+ // 这里的 DeepMapKeys 只能承诺 key 是 string,无法推断出 key 的具体字面量变化
57
+ // 因为 TS 不支持根据任意函数反推 Key 的字面量类型
58
+ export type DeepMapKeys<T> =
59
+ T extends Array<infer U>
60
+ ? Array<DeepMapKeys<U>>
61
+ : T extends object
62
+ ? { [key: string]: DeepMapKeys<T[keyof T]> }
63
+ : T;
64
+
65
+ /**
66
+ * 递归处理对象里的 key
67
+ *
68
+ * @remarks
69
+ * 无法完整推导出类型,只能做到有递归,key 全为 string,value 为同层级的所有类型的联合
70
+ *
71
+ * @template T 要转换的对象
72
+ *
73
+ * @example
74
+ * const obj = { a: { b: 1 } };
75
+ * const result = mapKeys(obj, (key) => key.toUpperCase());
76
+ * console.log(result); // { A: { B: 1 } }
77
+ */
78
+ export const mapKeys = <T>(
79
+ obj: T,
80
+ getNewKey: (key: string) => string,
81
+ ): DeepMapKeys<T> => {
82
+ // 递归处理数组
83
+ if (Array.isArray(obj)) {
84
+ return obj.map((item) => mapKeys(item, getNewKey)) as any;
85
+ }
86
+
87
+ // 处理普通对象
88
+ if (isObject(obj)) {
89
+ const keys = Object.keys(obj);
90
+ return keys.reduce(
91
+ (result, key) => {
92
+ const newKey = getNewKey(key);
93
+ const value = (obj as any)[key];
94
+ result[newKey] = mapKeys(value, getNewKey);
95
+ return result;
96
+ },
97
+ {} as Record<string, any>,
98
+ ) as DeepMapKeys<T>;
99
+ }
100
+
101
+ // 处理非数组/对象
102
+ return obj as any;
103
+ };
104
+
105
+ // --- mapValues ---
106
+
107
+ // 这里的 DeepMapValues 尝试保留 key,但将 value 类型替换为 R
108
+ // 注意:如果原 value 是对象,我们递归处理结构,而不是把整个对象变成 R
109
+ export type DeepMapValues<T, R> =
110
+ T extends Array<infer U>
111
+ ? Array<DeepMapValues<U, R>>
112
+ : T extends object
113
+ ? { [K in keyof T]: T[K] extends object ? DeepMapValues<T[K], R> : R }
114
+ : R;
115
+
116
+ /**
117
+ * 递归处理对象里的 value
118
+ *
119
+ * @remarks
120
+ * 无法完整推导出类型,所有 value 最终都会变为 any
121
+ *
122
+ * @template T 要转换的对象
123
+ * @template R 转换后的值类型,为 any,无法进一步推导
124
+ *
125
+ * @example
126
+ * const obj = { a: 1, b: { c: 2 } };
127
+ * const result = mapValues(obj, (value, key) => isPrimitive(value) ? value + 1 : value);
128
+ * console.log(result); // { a: 2, b: { c: 3 } }
129
+ */
130
+ export const mapValues = <T, R = any>(
131
+ obj: T,
132
+ getNewValue: (value: any, key: string | number) => R,
133
+ ): DeepMapValues<T, R> => {
134
+ // 处理数组
135
+ if (Array.isArray(obj)) {
136
+ return obj.map((item, index) => {
137
+ // 如果元素是对象,则递归处理
138
+ if (isObject(item)) {
139
+ return mapValues(item, getNewValue);
140
+ }
141
+ // 如果元素是原始值,则直接应用 getNewValue(此时 key 为数组下标)
142
+ return getNewValue(item, index);
143
+ }) as any;
144
+ }
145
+
146
+ // 处理普通对象
147
+ if (isObject(obj)) {
148
+ const keys = Object.keys(obj);
149
+ return keys.reduce((result, key) => {
150
+ const value = (obj as any)[key];
151
+ // 如果值为对象或数组,则递归处理
152
+ if (!isPrimitive(value)) {
153
+ result[key] = mapValues(value, getNewValue);
154
+ }
155
+ // 否则直接应用 getNewValue
156
+ else {
157
+ result[key] = getNewValue(value, key);
158
+ }
159
+ return result;
160
+ }, {} as any) as DeepMapValues<T, R>;
161
+ }
162
+
163
+ // 处理非数组/对象
164
+ return obj as any;
165
+ };