{"size": 1335, "ext": "js", "max_stars_repo_path": "src/privates/_asPartial.js", "max_stars_repo_name": "ascartabelli/lamb", "max_stars_repo_stars_event_min_datetime": "2015-04-10T13:58:27.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-23T19:37:48.000Z", "max_issues_repo_path": "src/privates/_asPartial.js", "max_issues_repo_name": "ascartabelli/lamb", "max_issues_repo_issues_event_min_datetime": "2016-09-03T05:58:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-28T22:35:17.000Z", "max_forks_repo_path": "src/privates/_asPartial.js", "max_forks_repo_name": "ascartabelli/lamb", "max_forks_repo_forks_event_min_datetime": "2015-04-10T10:22:08.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-06T20:45:56.000Z"}
{"max_stars_count": 19, "max_issues_count": 8, "max_forks_count": 7, "avg_line_length": 29.0217391304, "max_line_length": 96, "alphanum_fraction": 0.5595505618}
import __ from "../core/__";

/**
 * Keeps building a partial application of the received function as long
 * as it's called with placeholders; applies the original function to
 * the collected parameters otherwise.<br/>
 * The function checks only the public placeholder to gain a little performance
 * as no function in Lamb is built with {@link module:lamb.asPartial|asPartial}.
 * @private
 * @param {Function} fn
 * @param {Array} argsHolder
 * @returns {Function|*}
 */
function _asPartial (fn, argsHolder) {
    return function () {
        var argsLen = arguments.length;
        var lastIdx = 0;
        var newArgs = [];

        for (var i = 0, len = argsHolder.length, boundArg; i < len; i++) {
            boundArg = argsHolder[i];
            newArgs[i] = boundArg === __ && lastIdx < argsLen ? arguments[lastIdx++] : boundArg;
        }

        while (lastIdx < argsLen) {
            newArgs[i++] = arguments[lastIdx++];
        }

        for (i = 0; i < argsLen; i++) {
            if (arguments[i] === __) {
                return _asPartial(fn, newArgs);
            }
        }

        for (i = 0, len = newArgs.length; i < len; i++) {
            if (newArgs[i] === __) {
                newArgs[i] = void 0;
            }
        }

        return fn.apply(this, newArgs);
    };
}

export default _asPartial;
