Module:Func

Revision as of 09:56, 23 October 2022 by NARA>WOSlinker (remove no globals link)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Func/doc

--------------------------------------------------------------------------------
-- This module provies helper functions for manipulating Lua functions.
--
-- @module func
-- @alias  p
-- @author ExE Boss
--------------------------------------------------------------------------------

require("strict");
local checkTypeMulti = require("libraryUtil").checkTypeMulti;

local p = {};

--------------------------------------------------------------------------------
-- Creates a bound function that calls `func` with the varargs passed to `bind`
-- preceding the varargs passed to the newly created bound function.
--
-- @param {function} func
-- @param {any} ...
-- @return {function}
-- @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
--------------------------------------------------------------------------------
function p.bind(func, ...)
	checkTypeMulti("bind", 1, func, { "table", "function" });

	local len = select("#", ...);
	local args = {...};

	return function(...)
		return func(unpack(args, 1, len), ...);
	end
end

return p;