Introduction
Some javascript function enhacements. Arguments:pass, prepend, append. Timing: delay, periodical, debounce, throttle, once
var Fun = ;// Returns a closure with arguments and bindFun// Returns a closure with the given arguments before the ones you send at the callFun// Returns a closure with the given arguments after the ones you send at the callFun// Delays the execution of a function by a specified duration.Fun// Executes a function in the specified intervals of time.// Periodic execution can be stopped using the clearInterval function.Fun// Returns a function, that, as long as it continues to be invoked, will not// be triggered. The function will be called after it stops being called for// N milliseconds.Fun// Creates and returns a new, throttled version of the passed function,// that, when invoked repeatedly, will only actually call the original// function at most once per every wait milliseconds. Useful for// rate-limiting events that occur faster than you can keep up with.Fun// Returns a function that will be executed at most one time, no matter how// often you call it. Useful for lazy initialization.Fun// Returns a function that will be executed every <ntimes> times at most of <max_executions>Fun// Returns a function that will be executed after being called n timesFun// Returns a function that will be executed ntimes with a given delay between them// If delay is false is cero will be executed right now// If first_delay is false is cero will be executed right nowFun// Create a function, when called invoke this.// If you called again and it's in execution, the execution is queued. So only (max) execution at time// A new argument is sent to your function, a callback no notify the execution endedFun// Create a function that can only be call once in parallel, the followings will be queued// A new argument is sent to your function, a callback no notify the execution endedFun// Creates a function that memoizes the result of func for a given time.// If hash_function is provided it will be used to determine the cache// key for storing the result based on the arguments provided to the memoized function.Fun// for compatibility with old browsers @lib/functions-compat.jsFunbind
In action!
Check the test/test.js for more examples.
var {console;console;}test { return "[test Function]";};// Function.args: prepend given argumentsvar t2 = test;;// > arguments: { '0': 'say', '1': 'hello' }// > this: { iamthis: true };// > arguments: { '0': 'say', '1': 'hello', '2': 'thidparam!' }// > this: { iamthis: true }// Function.pass: create a function with given args and any call you will have the same argumentsvar t3 = test;;// arguments: { '0': 'dont mind your args' }// this: { whoami: 'root' };// arguments: { '0': 'dont mind your args' }// this: { whoami: 'root' }// Function.delay execute the funtion in X milisecondsvar del = t2;;// Function.periodical execute the funtion every X milisecondsvar inter = t2;;// Function.throttle execute a function once every X miliseconds, dont mind how many time you call it.var t4 = test;var inter = t4;;// 10 times// > this: [test Function]// > arguments: {}
this gives you an idea :) check the test/test.js for more examples
Install
With npm do:
npm install function-enhancements
test (travis-ci ready!)
npm test
// or
cd /test
node test.js
license
MIT.