import clsx from 'clsx'; import NP from 'number-precision'; import { PLUGIN_EDITOR_PREFIX_CLS, PLUGIN_PREFIX_CLS, PLUGIN_RENDER_PREFIX_CLS, } from './constants'; export const pluginCls = (...inputs: Parameters) => clsx(PLUGIN_PREFIX_CLS, ...inputs); export const pluginEditorCls = (...inputs: Parameters) => clsx(PLUGIN_EDITOR_PREFIX_CLS, ...inputs); export const pluginRenderCls = (...inputs: Parameters) => clsx(PLUGIN_RENDER_PREFIX_CLS, ...inputs); export const getStatic = (path: string) => `${window.wpPluginData?.dir || ''}static${path}`; export type TypeCheckValidType = | 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function' | 'null' | 'undefined' | 'regexp' | 'date'; export const typeCheck = ( value: any, type: TypeCheckValidType | TypeCheckValidType[], ) => { const valueType = Object.prototype.toString .call(value) .toLowerCase() .slice(8, -1) as TypeCheckValidType; if (Array.isArray(type)) { return type.includes(valueType); } return valueType === type; }; export const parsePrice = ( price: number, fixed: number = 2, ): string | number => { if (Number.isNaN(price)) return price; return NP.divide(price, 100).toFixed(fixed); }; export const storageGet = (key: string, defaultValue: T): T => { const storageValue = localStorage.getItem(key); if (!storageValue) return defaultValue; try { const parsedValue: T = JSON.parse(storageValue); return parsedValue; } catch { return storageValue as T; } }; export const storageSet = (key: string, value: any) => { if (typeCheck(value, 'string')) { localStorage.setItem(key, value); } else { localStorage.setItem(key, JSON.stringify(value)); } };