|
| 1 | +import dayjs, { Dayjs } from "dayjs"; |
| 2 | + |
| 3 | +/* eslint no-useless-escape:0 import/prefer-default-export:0 */ |
| 4 | +const regexUrl = |
| 5 | + /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/; |
| 6 | +const regexEmail = |
| 7 | + /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; |
| 8 | +const regexPositiveNumber = /^([0]*[1-9][0-9]*)(\.[0-9]*)?$/; |
| 9 | +const regexChineseMobileNumber = |
| 10 | + /^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/; |
| 11 | + |
| 12 | +export const isUrl = (path: string): boolean => regexUrl.test(path); |
| 13 | +export const isEmail = (path: string): boolean => regexEmail.test(path); |
| 14 | +export const isPositiveNumber = (path: string): boolean => regexPositiveNumber.test(path); |
| 15 | +export const isChineseMobileNumber = (path: string): boolean => regexChineseMobileNumber.test(path); |
| 16 | +export function isPositiveInt(value?: string): boolean { |
| 17 | + return value !== undefined && /^\d+$/.test(value) && Number(value) > 0; |
| 18 | +} |
| 19 | + |
| 20 | +export const rangeNumber = (from: number, to: number, step: number = 1) => [...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step); |
| 21 | + |
| 22 | +export const isNullOrBlankString = (value?: any) => |
| 23 | + typeof value !== "string" || value == null || value!!.trim().length === 0; |
| 24 | + |
| 25 | +export const isNotNullOrBlankString = (value?: any) => !isNullOrBlankString(value); |
| 26 | + |
| 27 | +export function generateUUID() { |
| 28 | + let d = new Date().getTime(); |
| 29 | + const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { |
| 30 | + const r = (d + Math.random() * 16) % 16 | 0; |
| 31 | + d = Math.floor(d / 16); |
| 32 | + return (c === "x" ? r : (r & 0x3) | 0x8).toString(16); |
| 33 | + }); |
| 34 | + return uuid; |
| 35 | +} |
| 36 | + |
| 37 | +export function randomStr(len: number = 8) { |
| 38 | + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 39 | + const maxPos = chars.length |
| 40 | + let s = "" |
| 41 | + for (let i = 0; i < len; i++) { |
| 42 | + s += chars.charAt(Math.floor(Math.random() * maxPos)) |
| 43 | + } |
| 44 | + return s |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +export type SessionCachedType<T> = { |
| 49 | + [P in keyof T]?: T[P]; |
| 50 | +}; |
| 51 | + |
| 52 | +export class SessionCached<T> implements SessionCachedType<{ data: T; expired: number }> { |
| 53 | + constructor(public data: T, public expired: number) {} |
| 54 | +} |
| 55 | + |
| 56 | +export async function getOrSetCached<T>(key: string, timeout: Dayjs, setter: () => Promise<T | undefined | null>) { |
| 57 | + const cached = sessionStorage.getItem(key); |
| 58 | + if (isNotNullOrBlankString(cached)) { |
| 59 | + let item: any; |
| 60 | + try { |
| 61 | + item = JSON.parse(cached as string) as SessionCached<T>; |
| 62 | + } catch (e) { |
| 63 | + console.warn(e); |
| 64 | + } |
| 65 | + if (item !== undefined && item.expired > dayjs()) { |
| 66 | + return item.data; |
| 67 | + } else { |
| 68 | + sessionStorage.removeItem(key); |
| 69 | + } |
| 70 | + } |
| 71 | + const data = await setter(); |
| 72 | + if (data !== undefined && data !== null) { |
| 73 | + const cache = new SessionCached<T>(data, timeout.valueOf()); |
| 74 | + sessionStorage.setItem(key, JSON.stringify(cache)); |
| 75 | + } |
| 76 | + return data; |
| 77 | +} |
| 78 | + |
| 79 | +export { regexUrl, regexEmail, regexPositiveNumber, regexChineseMobileNumber }; |
| 80 | + |
| 81 | + |
0 commit comments