UNPKG

mocoolka-function

Version:

Function lib for function.

717 lines (638 loc) 22.2 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("MocoolkaFunction", [], factory); else if(typeof exports === 'object') exports["MocoolkaFunction"] = factory(); else root["MocoolkaFunction"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 6); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { /** * Gets the name of a function, in a cross-browser way. * * @param {Function} fn function (usually a constructor) * @return {string} */ Object.defineProperty(exports, "__esModule", { value: true }); var name = function (fn) { var functionName = fn['displayName'] == null ? fn['name'] : fn['displayName']; if (!functionName) { /* istanbul ignore next */ var match = /^\s?function ([^(]*)\(/.exec(fn.toString()); /* istanbul ignore next */ functionName = match && match[1] ? match[1] : ''; } return functionName; }; exports.default = name; /***/ }), /* 1 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Set given function name * @param fn * @param name * @return {Function} */ var setName = function (fn, name) { //Object.defineProperty(fn, 'name', { writable: true }); //fn.name = name; fn['displayName'] = name; //Object.defineProperty(fn, 'name', { writable: false }); return fn; }; exports.default = setName; /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var arity_1 = __webpack_require__(10); var isPlaceholder_1 = __webpack_require__(11); var setName_1 = __webpack_require__(1); var getName_1 = __webpack_require__(0); var _curryN = function (length, received, fn, reverse) { if (reverse === void 0) { reverse = false; } var temp = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var combined = []; var argsIdx = 0; var left = length; var combinedIdx = 0; while (combinedIdx < received.length || argsIdx < args.length) { var result = void 0; // get result from current arguments when placeholder is true otherwise get from received if (combinedIdx < received.length && (!isPlaceholder_1.default(received[combinedIdx]) || argsIdx >= args.length)) { result = received[combinedIdx]; } else { result = args[argsIdx]; argsIdx += 1; } combined[combinedIdx] = result; if (!isPlaceholder_1.default(result)) { left -= 1; } combinedIdx += 1; } if (left <= 0) { return (reverse === true) ? fn.apply(null, combined.reverse()) : fn.apply(null, combined); } else { return arity_1.default(left, _curryN(length, combined, fn, reverse)); } }; setName_1.default(temp, getName_1.default(fn)); return temp; }; /** * Returns a curried equivalent of the provided function. * The curried function has two unusual capabilities. * First, its arguments needn't be provided one at a time. * If f is a ternary function and g is curry(f), the following are equivalent: * * - g(1)(2)(3) * - g(1)(2, 3) * - g(1, 2)(3) * - g(1, 2, 3) * Secondly, the special placeholder value __ may be used to specify "gaps", * allowing partial application of any combination of arguments, * regardless of their positions. If g is as above and _ is placeholder, * the following are equivalent: * g(1, 2, 3) * - g(__, 2, 3)(1) * - g(__, __, 3)(1)(2) * - g(__, __, 3)(1, 2) * - g(__, 2)(1)(3) * - g(__, 2)(1, 3) * - g(__, 2)(__, 3)(1) * @since v0.1.0 * @category Arity * @ts Number -> (* -> a) -> (* -> a) * @keywords wrap * @param {number} length -The param number in new function. * @param {Function} fn The function to curry. * @param {boolean} [reverse] * True meaning The given function call params from left to right. * False meaning The given function call params from right to left * @return {Function} A new curried function. */ var curryN = function (fn, length, reverse) { if (reverse === void 0) { reverse = false; } /* istanbul ignore next */ length = length < 0 ? 0 : length; return arity_1.default(length, _curryN(length, [], fn, reverse)); }; exports.default = curryN; /***/ }), /* 3 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.__ = '@@__function/placeholder__@@'; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var setName_1 = __webpack_require__(1); /** * Create a Function with those param * @param name * @param params * @param functionBody * @return {Function} */ var create = function (name, params, functionBody) { var temp = new Function(params, functionBody); setName_1.default(temp, name); return temp; }; exports.default = create; /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var curryN_1 = __webpack_require__(2); /** * Returns a curried equivalent of the provided function. * The curried function has two unusual capabilities. * First, its arguments needn't be provided one at a time. * If f is a ternary function and g is curry(f), the following are equivalent: * * - `g(1)(2)(3)` * - `g(1)(2, 3)` * - `g(1, 2)(3)` * - `g(1, 2, 3)` * Secondly, the special placeholder value __ may be used to specify "gaps", * allowing partial application of any combination of arguments, * regardless of their positions. If g is as above and _ is placeholder, * the following are equivalent: * - `g(1, 2, 3)` * - `g(__, 2, 3)(1)` * - `g(__, __, 3)(1)(2)` * - `g(__, __, 3)(1, 2)` * - `g(__, 2)(1)(3)` * - `g(__, 2)(1, 3)` * - `g(__, 2)(__, 3)(1)` * @since v0.1.0 * @category Arity * @ts `(* -> a) -> (* -> a)` * @keywords wrap * @param {Function} fn The function to curry. * @param {boolean} [reverse] * True meaning The given function call params from left to right. * False meaning The given function call params from right to left * @return {Function} A new curried function. */ var curry = function (fn, reverse) { if (reverse === void 0) { reverse = false; } return curryN_1.default(fn, fn.length, reverse); }; exports.default = curry; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); __export(__webpack_require__(3)); var and_1 = __webpack_require__(7); exports.and = and_1.default; var constant_1 = __webpack_require__(8); exports.constant = constant_1.default; var compose_1 = __webpack_require__(9); exports.compose = compose_1.default; var create_1 = __webpack_require__(4); exports.create = create_1.default; var curry_1 = __webpack_require__(5); exports.curry = curry_1.default; var curryN_1 = __webpack_require__(2); exports.curryN = curryN_1.default; var curryR_1 = __webpack_require__(12); exports.curryR = curryR_1.default; var defaultTo_1 = __webpack_require__(13); exports.defaultTo = defaultTo_1.default; var getName_1 = __webpack_require__(0); exports.getName = getName_1.default; var identity_1 = __webpack_require__(14); exports.identity = identity_1.default; var ifElse_1 = __webpack_require__(15); exports.ifElse = ifElse_1.default; var isNothing_1 = __webpack_require__(16); exports.isNothing = isNothing_1.default; var neg_1 = __webpack_require__(17); exports.neg = neg_1.default; var not_1 = __webpack_require__(18); exports.not = not_1.default; var noop_1 = __webpack_require__(19); exports.noop = noop_1.default; var or_1 = __webpack_require__(20); exports.or = or_1.default; var setName_1 = __webpack_require__(1); exports.setName = setName_1.default; var swap_1 = __webpack_require__(21); exports.swap = swap_1.default; var thunk_1 = __webpack_require__(22); exports.thunk = thunk_1.default; var toString_1 = __webpack_require__(23); exports.toString = toString_1.default; var unless_1 = __webpack_require__(24); exports.unless = unless_1.default; var when_1 = __webpack_require__(25); exports.when = when_1.default; var T = constant_1.default(true); exports.T = T; var F = constant_1.default(false); exports.F = F; var notPredicate = function (predicate) { return function (a) { return not_1.default(predicate(a)); }; }; exports.notPredicate = notPredicate; /***/ }), /* 7 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Returns `true` if both arguments are `true`; `false` otherwise. * @since v0.1.0 * @param a * @param b * @return */ var and = function (a, b) { return a && b; }; exports.default = and; /***/ }), /* 8 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Returns a new function that will return the given value.Note that for * non-primitives the value returned is a reference to the original value. * * This function is known as `const`, `always`, or `K` (for K combinator) in * other languages and libraries. * @since v0.1.0 * @param value -The value to wrap in a function * @return The function return value */ var constant = function (value) { return function () { return value; }; }; exports.default = constant; /***/ }), /* 9 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * * @param fnA * @param fnB */ var compose = function (fnA, fnB) { return function (x) { return fnA(fnB(x)); }; }; exports.default = compose; /***/ }), /* 10 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var create_1 = __webpack_require__(4); var getName_1 = __webpack_require__(0); /** * Returns a new function which is equivalent to the given function, * except that the new function's length property is equal to given length. * This does not limit the function to using that number of arguments. * It's only effect is on the reported length. * @since v0.1.0 * @keywords wrap * @param {number} length -The function parameter's number * @param {Function} fn - The function be wrap * @param {boolean} isNew - True register new Function * @return {Function} New function */ var arity = function (length, fn, isNew) { if (isNew === void 0) { isNew = false; } /* istanbul ignore next */ length = length < 0 ? 0 : length; var parameters = new Array(length); for (var i = 0; i < length; ++i) { parameters[i] = 'a' + i; } var params = parameters.join(); var code = isNew ? "return function (" + params + ") { return new fn( " + params + "); };" : "return function (" + params + ") { return fn.apply(this, arguments); };"; return create_1.default(getName_1.default(fn), ['fn'], code)(fn); }; exports.default = arity; /***/ }), /* 11 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var symbols_1 = __webpack_require__(3); /** * Return true when the input value is a Placeholder,otherwise false * @param a */ var isPlaceholder = function (a) { return a === symbols_1.__; }; exports.default = isPlaceholder; /***/ }), /* 12 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var curry_1 = __webpack_require__(5); /** * Curry a function from right * @param fn */ var curryR = function (fn) { return curry_1.default(fn, true); }; exports.default = curryR; /***/ }), /* 13 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Returns the second argument if it is not `null`, `undefined` or `NaN`; * otherwise the first argument is returned. * * @since v0.1.0 * @param defaultValue The default value. * @param value `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`. * @return The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value */ var defaultTo = function (defaultValue, value) { return value == null || value !== value ? defaultValue : value; }; exports.default = defaultTo; /***/ }), /* 14 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * return the input value * @param x */ var identity = function (x) { return x; }; exports.default = identity; /***/ }), /* 15 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var curryN_1 = __webpack_require__(2); var ifElse = function (condition, onTrue, onFalse) { return curryN_1.default(function _ifElse() { return condition.apply(null, arguments) ? onTrue.apply(null, arguments) : onFalse.apply(null, arguments); }, Math.max(condition.length, onTrue.length, onFalse.length)); }; exports.default = ifElse; /***/ }), /* 16 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * predicate is nothing * @param a */ var isNothing = function (a) { return a == null; }; exports.default = isNothing; /***/ }), /* 17 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert return value with neg when call a predicate function * @param predicate * @return {()=>number} */ var neg = function (predicate) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return -predicate.apply(null, args); }; }; exports.default = neg; /***/ }), /* 18 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * A function that returns the `!` of its argument. It will return `true` when * passed false-y value, and `false` when passed a truth-y one. * * @since v0.1.0 * @param {*} a any value * @return {Boolean} the logical inverse of passed argument. * @example * * not(true); //=> false * not(false); //=> true * not(0); //=> true * not(1); //=> false */ var not = function (a) { return !a; }; exports.default = not; /***/ }), /* 19 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * The function express empty operate */ var noop = function () { }; exports.default = noop; /***/ }), /* 20 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Returns `true` if both arguments are `true`; `false` otherwise. * @since v0.1.0 * @param a * @param b * @return the first argument if it is falsy, otherwise the second argument. */ var or = function (a, b) { return a || b; }; exports.default = or; /***/ }), /* 21 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * swap a given function 's params * @param fn */ var swap = function (fn) { return function (a, b) { return fn(b, a); }; }; exports.default = swap; /***/ }), /* 22 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * The convert callback function to promise function * @param target * @param context * @param resolver * @return {Promise<T>} */ var thunk = function (target, context, resolver) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return new Promise(function (resolve, reject) { target.apply(context, Array.prototype.slice.call(args).concat([function (err, result) { if (err) { reject(err); } else if (resolver) { resolver.apply(context, args); } else { resolve(result); } }])); }); }; }; exports.default = thunk; /***/ }), /* 23 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var getName_1 = __webpack_require__(0); /** * Convert a function to string * @param fn * @return {string} */ var toString = function (fn) { var fnName = getName_1.default(fn); var nameSuffix = fnName ? ': ' + fnName : ''; return '[Function' + nameSuffix + ']'; }; exports.default = toString; /***/ }), /* 24 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Tests the final argument by passing it to the given predicate function. If * the predicate is not satisfied, the function will return the result of * calling the `whenFalseFn` function with the same argument. If the predicate * is satisfied, the argument is returned as is. * * @since v0.1.0 * @param {Function} pred A predicate function * @param {Function} whenFalseFn A function to invoke when the `pred` evaluates * to a falsy value. * @param {*} x An object to test with the `pred` function and * pass to `whenFalseFn` if necessary. * @return {*} Either `x` or the result of applying `x` to `whenFalseFn`. * @example * * let safeInc = _unless(R.isNil, R.inc); * safeInc(null); //=> null * safeInc(1); //=> 2 */ var unless = function (pred, whenFalseFn, x) { return pred(x) ? x : whenFalseFn(x); }; exports.default = unless; /***/ }), /* 25 */ /***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true }); /** * Tests the final argument by passing it to the given predicate function. If * the predicate is satisfied, the function will return the result of calling * the `whenTrueFn` function with the same argument. If the predicate is not * satisfied, the argument is returned as is. * * @since v0.1.0 * @param {Function} pred A predicate function * @param {Function} whenTrueFn A function to invoke when the `condition` * evaluates to a truthy value. * @param {*} x An object to test with the `pred` function and * pass to `whenTrueFn` if necessary. * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`. * @example * * // truncate :: String -> String * var truncate = R.when( * R.propSatisfies(R.gt(R.__, 10), 'length'), * R.pipe(R.take(10), R.append('…'), R.join('')) * ); * truncate('12345'); //=> '12345' * truncate('0123456789ABC'); //=> '0123456789…' */ var when = function (pred, whenTrueFn, x) { return pred(x) ? whenTrueFn(x) : x; }; exports.default = when; /***/ }) /******/ ]); }); //# sourceMappingURL=mocoolka-function.js.map