123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import { prefix } from './config';
- const systemInfo = wx.getSystemInfoSync();
- export const debounce = function (func, wait = 500) {
- let timerId;
- return function (...rest) {
- if (timerId) {
- clearTimeout(timerId);
- }
- timerId = setTimeout(() => {
- func.apply(this, rest);
- }, wait);
- };
- };
- export const throttle = (func, wait = 100, options = null) => {
- let previous = 0;
- let timerid = null;
- if (!options) {
- options = {
- leading: true,
- };
- }
- return function (...args) {
- const now = Date.now();
- if (!previous && !options.leading)
- previous = now;
- const remaining = wait - (now - previous);
- const context = this;
- if (remaining <= 0) {
- if (timerid) {
- clearTimeout(timerid);
- timerid = null;
- }
- previous = now;
- func.apply(context, args);
- }
- };
- };
- export const classNames = function (...args) {
- const hasOwn = {}.hasOwnProperty;
- const classes = [];
- args.forEach((arg) => {
- if (!arg)
- return;
- const argType = typeof arg;
- if (argType === 'string' || argType === 'number') {
- classes.push(arg);
- }
- else if (Array.isArray(arg) && arg.length) {
- const inner = classNames(...arg);
- if (inner) {
- classes.push(inner);
- }
- }
- else if (argType === 'object') {
- for (const key in arg) {
- if (hasOwn.call(arg, key) && arg[key]) {
- classes.push(key);
- }
- }
- }
- });
- return classes.join(' ');
- };
- export const styles = function (styleObj) {
- return Object.keys(styleObj)
- .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`)
- .join('; ');
- };
- export const getAnimationFrame = function (cb) {
- return wx
- .createSelectorQuery()
- .selectViewport()
- .boundingClientRect()
- .exec(() => {
- cb();
- });
- };
- export const getRect = function (context, selector, needAll = false) {
- return new Promise((resolve, reject) => {
- wx.createSelectorQuery()
- .in(context)[needAll ? 'selectAll' : 'select'](selector)
- .boundingClientRect((rect) => {
- if (rect) {
- resolve(rect);
- }
- else {
- reject(rect);
- }
- })
- .exec();
- });
- };
- const isDef = function (value) {
- return value !== undefined && value !== null;
- };
- export const isNumber = function (value) {
- return /^\d+(\.\d+)?$/.test(value);
- };
- export const addUnit = function (value) {
- if (!isDef(value)) {
- return undefined;
- }
- value = String(value);
- return isNumber(value) ? `${value}px` : value;
- };
- export const getCharacterLength = (type, str, max) => {
- if (!str || str.length === 0) {
- return {
- length: 0,
- characters: '',
- };
- }
- if (type === 'maxcharacter') {
- let len = 0;
- for (let i = 0; i < str.length; i += 1) {
- let currentStringLength = 0;
- if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
- currentStringLength = 2;
- }
- else {
- currentStringLength = 1;
- }
- if (len + currentStringLength > max) {
- return {
- length: len,
- characters: str.slice(0, i),
- };
- }
- len += currentStringLength;
- }
- return {
- length: len,
- characters: str,
- };
- }
- else if (type === 'maxlength') {
- const length = str.length > max ? max : str.length;
- return {
- length,
- characters: str.slice(0, length),
- };
- }
- return {
- length: str.length,
- characters: str,
- };
- };
- export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
- export const getInstance = function (context, selector) {
- if (!context) {
- const pages = getCurrentPages();
- const page = pages[pages.length - 1];
- context = page.$$basePage || page;
- }
- const instance = context ? context.selectComponent(selector) : null;
- if (!instance) {
- console.warn('未找到组件,请检查selector是否正确');
- return null;
- }
- return instance;
- };
- export const unitConvert = (value) => {
- var _a;
- if (typeof value === 'string') {
- if (value.includes('rpx')) {
- return (parseInt(value, 10) * ((_a = systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.screenWidth) !== null && _a !== void 0 ? _a : 750)) / 750;
- }
- return parseInt(value, 10);
- }
- return value;
- };
- export const setIcon = (iconName, icon, defaultIcon) => {
- if (icon) {
- if (typeof icon === 'string') {
- return {
- [`${iconName}Name`]: icon,
- [`${iconName}Data`]: {},
- };
- }
- else if (typeof icon === 'object') {
- return {
- [`${iconName}Name`]: '',
- [`${iconName}Data`]: icon,
- };
- }
- else {
- return {
- [`${iconName}Name`]: defaultIcon,
- [`${iconName}Data`]: {},
- };
- }
- }
- return {
- [`${iconName}Name`]: '',
- [`${iconName}Data`]: {},
- };
- };
- export const isBool = (val) => typeof val === 'boolean';
- export const isObject = (val) => typeof val === 'object' && val != null;
- export const isString = (val) => typeof val === 'string';
- export const toCamel = (str) => str.replace(/-(\w)/g, (match, m1) => m1.toUpperCase());
- export const getCurrentPage = function () {
- const pages = getCurrentPages();
- return pages[pages.length - 1];
- };
- export const uniqueFactory = (compName) => {
- let number = 0;
- return () => `${prefix}_${compName}_${number++}`;
- };
- export const calcIcon = (icon, defaultIcon) => {
- if ((isBool(icon) && icon && defaultIcon) || isString(icon)) {
- return { name: isBool(icon) ? defaultIcon : icon };
- }
- if (isObject(icon)) {
- return icon;
- }
- return null;
- };
|