@nickyzj2023/utils 1.0.9 → 1.0.11
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/README.md +35 -35
- package/bun.lock +73 -73
- package/dist/index.js +1 -1
- package/dist/network.d.ts +3 -2
- package/docs/functions/fetcher.html +4 -3
- package/docs/functions/isObject.html +1 -1
- package/docs/functions/timeLog.html +1 -1
- package/docs/functions/to.html +1 -1
- package/docs/functions/withCache.html +3 -3
- package/package.json +22 -22
- package/src/dom.ts +10 -10
- package/src/hoc.ts +109 -109
- package/src/index.ts +4 -4
- package/src/is.ts +8 -8
- package/src/lru-cache.ts +50 -50
- package/src/network.ts +101 -100
- package/tsconfig.json +32 -32
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
</div></li><li><span><span class="tsd-kind-parameter">ttlSeconds</span>: <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol"> = -1</span></span><div class="tsd-comment tsd-typography"><p>以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖</p>
|
|
6
6
|
</div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-symbol">{</span> <span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span> <span class="tsd-kind-call-signature">updateTtl</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">:</span> <span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">;</span> <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a> <span class="tsd-signature-symbol">}</span></h4><p>返回包装后的函数,以及缓存相关的额外方法</p>
|
|
7
7
|
<ul class="tsd-parameters"><li class="tsd-parameter-signature"><ul class="tsd-signatures"><li class="tsd-signature"><span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">...</span><span class="tsd-kind-parameter">args</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></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-signature-symbol">...</span><span class="tsd-kind-parameter">args</span>: <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheargs">Args</a></span></li></ul></div><h4 class="tsd-returns-title">Returns <a class="tsd-signature-type tsd-kind-type-parameter" href="#withcacheresult">Result</a></h4></li></ul></li><li class="tsd-parameter"><h5><span class="tsd-kind-method">clear</span><span class="tsd-signature-symbol">:</span> function</h5><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link"><span class="tsd-kind-call-signature">clear</span><span class="tsd-signature-symbol">()</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">void</span></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>手动清除缓存</p>
|
|
8
|
-
</div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in hoc.ts:90</li></ul></aside></div></li></ul></li><li class="tsd-parameter"><h5><span class="tsd-kind-method">updateTtl</span><span class="tsd-signature-symbol">:</span> function</h5><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link"><span class="tsd-kind-call-signature">updateTtl</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">:</span> <span class="tsd-signature-type">void</span></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>更新 TTL,同时刷新所有未过期缓存的时间</p>
|
|
9
|
-
</div><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><aside class="tsd-sources"><ul><li>Defined in hoc.ts:93</li></ul></aside></div></li></ul></li></ul><div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-0">// 异步函数示例</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">fetchData</span><span class="hl-2"> = </span><span class="hl-1">withCache</span><span class="hl-2">(</span><span class="hl-6">async</span><span class="hl-2"> (</span><span class="hl-5">setTtl</span><span class="hl-2">, </span><span class="hl-5">url</span><span class="hl-2">: </span><span class="hl-8">string</span><span class="hl-2">) </span><span class="hl-6">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">data</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetch</span><span class="hl-2">(</span><span class="hl-5">url</span><span class="hl-2">).</span><span class="hl-1">then</span><span class="hl-2">((</span><span class="hl-5">res</span><span class="hl-2">) </span><span class="hl-6">=></span><span class="hl-2"> </span><span class="hl-5">res</span><span class="hl-2">.</span><span class="hl-1">json</span><span class="hl-2">());</span><br/><span class="hl-2"> </span><span class="hl-1">setTtl</span><span class="hl-2">(</span><span class="hl-5">data</span><span class="hl-2">.</span><span class="hl-5">expiresIn</span><span class="hl-2">); </span><span class="hl-0">// 根据实际情况调整过期时间</span><br/><span class="hl-2"> </span><span class="hl-4">return</span><span class="hl-2"> </span><span class="hl-5">data</span><span class="hl-2">;</span><br/><span class="hl-2">});</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">clear</span><span class="hl-2">(); </span><span class="hl-0">// urlA 和 urlB 的缓存都被清除</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 重新请求数据</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 重新请求数据</span><br/><br/><span class="hl-0">// 缓存过期前</span><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">updateTtl</span><span class="hl-2">(</span><span class="hl-9">180</span><span class="hl-2">); </span><span class="hl-0">// 更新 ttl 并为所有未过期缓存续期</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span>
|
|
8
|
+
</div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/f2adbe8702c0f8095be72e5e5f12883be8bf50d8/src/hoc.ts#L90">hoc.ts:90</a></li></ul></aside></div></li></ul></li><li class="tsd-parameter"><h5><span class="tsd-kind-method">updateTtl</span><span class="tsd-signature-symbol">:</span> function</h5><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link"><span class="tsd-kind-call-signature">updateTtl</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">:</span> <span class="tsd-signature-type">void</span></div><div class="tsd-description"><div class="tsd-comment tsd-typography"><p>更新 TTL,同时刷新所有未过期缓存的时间</p>
|
|
9
|
+
</div><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><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/f2adbe8702c0f8095be72e5e5f12883be8bf50d8/src/hoc.ts#L93">hoc.ts:93</a></li></ul></aside></div></li></ul></li></ul><div class="tsd-comment tsd-typography"><div class="tsd-tag-example"><h4 class="tsd-anchor-link" id="example">Example<a href="#example" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><pre><code class="ts"><span class="hl-0">// 异步函数示例</span><br/><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">fetchData</span><span class="hl-2"> = </span><span class="hl-1">withCache</span><span class="hl-2">(</span><span class="hl-6">async</span><span class="hl-2"> (</span><span class="hl-5">setTtl</span><span class="hl-2">, </span><span class="hl-5">url</span><span class="hl-2">: </span><span class="hl-8">string</span><span class="hl-2">) </span><span class="hl-6">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">data</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetch</span><span class="hl-2">(</span><span class="hl-5">url</span><span class="hl-2">).</span><span class="hl-1">then</span><span class="hl-2">((</span><span class="hl-5">res</span><span class="hl-2">) </span><span class="hl-6">=></span><span class="hl-2"> </span><span class="hl-5">res</span><span class="hl-2">.</span><span class="hl-1">json</span><span class="hl-2">());</span><br/><span class="hl-2"> </span><span class="hl-1">setTtl</span><span class="hl-2">(</span><span class="hl-5">data</span><span class="hl-2">.</span><span class="hl-5">expiresIn</span><span class="hl-2">); </span><span class="hl-0">// 根据实际情况调整过期时间</span><br/><span class="hl-2"> </span><span class="hl-4">return</span><span class="hl-2"> </span><span class="hl-5">data</span><span class="hl-2">;</span><br/><span class="hl-2">});</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">);</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">clear</span><span class="hl-2">(); </span><span class="hl-0">// urlA 和 urlB 的缓存都被清除</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 重新请求数据</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 重新请求数据</span><br/><br/><span class="hl-0">// 缓存过期前</span><br/><span class="hl-5">fetchData</span><span class="hl-2">.</span><span class="hl-1">updateTtl</span><span class="hl-2">(</span><span class="hl-9">180</span><span class="hl-2">); </span><span class="hl-0">// 更新 ttl 并为所有未过期缓存续期</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlA</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">fetchData</span><span class="hl-2">(</span><span class="hl-5">urlB</span><span class="hl-2">); </span><span class="hl-0">// 使用缓存结果</span>
|
|
10
10
|
</code><button type="button">Copy</button></pre>
|
|
11
11
|
|
|
12
|
-
</div></div><aside class="tsd-sources"><ul><li>Defined in hoc.ts:42</li></ul></aside></div></li></ul></section></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>
|
|
12
|
+
</div></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/Nickyzj628/utils/blob/f2adbe8702c0f8095be72e5e5f12883be8bf50d8/src/hoc.ts#L42">hoc.ts:42</a></li></ul></aside></div></li></ul></section></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>
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nickyzj2023/utils",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"module": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"docs": "typedoc src/index.ts",
|
|
9
|
-
"build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
|
|
10
|
-
},
|
|
11
|
-
"devDependencies": {
|
|
12
|
-
"@types/bun": "latest",
|
|
13
|
-
"typedoc": "^0.28.14"
|
|
14
|
-
},
|
|
15
|
-
"peerDependencies": {
|
|
16
|
-
"typescript": "^5"
|
|
17
|
-
},
|
|
18
|
-
"repository": {
|
|
19
|
-
"type": "git",
|
|
20
|
-
"url": "https://github.com/Nickyzj628/utils.git"
|
|
21
|
-
}
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@nickyzj2023/utils",
|
|
3
|
+
"version": "1.0.11",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"docs": "typedoc src/index.ts",
|
|
9
|
+
"build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/bun": "latest",
|
|
13
|
+
"typedoc": "^0.28.14"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"typescript": "^5"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/Nickyzj628/utils.git"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/dom.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 附带时间的 console.log
|
|
3
|
-
* @param args
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* timeLog("Hello", "World"); // 14:30:00 Hello World
|
|
7
|
-
*/
|
|
8
|
-
export const timeLog = (...args: any[]) => {
|
|
9
|
-
console.log(`${new Date().toLocaleTimeString()}`, ...args);
|
|
10
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 附带时间的 console.log
|
|
3
|
+
* @param args
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* timeLog("Hello", "World"); // 14:30:00 Hello World
|
|
7
|
+
*/
|
|
8
|
+
export const timeLog = (...args: any[]) => {
|
|
9
|
+
console.log(`${new Date().toLocaleTimeString()}`, ...args);
|
|
10
|
+
};
|
package/src/hoc.ts
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
type SetTtl = (seconds: number) => void;
|
|
2
|
-
|
|
3
|
-
type CacheEntry = {
|
|
4
|
-
value: any;
|
|
5
|
-
expiresAt: number; // 时间戳(毫秒)
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 创建一个带缓存的高阶函数
|
|
10
|
-
*
|
|
11
|
-
* @template Args 被包装函数的参数类型数组
|
|
12
|
-
* @template Result 被包装函数的返回类型
|
|
13
|
-
*
|
|
14
|
-
* @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
|
|
15
|
-
* @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
|
|
16
|
-
*
|
|
17
|
-
* @returns 返回包装后的函数,以及缓存相关的额外方法
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // 异步函数示例
|
|
21
|
-
* const fetchData = withCache(async (setTtl, url: string) => {
|
|
22
|
-
* const data = await fetch(url).then((res) => res.json());
|
|
23
|
-
* setTtl(data.expiresIn); // 根据实际情况调整过期时间
|
|
24
|
-
* return data;
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* await fetchData(urlA);
|
|
28
|
-
* await fetchData(urlA); // 使用缓存结果
|
|
29
|
-
|
|
30
|
-
* await fetchData(urlB);
|
|
31
|
-
* await fetchData(urlB); // 使用缓存结果
|
|
32
|
-
*
|
|
33
|
-
* fetchData.clear(); // urlA 和 urlB 的缓存都被清除
|
|
34
|
-
* await fetchData(urlA); // 重新请求数据
|
|
35
|
-
* await fetchData(urlB); // 重新请求数据
|
|
36
|
-
*
|
|
37
|
-
* // 缓存过期前
|
|
38
|
-
* fetchData.updateTtl(180); // 更新 ttl 并为所有未过期缓存续期
|
|
39
|
-
* await fetchData(urlA); // 使用缓存结果
|
|
40
|
-
* await fetchData(urlB); // 使用缓存结果
|
|
41
|
-
*/
|
|
42
|
-
export const withCache = <Args extends any[], Result>(
|
|
43
|
-
fn: (setTtl: SetTtl, ...args: Args) => Result,
|
|
44
|
-
ttlSeconds = -1,
|
|
45
|
-
) => {
|
|
46
|
-
const cache = new Map<string, CacheEntry>();
|
|
47
|
-
|
|
48
|
-
const wrapped = (...args: Args): Result => {
|
|
49
|
-
const key = JSON.stringify(args);
|
|
50
|
-
const now = Date.now();
|
|
51
|
-
const entry = cache.get(key);
|
|
52
|
-
|
|
53
|
-
// 命中缓存且未过期
|
|
54
|
-
if (entry && now < entry.expiresAt) {
|
|
55
|
-
return entry.value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const setTtl: SetTtl = (seconds) => (ttlSeconds = seconds);
|
|
59
|
-
const result = fn(setTtl, ...args);
|
|
60
|
-
|
|
61
|
-
// 异步函数:缓存 Promise 的 resolved 值
|
|
62
|
-
if (result instanceof Promise) {
|
|
63
|
-
const promise = result.then((resolved) => {
|
|
64
|
-
cache.set(key, {
|
|
65
|
-
value: resolved,
|
|
66
|
-
expiresAt: Date.now() + ttlSeconds * 1000,
|
|
67
|
-
});
|
|
68
|
-
return resolved;
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// 将 promise 先塞进去避免重复请求
|
|
72
|
-
cache.set(key, {
|
|
73
|
-
value: promise,
|
|
74
|
-
expiresAt: now + ttlSeconds * 1000,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
return promise as Result;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 同步函数缓存
|
|
81
|
-
cache.set(key, {
|
|
82
|
-
value: result,
|
|
83
|
-
expiresAt: now + ttlSeconds * 1000,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
return result;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/** 手动清除缓存 */
|
|
90
|
-
wrapped.clear = () => cache.clear();
|
|
91
|
-
|
|
92
|
-
/** 更新 TTL,同时刷新所有未过期缓存的时间 */
|
|
93
|
-
wrapped.updateTtl = (seconds: number) => {
|
|
94
|
-
// 更新默认 TTL
|
|
95
|
-
ttlSeconds = seconds;
|
|
96
|
-
|
|
97
|
-
// 给未过期缓存续期
|
|
98
|
-
const now = Date.now();
|
|
99
|
-
const newExpiresAt = now + seconds * 1000;
|
|
100
|
-
for (const [key, entry] of cache.entries()) {
|
|
101
|
-
if (entry.expiresAt > now) {
|
|
102
|
-
entry.expiresAt = newExpiresAt;
|
|
103
|
-
cache.set(key, entry);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
return wrapped;
|
|
109
|
-
};
|
|
1
|
+
type SetTtl = (seconds: number) => void;
|
|
2
|
+
|
|
3
|
+
type CacheEntry = {
|
|
4
|
+
value: any;
|
|
5
|
+
expiresAt: number; // 时间戳(毫秒)
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 创建一个带缓存的高阶函数
|
|
10
|
+
*
|
|
11
|
+
* @template Args 被包装函数的参数类型数组
|
|
12
|
+
* @template Result 被包装函数的返回类型
|
|
13
|
+
*
|
|
14
|
+
* @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
|
|
15
|
+
* @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
|
|
16
|
+
*
|
|
17
|
+
* @returns 返回包装后的函数,以及缓存相关的额外方法
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // 异步函数示例
|
|
21
|
+
* const fetchData = withCache(async (setTtl, url: string) => {
|
|
22
|
+
* const data = await fetch(url).then((res) => res.json());
|
|
23
|
+
* setTtl(data.expiresIn); // 根据实际情况调整过期时间
|
|
24
|
+
* return data;
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* await fetchData(urlA);
|
|
28
|
+
* await fetchData(urlA); // 使用缓存结果
|
|
29
|
+
|
|
30
|
+
* await fetchData(urlB);
|
|
31
|
+
* await fetchData(urlB); // 使用缓存结果
|
|
32
|
+
*
|
|
33
|
+
* fetchData.clear(); // urlA 和 urlB 的缓存都被清除
|
|
34
|
+
* await fetchData(urlA); // 重新请求数据
|
|
35
|
+
* await fetchData(urlB); // 重新请求数据
|
|
36
|
+
*
|
|
37
|
+
* // 缓存过期前
|
|
38
|
+
* fetchData.updateTtl(180); // 更新 ttl 并为所有未过期缓存续期
|
|
39
|
+
* await fetchData(urlA); // 使用缓存结果
|
|
40
|
+
* await fetchData(urlB); // 使用缓存结果
|
|
41
|
+
*/
|
|
42
|
+
export const withCache = <Args extends any[], Result>(
|
|
43
|
+
fn: (setTtl: SetTtl, ...args: Args) => Result,
|
|
44
|
+
ttlSeconds = -1,
|
|
45
|
+
) => {
|
|
46
|
+
const cache = new Map<string, CacheEntry>();
|
|
47
|
+
|
|
48
|
+
const wrapped = (...args: Args): Result => {
|
|
49
|
+
const key = JSON.stringify(args);
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
const entry = cache.get(key);
|
|
52
|
+
|
|
53
|
+
// 命中缓存且未过期
|
|
54
|
+
if (entry && now < entry.expiresAt) {
|
|
55
|
+
return entry.value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const setTtl: SetTtl = (seconds) => (ttlSeconds = seconds);
|
|
59
|
+
const result = fn(setTtl, ...args);
|
|
60
|
+
|
|
61
|
+
// 异步函数:缓存 Promise 的 resolved 值
|
|
62
|
+
if (result instanceof Promise) {
|
|
63
|
+
const promise = result.then((resolved) => {
|
|
64
|
+
cache.set(key, {
|
|
65
|
+
value: resolved,
|
|
66
|
+
expiresAt: Date.now() + ttlSeconds * 1000,
|
|
67
|
+
});
|
|
68
|
+
return resolved;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 将 promise 先塞进去避免重复请求
|
|
72
|
+
cache.set(key, {
|
|
73
|
+
value: promise,
|
|
74
|
+
expiresAt: now + ttlSeconds * 1000,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return promise as Result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 同步函数缓存
|
|
81
|
+
cache.set(key, {
|
|
82
|
+
value: result,
|
|
83
|
+
expiresAt: now + ttlSeconds * 1000,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** 手动清除缓存 */
|
|
90
|
+
wrapped.clear = () => cache.clear();
|
|
91
|
+
|
|
92
|
+
/** 更新 TTL,同时刷新所有未过期缓存的时间 */
|
|
93
|
+
wrapped.updateTtl = (seconds: number) => {
|
|
94
|
+
// 更新默认 TTL
|
|
95
|
+
ttlSeconds = seconds;
|
|
96
|
+
|
|
97
|
+
// 给未过期缓存续期
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
const newExpiresAt = now + seconds * 1000;
|
|
100
|
+
for (const [key, entry] of cache.entries()) {
|
|
101
|
+
if (entry.expiresAt > now) {
|
|
102
|
+
entry.expiresAt = newExpiresAt;
|
|
103
|
+
cache.set(key, entry);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return wrapped;
|
|
109
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from "./dom";
|
|
2
|
-
export * from "./hoc";
|
|
3
|
-
export * from "./is";
|
|
4
|
-
export * from "./network";
|
|
1
|
+
export * from "./dom";
|
|
2
|
+
export * from "./hoc";
|
|
3
|
+
export * from "./is";
|
|
4
|
+
export * from "./network";
|
package/src/is.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 检测传入的值是否为**普通对象**
|
|
3
|
-
* @param value
|
|
4
|
-
* @returns {Boolean} 如果是普通对象,返回 true,否则返回 false
|
|
5
|
-
*/
|
|
6
|
-
export const isObject = (value: any): value is object => {
|
|
7
|
-
return value?.constructor === Object;
|
|
8
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 检测传入的值是否为**普通对象**
|
|
3
|
+
* @param value
|
|
4
|
+
* @returns {Boolean} 如果是普通对象,返回 true,否则返回 false
|
|
5
|
+
*/
|
|
6
|
+
export const isObject = (value: any): value is object => {
|
|
7
|
+
return value?.constructor === Object;
|
|
8
|
+
};
|
package/src/lru-cache.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 简易 LRU 缓存
|
|
3
|
-
* @example
|
|
4
|
-
* const cache = new LRUCache<string, number>(2);
|
|
5
|
-
* cache.set("a", 1);
|
|
6
|
-
* cache.set("b", 2);
|
|
7
|
-
* cache.set("c", 3); // 缓存已满,a 被淘汰
|
|
8
|
-
* cache.get("a"); // undefined
|
|
9
|
-
*/
|
|
10
|
-
class LRUCache<K, V> {
|
|
11
|
-
private cache: Map<K, V>;
|
|
12
|
-
private maxSize: number;
|
|
13
|
-
|
|
14
|
-
constructor(maxSize = 10) {
|
|
15
|
-
this.cache = new Map();
|
|
16
|
-
this.maxSize = maxSize;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get(key: K): V | undefined {
|
|
20
|
-
const value = this.cache.get(key);
|
|
21
|
-
if (!value) {
|
|
22
|
-
return undefined;
|
|
23
|
-
}
|
|
24
|
-
// 重置缓存顺序
|
|
25
|
-
this.cache.delete(key);
|
|
26
|
-
this.cache.set(key, value);
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
set(key: K, value: V) {
|
|
31
|
-
// 刷新缓存
|
|
32
|
-
if (this.cache.has(key)) {
|
|
33
|
-
this.cache.delete(key);
|
|
34
|
-
}
|
|
35
|
-
// 删除最旧的缓存
|
|
36
|
-
else if (this.cache.size >= this.maxSize) {
|
|
37
|
-
const oldestKey = [...this.cache.keys()][0];
|
|
38
|
-
if (oldestKey) {
|
|
39
|
-
this.cache.delete(oldestKey);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
this.cache.set(key, value);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
has(key: K) {
|
|
46
|
-
return this.cache.has(key);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default LRUCache;
|
|
1
|
+
/**
|
|
2
|
+
* 简易 LRU 缓存
|
|
3
|
+
* @example
|
|
4
|
+
* const cache = new LRUCache<string, number>(2);
|
|
5
|
+
* cache.set("a", 1);
|
|
6
|
+
* cache.set("b", 2);
|
|
7
|
+
* cache.set("c", 3); // 缓存已满,a 被淘汰
|
|
8
|
+
* cache.get("a"); // undefined
|
|
9
|
+
*/
|
|
10
|
+
class LRUCache<K, V> {
|
|
11
|
+
private cache: Map<K, V>;
|
|
12
|
+
private maxSize: number;
|
|
13
|
+
|
|
14
|
+
constructor(maxSize = 10) {
|
|
15
|
+
this.cache = new Map();
|
|
16
|
+
this.maxSize = maxSize;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get(key: K): V | undefined {
|
|
20
|
+
const value = this.cache.get(key);
|
|
21
|
+
if (!value) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
// 重置缓存顺序
|
|
25
|
+
this.cache.delete(key);
|
|
26
|
+
this.cache.set(key, value);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
set(key: K, value: V) {
|
|
31
|
+
// 刷新缓存
|
|
32
|
+
if (this.cache.has(key)) {
|
|
33
|
+
this.cache.delete(key);
|
|
34
|
+
}
|
|
35
|
+
// 删除最旧的缓存
|
|
36
|
+
else if (this.cache.size >= this.maxSize) {
|
|
37
|
+
const oldestKey = [...this.cache.keys()][0];
|
|
38
|
+
if (oldestKey) {
|
|
39
|
+
this.cache.delete(oldestKey);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.cache.set(key, value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
has(key: K) {
|
|
46
|
+
return this.cache.has(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default LRUCache;
|