UNPKG

@gvray/eskit

Version:

A rich and colorful toolkit about typescript and javascript.

94 lines 3.51 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Creates a curried version of a function that can be called with partial arguments. * 创建函数的柯里化版本,可以使用部分参数调用。 * * Currying transforms a function that takes multiple arguments into a sequence * of functions that each take a single argument. This enables partial application * and function composition patterns. * 柯里化将接受多个参数的函数转换为一系列每个都接受单个参数的函数。 * 这使得部分应用和函数组合模式成为可能。 * * @param fn - The function to be curried / 要柯里化的函数 * @returns A curried function / 柯里化的函数 * * @example * ```typescript * // Basic currying / 基本柯里化 * const add = (a: number, b: number, c: number) => a + b + c * const curriedAdd = curry(add) * * // Partial application / 部分应用 * const add5 = curriedAdd(5) * const add5And3 = add5(3) * console.log(add5And3(2)) // 10 * * // Or call with all arguments at once / 或一次性传入所有参数 * console.log(curriedAdd(1)(2)(3)) // 6 * console.log(curriedAdd(1, 2, 3)) // 6 * * // Practical example: creating specialized functions / 实际示例:创建专用函数 * const multiply = (a: number, b: number, c: number) => a * b * c * const curriedMultiply = curry(multiply) * * const double = curriedMultiply(2) * const doubleAndTriple = double(3) * console.log(doubleAndTriple(4)) // 24 * * // Function composition / 函数组合 * const formatMessage = curry((prefix: string, type: string, message: string) => * `[${prefix}] ${type}: ${message}` * ) * * const logError = formatMessage('APP')('ERROR') * const logWarning = formatMessage('APP')('WARNING') * * console.log(logError('Something went wrong')) // "[APP] ERROR: Something went wrong" * console.log(logWarning('This is a warning')) // "[APP] WARNING: This is a warning" * ``` * * @since 1.0.0 */ function curry(fn) { return function curried() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return args.length >= fn.length ? fn.apply(void 0, __spreadArray([], __read(args), false)) : function () { var moreArgs = []; for (var _i = 0; _i < arguments.length; _i++) { moreArgs[_i] = arguments[_i]; } return curried.apply(void 0, __spreadArray(__spreadArray([], __read(args), false), __read(moreArgs), false)); }; }; } exports.default = curry; //# sourceMappingURL=curry.js.map