UNPKG

shineout

Version:

Shein 前端组件库

105 lines (86 loc) 2.6 kB
"use strict"; exports.__esModule = true; var _exportNames = { compose: true, curry: true, empty: true, memoize: true, createFunc: true, throttle: true }; exports.curry = curry; exports.empty = empty; exports.memoize = memoize; exports.createFunc = createFunc; exports.throttle = exports.compose = void 0; var _compose = require("./compose"); exports.compose = _compose.compose; var _curry = require("./curry"); Object.keys(_curry).forEach(function (key) { if (key === "default" || key === "__esModule") return; if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; exports[key] = _curry[key]; }); /** * from redux.compose https://github.com/reactjs/redux/blob/master/src/compose.js * Composes single-argument functions from right to left. The rightmost * function can take multiple arguments as it provides the signature for * the resulting composite function. * * @param {...Function} funcs The functions to compose. * @returns {Function} A function obtained by composing the argument functions * from right to left. For example, compose(f, g, h) is identical to doing * (...args) => f(g(h(...args))). */ function curry(f) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (args.length >= f.length) { return f.apply(void 0, args); } return function () { for (var _len2 = arguments.length, next = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { next[_key2] = arguments[_key2]; } return curry.apply(void 0, [f.bind.apply(f, [f].concat(args))].concat(next)); }; } function empty(e) { e.preventDefault(); } function memoize(fn) { return function (key) { fn.cache = fn.cache || {}; if (!(key in fn.cache)) { fn.cache[key] = fn(key); } return fn.cache[key]; }; } function createFunc(func) { if (typeof func === 'function') return func; return function (data) { return func ? data[func] : data; }; } var throttle = function throttle(func, timer) { var that = {}; var cleanTimer = function cleanTimer() { if (that.timer) { clearTimeout(that.timer); that.timer = null; } }; if (!timer) return [func, cleanTimer]; return [function () { for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } cleanTimer(); that.timer = setTimeout(function () { func.apply(void 0, args); }, timer); }, cleanTimer]; }; exports.throttle = throttle;