utils.d.ts.map 3.6 KB

1
  1. {"version":3,"sources":["../src/common/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,wDAUpB,CAAC;AAEF,eAAO,MAAM,UAAU,4BA8BtB,CAAC;AAEF,eAAO,MAAM,MAAM,2BAIlB,CAAC;AAEF,eAAO,MAAM,qBAAqB,OAAiB,QAAQ,+BAQ1D,CAAC;AAMF,eAAO,MAAM,QAAQ,yBAEpB,CAAC;AAEF,eAAO,MAAM,OAAO,WAAqB,MAAM,GAAG,MAAM,KAAG,MAAM,GAAG,SAMnE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,QACxB,MAAM,iBACI,MAAM,KACpB;IAAE,QAAQ,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAyCtC,CAAC","file":"utils.d.ts","sourcesContent":["export const debounce = function (func, wait = 500) {\n let timerId;\n return function (...rest) {\n if (timerId) {\n clearTimeout(timerId);\n }\n timerId = setTimeout(() => {\n func.apply(this, rest);\n }, wait);\n };\n};\n\nexport const classNames = function (...args) {\n const hasOwn = {}.hasOwnProperty;\n const classes = [];\n\n args.forEach((arg) => {\n // for (let i = 0; i < args.length; i++) {\n // eslint-disable-next-line\n // const arg = args[i]\n if (!arg) return;\n\n const argType = typeof arg;\n\n if (argType === 'string' || argType === 'number') {\n classes.push(arg);\n } else if (Array.isArray(arg) && arg.length) {\n const inner = classNames(...arg);\n if (inner) {\n classes.push(inner);\n }\n } else if (argType === 'object') {\n // eslint-disable-next-line\n for (const key in arg) {\n if (hasOwn.call(arg, key) && arg[key]) {\n classes.push(key);\n }\n }\n }\n });\n\n return classes.join(' ');\n};\n\nexport const styles = function (styleObj) {\n return Object.keys(styleObj)\n .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`)\n .join('; ');\n};\n\nexport const requestAnimationFrame = function (cb: Function) {\n return wx\n .createSelectorQuery()\n .selectViewport()\n .boundingClientRect()\n .exec(() => {\n cb();\n });\n};\n\nconst isDef = function (value: any): boolean {\n return value !== undefined && value !== null;\n};\n\nexport const isNumber = function (value) {\n return /^\\d+(\\.\\d+)?$/.test(value);\n};\n\nexport const addUnit = function (value?: string | number): string | undefined {\n if (!isDef(value)) {\n return undefined;\n }\n value = String(value);\n return isNumber(value) ? `${value}px` : value;\n};\n\n/**\n * 计算字符串字符的长度并可以截取字符串。\n * @param str 传入字符串\n * @param maxCharacter 规定最大字符串长度\n * @returns 当没有传入maxCharacter时返回字符串字符长度,当传入maxCharacter时返回截取之后的字符串和长度。\n */\nexport const getCharacterLength = (\n str: string,\n maxCharacter?: number,\n): { length: number; characters: string } => {\n const hasMaxCharacter = typeof maxCharacter === 'number';\n if (!str || str.length === 0) {\n if (hasMaxCharacter) {\n return {\n length: 0,\n characters: str,\n };\n }\n return {\n length: 0,\n characters: '',\n };\n }\n let len = 0;\n // eslint-disable-next-line no-plusplus\n for (let i = 0; i < str.length; i++) {\n let currentStringLength = 0;\n if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {\n currentStringLength = 2;\n } else {\n currentStringLength = 1;\n }\n if (hasMaxCharacter && len + currentStringLength > maxCharacter) {\n return {\n length: len,\n characters: str.slice(0, i),\n };\n }\n len += currentStringLength;\n }\n if (hasMaxCharacter) {\n return {\n length: len,\n characters: str,\n };\n }\n return {\n length: len,\n characters: '',\n };\n};\n"]}