@bassist/utils
Advanced tools
Comparing version 0.2.0 to 0.3.0
/** | ||
* name: @bassist/utils | ||
* version: v0.2.0 | ||
* version: v0.3.0 | ||
* description: Opinionated collection of common JavaScript / TypeScript utils by @chengpeiquan . | ||
@@ -9,2 +9,2 @@ * author: chengpeiquan <chengpeiquan@chengpeiquan.com> | ||
*/ | ||
(function(n,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.Utils={}))})(this,function(n){"use strict";function o(i){return Object.prototype.toString.call(i).slice(8,-1).toLowerCase()}function a(i){return o(i)==="object"}function m(i,t){return a(i)?Object.prototype.hasOwnProperty.call(i,t):!1}const u=typeof window<"u",b=!u,e=u?navigator.userAgent.toLowerCase():"",c=/iPhone|phone|android|iPod|pad|iPad/i,h=c.test(e),w=!c.test(e),g=/Android/i.test(e),v=/iPhone|iPod|iPad|iOS/i.test(e),y=/uni-app|html5plus/.test(e),E=/MicroMessenger/i.test(e),A=/\sQQ|mqqbrowser|qzone|qqbrowser/i.test(e),M=/mqqbrowser|qqbrowser/i.test(e),Q=/qzone\/.*_qz_([\d.]+)/i.test(e),R=/(weibo).*weibo__([\d.]+)/i.test(e),q=/(baiduboxapp)\/([\d.]+)/i.test(e);function f(i=0,t=100,s="round"){return Math[s](Math.random()*(t-i)+i)}function O(i=10){const t="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let s="",r=i;const U=t.length;for(;r--;)s+=t[Math.random()*U|0];return s}function S(){const i=f(0,1);return[!0,!1][i]}function _(i){if(!Array.isArray(i))return i;for(let t=i.length-1;t>0;t--){const s=Math.floor(Math.random()*(t+1)),r=i[t];i[t]=i[s],i[s]=r}return i}function B(i){return/^1[3456789]\d{9}$/.test(String(i))}function D(i){return/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(i)}function P(i){return/https?:\/\/[\w-]+(\.[\w-]+){1,2}(\/[\w-]{3,6}){0,2}(\?[\w_]{4,6}=[\w_]{4,6}(&[\w_]{4,6}=[\w_]{4,6}){0,2})?/.test(i)}function z(i){const t=/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,s=/^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/;return t.test(i)||s.test(i)}function j(i){return/^([1-9]{1})(\d{15}|\d{16}|\d{18})$/.test(i)}function l(){return process.env.NODE_ENV}const T=l();function d(i){return process.env.NODE_ENV===i}const k=d("development"),C=d("test"),N=d("production");n.checkRuntimeEnv=d,n.getDataType=o,n.getRuntimeEnv=l,n.hasKey=m,n.isAndroid=g,n.isBaidu=q,n.isBankCard=j,n.isBrowser=u,n.isDesktop=w,n.isDevRuntime=k,n.isEmail=D,n.isIOS=v,n.isIdCard=z,n.isMob=B,n.isMobile=h,n.isObject=a,n.isProdRuntime=N,n.isQQ=A,n.isQQBrowser=M,n.isQzone=Q,n.isServer=b,n.isTestRuntime=C,n.isUniApp=y,n.isUrl=P,n.isWeibo=R,n.isWeixin=E,n.mobileDevicesRegExp=c,n.randomBoolean=S,n.randomNumber=f,n.randomString=O,n.runtimeEnv=T,n.shuffle=_,n.ua=e,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}); | ||
(function(e,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(e=typeof globalThis<"u"?globalThis:e||self,s(e.Utils={}))})(this,function(e){"use strict";var K=Object.defineProperty;var V=(e,s,a)=>s in e?K(e,s,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[s]=a;var f=(e,s,a)=>(V(e,typeof s!="symbol"?s+"":s,a),a);const s={isSupported:typeof navigator>"u"?!1:Boolean(navigator.clipboard),async copy(i){if(!this.isSupported)return!1;const t=i.innerText||i.value;return await this.write(t)},async cut(i){return!this.isSupported||!await this.copy(i)?!1:(i.value="",!0)},async read(){return this.isSupported?await navigator.clipboard.readText():""},async write(i){return this.isSupported?(await navigator.clipboard.writeText(i),!0):!1}};function a(i){return Object.prototype.toString.call(i).slice(8,-1).toLowerCase()}function m(i){return a(i)==="object"}function u(i,t){return m(i)?Object.prototype.hasOwnProperty.call(i,t):!1}const d=typeof window<"u",p=!d,r=d?navigator.userAgent.toLowerCase():"",h=/iPhone|phone|android|iPod|pad|iPad/i,y=h.test(r),v=!h.test(r),O=/Android/i.test(r),E=/iPhone|iPod|iPad|iOS/i.test(r),k=/uni-app|html5plus/.test(r),R=/MicroMessenger/i.test(r),A=/\sQQ|mqqbrowser|qzone|qqbrowser/i.test(r),B=/mqqbrowser|qqbrowser/i.test(r),M=/qzone\/.*_qz_([\d.]+)/i.test(r),Q=/(weibo).*weibo__([\d.]+)/i.test(r),q=/(baiduboxapp)\/([\d.]+)/i.test(r);function b(i=0,t=100,n="round"){return Math[n](Math.random()*(t-i)+i)}function D(i=10){const t="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let n="",o=i;const c=t.length;for(;o--;)n+=t[Math.random()*c|0];return n}function T(){const i=b(0,1);return[!0,!1][i]}function _(i){if(!Array.isArray(i))return i;for(let t=i.length-1;t>0;t--){const n=Math.floor(Math.random()*(t+1)),o=i[t];i[t]=i[n],i[n]=o}return i}function $(i){return/^1[3456789]\d{9}$/.test(String(i))}function I(i){return/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(i)}function P(i){return/https?:\/\/[\w-]+(\.[\w-]+){1,2}(\/[\w-]{3,6}){0,2}(\?[\w_]{4,6}=[\w_]{4,6}(&[\w_]{4,6}=[\w_]{4,6}){0,2})?/.test(i)}function j(i){const t=/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,n=/^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/;return t.test(i)||n.test(i)}function z(i){return/^([1-9]{1})(\d{15}|\d{16}|\d{18})$/.test(i)}function w(){return process.env.NODE_ENV}const N=w();function l(i){return process.env.NODE_ENV===i}const C=l("development"),U=l("test"),W=l("production"),g={};class L{constructor(t){f(this,"data");this.data={};const n=u(g,t);this.data=n?g[t].data:{},n||(g[t]=this)}get length(){return Object.keys(this.data).length}clear(){this.data={}}getItem(t){return u(this.data,t)?this.data[t]:null}setItem(t,n){this.data[t]=n}removeItem(t){u(this.data,t)&&delete this.data[t]}key(t){const n=Object.keys(this.data);return t>n.length?null:n[t]}}class S{constructor(t,n){f(this,"prefix");f(this,"storage");this.prefix=t,this.storage=d?window[n]:new L(t)}get(t){const n=this.storage.getItem(`${this.prefix}-${t}`);if(!n)return n;try{return n==="true"?!0:n==="false"?!1:n==="null"?null:n==="undefined"?void 0:JSON.parse(n)}catch{return n}}set(t,n){try{const c=a(n)==="string"?n:JSON.stringify(n);this.storage.setItem(`${this.prefix}-${t}`,c)}catch(o){console.log(o)}}remove(t){this.storage.removeItem(`${this.prefix}-${t}`)}clear(){this.list().forEach(n=>{this.remove(n)})}count(){return this.list().length}list(){if(!this.prefix)return[];const t=[],n=this.storage.length;for(let o=0;o<n;o++){const c=this.storage.key(o);c!=null&&c.startsWith(this.prefix)&&t.push(c.replace(`${this.prefix}-`,""))}return t}}class Z extends S{constructor(t){super(t,"localStorage")}}class J extends S{constructor(t){super(t,"sessionStorage")}}e.LocalStorage=Z,e.SessionStorage=J,e.checkRuntimeEnv=l,e.clipboard=s,e.getDataType=a,e.getRuntimeEnv=w,e.hasKey=u,e.isAndroid=O,e.isBaidu=q,e.isBankCard=z,e.isBrowser=d,e.isDesktop=v,e.isDevRuntime=C,e.isEmail=I,e.isIOS=E,e.isIdCard=j,e.isMob=$,e.isMobile=y,e.isObject=m,e.isProdRuntime=W,e.isQQ=A,e.isQQBrowser=B,e.isQzone=M,e.isServer=p,e.isTestRuntime=U,e.isUniApp=k,e.isUrl=P,e.isWeibo=Q,e.isWeixin=R,e.mobileDevicesRegExp=h,e.randomBoolean=T,e.randomNumber=b,e.randomString=D,e.runtimeEnv=N,e.shuffle=_,e.ua=r,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}); |
{ | ||
"name": "@bassist/utils", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Opinionated collection of common JavaScript / TypeScript utils by @chengpeiquan .", | ||
@@ -5,0 +5,0 @@ "author": "chengpeiquan <chengpeiquan@chengpeiquan.com>", |
/** | ||
* @category interactive | ||
*/ | ||
export type WritableElement = HTMLInputElement | HTMLTextAreaElement; | ||
/** | ||
* @category interactive | ||
*/ | ||
export type CopyableElement = HTMLElement | WritableElement; | ||
/** | ||
* @category interactive | ||
*/ | ||
export interface ClipboardInstance { | ||
/** | ||
* Determine whether the clipboard is supported | ||
*/ | ||
isSupported: boolean; | ||
/** | ||
* Copy the text passed in or the text of the specified DOM element | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* clipboard.copy(document.querySelector('.foo')) | ||
* ``` | ||
*/ | ||
copy: (el: CopyableElement) => Promise<boolean>; | ||
/** | ||
* Cut the text passed in or the text of the specified DOM element | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* clipboard.cut(document.querySelector('.foo')) | ||
* ``` | ||
*/ | ||
cut: (el: WritableElement) => Promise<boolean>; | ||
/** | ||
* Read the text content of the clipboard | ||
*/ | ||
read: () => Promise<string>; | ||
/** | ||
* Write text content to clipboard | ||
*/ | ||
write: (text: string) => Promise<boolean>; | ||
} | ||
/** | ||
* Extensions based on the Clipboard API | ||
* | ||
* @category interactive | ||
*/ | ||
export declare const clipboard: ClipboardInstance; | ||
/** | ||
* The actual type of the data | ||
@@ -189,3 +240,52 @@ * | ||
export declare const isProdRuntime: boolean; | ||
export type StorageType = "localStorage" | "sessionStorage"; | ||
declare class BaseStorage { | ||
prefix: string; | ||
private storage; | ||
constructor(prefix: string, storageType: StorageType); | ||
/** | ||
* Read stored data | ||
* @tips The `key` doesn't need to be prefixed | ||
* @returns The data in the format before storage | ||
*/ | ||
get(key: string): any; | ||
/** | ||
* Set storage data | ||
*/ | ||
set(key: string, value: any): void; | ||
/** | ||
* Remove the specified storage data under the current prefix | ||
*/ | ||
remove(key: string): void; | ||
/** | ||
* Clear all stored data under the current prefix | ||
*/ | ||
clear(): void; | ||
/** | ||
* Count the number of storage related to the current prefix | ||
*/ | ||
count(): number; | ||
/** | ||
* List storage keys associated under the current prefix | ||
* @tips All keys without prefix | ||
*/ | ||
list(): string[]; | ||
} | ||
/** | ||
* localStorage that supports prefixes | ||
* | ||
* @category storage | ||
*/ | ||
export declare class LocalStorage extends BaseStorage { | ||
constructor(prefix: string); | ||
} | ||
/** | ||
* sessionStorage that supports prefixes | ||
* | ||
* @category storage | ||
*/ | ||
export declare class SessionStorage extends BaseStorage { | ||
constructor(prefix: string); | ||
} | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23116
553