my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
2 lines (1 loc) • 2.5 kB
JavaScript
import{ErrorHandler as t,UniAppToolsError as r}from"./core.js";const n=t.getInstance();function e(t){if(null===t||"object"!=typeof t)return t;try{return"undefined"!=typeof structuredClone?structuredClone(t):JSON.parse(JSON.stringify(t))}catch(t){const e=t instanceof Error?t.message:String(t);throw n.handleError(new r("DEEP_CLONE_ERROR",`深拷贝失败: ${e}`,"utils"),"utils"),new Error(`深拷贝失败: ${e}`)}}function o(t,r){if(!t||"object"!=typeof t||Array.isArray(t))return e(r||{});if(!r||"object"!=typeof r||Array.isArray(r))return e(t);const n=e(t);return function t(r,n){for(const o in n)if(Object.prototype.hasOwnProperty.call(n,o)){const c=n[o],a=r[o];c&&"object"==typeof c&&!Array.isArray(c)&&a&&"object"==typeof a&&!Array.isArray(a)?t(a,c):r[o]=e(c)}}(n,r),n}function c(t,r){if(!t||"object"!=typeof t||Array.isArray(t))return t;if(!r||0===Object.keys(r).length)return t;const n={...t};for(const e of Object.keys(t))r&&"object"==typeof r&&Object.prototype.hasOwnProperty.call(r,e)&&(n[e]=r[e]);return n}function a(t,r,n=!1){let e,o=null;const c=function(...c){const a=n&&!o;return o&&clearTimeout(o),o=setTimeout(()=>{o=null,n||(e=t.apply(this,c))},r),a&&(e=t.apply(this,c)),e};return c.cancel=()=>{o&&(clearTimeout(o),o=null)},c}function i(t,r,n={}){let e,o=null,c=0;const{leading:a=!0,trailing:i=!0}=n,s=function(...n){const s=Date.now();c||a||(c=s);const l=r-(s-c);return l<=0||l>r?(o&&(clearTimeout(o),o=null),c=s,e=t.apply(this,n)):!o&&i&&(o=setTimeout(()=>{c=a?Date.now():0,o=null,e=t.apply(this,n)},l)),e};return s.cancel=()=>{o&&(clearTimeout(o),o=null),c=0},s}const s=t=>{try{if("undefined"==typeof window||!window.location)throw new Error("不在 H5 浏览器环境中");const r=new URLSearchParams(window.location.search),n={};return r.forEach((t,r)=>{try{n[r]=decodeURIComponent(t)}catch(e){n[r]=t}}),t?Object.prototype.hasOwnProperty.call(n,t)?n[t]:null:n}catch(e){return n.handleError(new r("GET_H5_URL_PARAMS_ERROR",`获取H5 URL参数失败: ${e instanceof Error?e.message:String(e)}`,"utils"),"utils"),t?null:{}}};function l(t){try{if("string"!=typeof t||!t.trim())throw new Error("URL不能为空");const r=new URL(t,"http://localhost"),n=r.pathname,e={};return r.searchParams.forEach((t,r)=>{e[r]=t}),{path:n,params:e}}catch(e){return n.handleError(new r("EXTRACT_URL_PARTS_ERROR",`无效的URL: ${t}`,"utils"),"utils"),{path:"",params:{}}}}export{a as debounce,e as deepClone,o as deepMerge,l as extractUrlParts,s as getH5UrlParams,c as mergeObjects,i as throttle};