UNPKG

extra-function

Version:

A function is a set of statements that performs a task or calculates a value.

248 lines (243 loc) 8.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _build = {}; Object.defineProperty(_build, "__esModule", { value: true }); exports.throttleEarly = _build.throttleEarly = exports.throttle = _build.throttle = exports.debounceEarly = _build.debounceEarly = exports.debounce = _build.debounce = exports.after = _build.after = exports.restrictAfter = _build.restrictAfter = exports.before = _build.before = exports.restrictBefore = _build.restrictBefore = exports.once = _build.once = exports.restrictOnce = _build.restrictOnce = exports.restrict = _build.restrict = exports.delay = _build.delay = exports.defer = _build.defer = exports.curryRight = _build.curryRight = exports.curry = _build.curry = exports.composeRight = _build.composeRight = exports.compose = _build.compose = exports.partialRight = _build.partialRight = exports.attachRight = _build.attachRight = exports.partial = _build.partial = exports.attach = _build.attach = exports.unspread = _build.unspread = exports.spread = _build.spread = exports.flip = _build.flip = exports.reverse = _build.reverse = exports.memoize = _build.memoize = exports.negate = _build.negate = exports.decontextify = _build.decontextify = exports.contextify = _build.contextify = exports.isGenerator = _build.isGenerator = exports.isAsync = _build.isAsync = exports.is = _build.is = exports.apply = _build.apply = exports.call = _build.call = exports.bind = _build.bind = exports.arity = _build.arity = exports.length = _build.length = exports.name = _build.name = exports.ARGUMENTS = _build.ARGUMENTS = exports.COMPARE = _build.COMPARE = exports.IDENTITY = _build.IDENTITY = exports.TRUE = _build.TRUE = exports.FALSE = _build.FALSE = exports.NOOP = _build.NOOP = void 0; function NOOP(...args) { } exports.NOOP = _build.NOOP = NOOP; function FALSE(...args) { return false; } exports.FALSE = _build.FALSE = FALSE; function TRUE(...args) { return true; } exports.TRUE = _build.TRUE = TRUE; function IDENTITY(v) { return v; } exports.IDENTITY = _build.IDENTITY = IDENTITY; function COMPARE(a, b) { return a < b ? -1 : (a > b ? 1 : 0); } exports.COMPARE = _build.COMPARE = COMPARE; function ARGUMENTS(...args) { return args; } exports.ARGUMENTS = _build.ARGUMENTS = ARGUMENTS; function name(x) { return x.name; } exports.name = _build.name = name; function length(x) { return x.length; } exports.length = _build.length = length; exports.arity = _build.arity = length; function bind(x, ths, ...prefix) { return x.bind(ths, ...prefix); } exports.bind = _build.bind = bind; function call(x, ths = null, ...args) { return x.call(ths, ...args); } exports.call = _build.call = call; function apply(x, ths = null, args) { return x.apply(ths, args); } exports.apply = _build.apply = apply; function is(v) { return typeof v === "function"; } exports.is = _build.is = is; function isAsync(v) { const AsyncFunction = (async function () { }).constructor; return v instanceof AsyncFunction; } exports.isAsync = _build.isAsync = isAsync; function isGenerator(v) { const GeneratorFunction = (function* () { }).constructor; return v instanceof GeneratorFunction; } exports.isGenerator = _build.isGenerator = isGenerator; function contextify(x) { return function (...args) { return x(this, ...args); }; } exports.contextify = _build.contextify = contextify; function decontextify(x) { return (ths = null, ...args) => x.call(ths, ...args); } exports.decontextify = _build.decontextify = decontextify; function negate(x) { return (...args) => !x(...args); } exports.negate = _build.negate = negate; function memoize(x, fr = null, cache = null) { var fr = fr || IDENTITY; var cache = cache || new Map(); return (...args) => { var k = fr(...args); if (cache.has(k)) return cache.get(k); var v = x(...args); cache.set(k, v); return v; }; } exports.memoize = _build.memoize = memoize; function reverse(x) { return (...args) => x(...args.reverse()); } exports.reverse = _build.reverse = reverse; exports.flip = _build.flip = reverse; function spread(x) { return (...args) => x(args); } exports.spread = _build.spread = spread; function unspread(x) { return (args) => x(...args); } exports.unspread = _build.unspread = unspread; function attach(x, ...prefix) { return (...args) => x(...prefix, ...args); } exports.attach = _build.attach = attach; exports.partial = _build.partial = attach; function attachRight(x, ...suffix) { return (...args) => x(...args, ...suffix); } exports.attachRight = _build.attachRight = attachRight; exports.partialRight = _build.partialRight = attachRight; function compose(...xs) { return composeRight(...xs.reverse()); } exports.compose = _build.compose = compose; function composeRight(...xs) { return (...args) => { if (xs.length === 0) return; var a = xs[0](...args); for (var i = 1, I = xs.length; i < I; i++) a = xs[i](a); return a; }; } exports.composeRight = _build.composeRight = composeRight; function curry(x, n = x.length) { return (...args) => { if (args.length >= n) return x(...args); else return curry((...rest) => x(...args, ...rest), n - args.length); }; } exports.curry = _build.curry = curry; function curryRight(x, n = x.length) { return curry(reverse(x), n); } exports.curryRight = _build.curryRight = curryRight; function defer(x) { return (...args) => { var h = setImmediate(flush); function clear() { clearImmediate(h); h = null; } function flush() { x(...args); clear(); } return { clear, flush }; }; } exports.defer = _build.defer = defer; function delay(x, t) { return (...args) => { var h = setTimeout(flush, t); function clear() { clearTimeout(h); h = null; } function flush() { x(...args); clear(); } return { clear, flush }; }; } exports.delay = _build.delay = delay; function restrict(x, start, end = -1) { var i = -1; return (...args) => { if ((++i < start) === (i < end || end < 0)) return; return x(...args); }; } exports.restrict = _build.restrict = restrict; function restrictOnce(x) { return restrict(x, 0, 1); } exports.restrictOnce = _build.restrictOnce = restrictOnce; exports.once = _build.once = restrictOnce; function restrictBefore(x, n) { return restrict(x, 0, n); } exports.restrictBefore = _build.restrictBefore = restrictBefore; exports.before = _build.before = restrictBefore; function restrictAfter(x, n) { return restrict(x, n); } exports.restrictAfter = _build.restrictAfter = restrictAfter; exports.after = _build.after = restrictAfter; function debounce(x, t, T = -1) { var savedArgs; var h = null, H = null; function clear() { clearTimeout(h); clearTimeout(H); h = H = null; } function flush() { x(...savedArgs); clear(); } return (...args) => { savedArgs = args; if (T >= 0) H = H || setTimeout(flush, T); if (T < 0 || t < T) { clearTimeout(h); h = setTimeout(flush, t); } return { clear, flush }; }; } exports.debounce = _build.debounce = debounce; function debounceEarly(x, t, T = -1) { var h = null, H = null; function clear() { h = H = null; } function flush() { clear(); } return (...args) => { if (!h && !H) x(...args); if (T >= 0) H = H || setTimeout(flush, T); if (T < 0 || t < T) { clearTimeout(h); h = setTimeout(flush, t); } return { clear, flush }; }; } exports.debounceEarly = _build.debounceEarly = debounceEarly; function throttle(x, t) { var savedArgs; var h = null; function clear() { h = null; } function flush() { x(...savedArgs); clear(); } return (...args) => { savedArgs = args; h = h || setTimeout(flush, t); return { clear, flush }; }; } exports.throttle = _build.throttle = throttle; function throttleEarly(x, t) { var h = null; function clear() { h = null; } function flush() { clear(); } return (...args) => { if (!h) x(...args); h = h || setTimeout(flush, t); return { clear, flush }; }; } exports.throttleEarly = _build.throttleEarly = throttleEarly; exports.default = _build;