UNPKG

press-ui

Version:

简单、易用的跨端组件库,兼容 Vue2 和 Vue3,同时支持 uni-app和普通 Vue 项目

55 lines (45 loc) 979 B
/** * Simple memoize * wxs doesn't support fn.apply, so this memoize only support up to 2 args */ function isPrimitive(value) { const type = typeof value; return ( type === 'boolean' || type === 'number' || type === 'string' || type === 'undefined' || value === null ); } // mock simple fn.call in wxs function call(fn, args) { if (args.length === 2) { return fn(args[0], args[1]); } if (args.length === 1) { return fn(args[0]); } return fn(); } function serializer(args) { if (args.length === 1 && isPrimitive(args[0])) { return args[0]; } const obj = {}; for (let i = 0; i < args.length; i++) { obj[`key${i}`] = args[i]; } return JSON.stringify(obj); } export function memoize(fn) { const cache = {}; return function () { const key = serializer(arguments); if (cache[key] === undefined) { cache[key] = call(fn, arguments); } return cache[key]; }; } export default memoize;