UNPKG

shineout

Version:
100 lines (98 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _exportNames = { curry: true, empty: true, memoize: true, createFunc: true, throttle: true, compose: true }; Object.defineProperty(exports, "compose", { enumerable: true, get: function get() { return _compose.compose; } }); exports.createFunc = createFunc; exports.curry = curry; exports.empty = empty; exports.memoize = memoize; exports.throttle = void 0; var _compose = require("./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; if (key in exports && exports[key] === _curry[key]) return; Object.defineProperty(exports, key, { enumerable: true, get: function get() { return _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 = exports.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]; };