| | |
| 1 | 1 | (function () { |
| 2 | 1 | "use strict"; |
| 3 | | |
| 4 | | // http://jsperf.com/apply-usages |
| 5 | | |
| 6 | 1 | var slice = Array.prototype.slice, |
| 7 | | __pass, |
| 8 | | __debounce, |
| 9 | | __every, |
| 10 | | __funnel; |
| 11 | | |
| 12 | | // |
| 13 | | // Function |
| 14 | | // |
| 15 | | |
| 16 | | /** |
| 17 | | * Returns a closure with arguments and bind |
| 18 | | * |
| 19 | | * credits - mootools |
| 20 | | * |
| 21 | | * @note: If you want to send null as arguments use: .pass([null], ?) |
| 22 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 23 | | * @param {Array} args The arguments passed |
| 24 | | * @return {Function} closure |
| 25 | | */ |
| 26 | 1 | module.exports.pass = __pass = function (fn, args, bind) { |
| 27 | 38 | if (args !== null && args !== undefined) { |
| 28 | 37 | args = slice.call(args); |
| 29 | | } |
| 30 | | |
| 31 | 38 | return function () { |
| 32 | 35 | var fargs = args || arguments, |
| 33 | | fbind = bind || fn; |
| 34 | | |
| 35 | 35 | if (fargs.length) { |
| 36 | 5 | return fn.apply(fbind, fargs); |
| 37 | | } |
| 38 | 30 | return fn.call(fbind); |
| 39 | | }; |
| 40 | | }; |
| 41 | | |
| 42 | | /** |
| 43 | | * Returns a closure with the given arguments before the ones you send at the call |
| 44 | | * |
| 45 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 46 | | * @param {Array} args The arguments passed |
| 47 | | * @return {Function} closure |
| 48 | | */ |
| 49 | 1 | module.exports.prepend = function (fn, args, bind) { |
| 50 | 2 | var max = args.length; |
| 51 | | |
| 52 | 2 | args = args ? slice.call(args) : []; |
| 53 | | |
| 54 | 2 | return function () { |
| 55 | 2 | var fbind = bind || fn, |
| 56 | | cloned = [], |
| 57 | | i; |
| 58 | | |
| 59 | 2 | for (i = 0; i < max; ++i) { |
| 60 | 4 | cloned.push(args[i]); |
| 61 | | } |
| 62 | 2 | for (i = 0; i < arguments.length; ++i) { |
| 63 | 1 | cloned.push(arguments[i]); |
| 64 | | } |
| 65 | | |
| 66 | 2 | return fn.apply(fbind, cloned); |
| 67 | | }; |
| 68 | | }; |
| 69 | | |
| 70 | | /** |
| 71 | | * Returns a closure with the given arguments after the ones you send at the call |
| 72 | | * |
| 73 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 74 | | * @param {Array} args The arguments passed |
| 75 | | * @return {Function} closure |
| 76 | | */ |
| 77 | 1 | module.exports.append = function (fn, args, bind) { |
| 78 | 4 | var max = args.length; |
| 79 | | |
| 80 | 4 | args = args ? slice.call(args) : []; |
| 81 | | |
| 82 | 4 | return function () { |
| 83 | 11 | var fbind = bind || fn, |
| 84 | | cloned = [], |
| 85 | | i; |
| 86 | | |
| 87 | 11 | for (i = 0; i < arguments.length; ++i) { |
| 88 | 1 | cloned.push(arguments[i]); |
| 89 | | } |
| 90 | 11 | for (i = 0; i < max; ++i) { |
| 91 | 13 | cloned.push(args[i]); |
| 92 | | } |
| 93 | | |
| 94 | 11 | return fn.apply(fbind, cloned); |
| 95 | | }; |
| 96 | | }; |
| 97 | | |
| 98 | | /** |
| 99 | | * Delays the execution of a function by a specified duration. |
| 100 | | * |
| 101 | | * credits - mootools |
| 102 | | * |
| 103 | | * @note use: clearTimeout to stop the scheduled execution |
| 104 | | * @param {Number} delay_ms |
| 105 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 106 | | * @param {Array} args The arguments passed |
| 107 | | * @return {Number} the interval so you can clearTimeout |
| 108 | | */ |
| 109 | 1 | module.exports.delay = function (fn, delay_ms, bind, args) { |
| 110 | 0 | return setTimeout(__pass(fn, args, bind || fn), delay_ms); |
| 111 | | }; |
| 112 | | |
| 113 | | /** |
| 114 | | * Executes a function in the specified intervals of time. |
| 115 | | * Periodic execution can be stopped using the clearInterval function. |
| 116 | | * |
| 117 | | * credits - mootools ( |
| 118 | | * |
| 119 | | * @param {Number} periodical_ms |
| 120 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 121 | | * @param {Array} args The arguments passed |
| 122 | | * @return {Number} the interval so you can clearInterval |
| 123 | | */ |
| 124 | 1 | module.exports.periodical = function (fn, periodical_ms, bind, args) { |
| 125 | 1 | return setInterval(__pass(fn, args, bind || fn), periodical_ms); |
| 126 | | }; |
| 127 | | |
| 128 | | /** |
| 129 | | * Returns a function, that, as long as it continues to be invoked, will not |
| 130 | | * be triggered. The function will be called after it stops being called for |
| 131 | | * N milliseconds. |
| 132 | | * |
| 133 | | * credits to underscore |
| 134 | | * |
| 135 | | * @param {Number} wait_ms |
| 136 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 137 | | * @param {Array} args The arguments passed |
| 138 | | */ |
| 139 | 1 | module.exports.debounce = __debounce = function (fn, wait_ms, args, bind) { |
| 140 | 2 | var timeout; |
| 141 | | |
| 142 | 2 | return function () { |
| 143 | 35 | var later = __pass(fn, args || arguments, bind || fn); |
| 144 | | |
| 145 | 35 | clearTimeout(timeout); |
| 146 | 35 | timeout = setTimeout(later, wait_ms); |
| 147 | | }; |
| 148 | | }; |
| 149 | | |
| 150 | | /** |
| 151 | | * 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. |
| 152 | | * |
| 153 | | * credits to underscore |
| 154 | | * |
| 155 | | * @param {Number} wait_ms |
| 156 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 157 | | * @param {Array} args The arguments passed |
| 158 | | */ |
| 159 | 1 | module.exports.throttle = function (fn, wait_ms, bind, args) { |
| 160 | 1 | var timeout, |
| 161 | | throttling, |
| 162 | | more, |
| 163 | 1 | whenDone = __debounce(function () { more = throttling = false; }, wait_ms); |
| 164 | | |
| 165 | 1 | return function () { |
| 166 | 29 | var fargs = args || arguments, |
| 167 | | fbind = bind || fn, |
| 168 | | later = function () { |
| 169 | 2 | timeout = null; |
| 170 | | |
| 171 | 2 | if (more) { |
| 172 | 2 | fn.apply(fbind, fargs); |
| 173 | | } |
| 174 | | |
| 175 | 2 | whenDone(); |
| 176 | | }; |
| 177 | | |
| 178 | 29 | if (!timeout) { |
| 179 | 2 | timeout = setTimeout(later, wait_ms); |
| 180 | | } |
| 181 | | |
| 182 | 29 | if (throttling) { |
| 183 | 28 | more = true; |
| 184 | | } else { |
| 185 | 1 | fn.apply(fbind, fargs); |
| 186 | | } |
| 187 | | |
| 188 | 29 | whenDone(); |
| 189 | 29 | throttling = true; |
| 190 | | }; |
| 191 | | }; |
| 192 | | |
| 193 | | /** |
| 194 | | * Returns a function that will be executed at most one time, no matter how |
| 195 | | * often you call it. Useful for lazy initialization. |
| 196 | | * |
| 197 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 198 | | * @param {Array} args The arguments passed |
| 199 | | * @returns the value of the first execution |
| 200 | | */ |
| 201 | 1 | module.exports.once = function (fn, bind, args) { |
| 202 | 1 | var first = false, |
| 203 | | memo; |
| 204 | | |
| 205 | 1 | return function () { |
| 206 | 2 | var fargs = args || arguments, |
| 207 | | fbind = bind || fn; |
| 208 | | |
| 209 | 2 | if (first) { |
| 210 | 1 | return memo; |
| 211 | | } |
| 212 | | |
| 213 | 1 | first = true; |
| 214 | 1 | return (memo = fn.apply(fbind, fargs)); |
| 215 | | }; |
| 216 | | }; |
| 217 | | |
| 218 | | /** |
| 219 | | * Returns a function that will be executed every <ntimes> times at most of <max_executions> |
| 220 | | * |
| 221 | | * @param {Number} ntimes every n times |
| 222 | | * @param {Number} max_executions maximum number of executions |
| 223 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 224 | | * @param {Array} args The arguments passed |
| 225 | | * @returns the value returned by given function or undefined if it's not executed. |
| 226 | | */ |
| 227 | 1 | module.exports.every = __every = function (fn, ntimes, max_executions, bind, args) { |
| 228 | 3 | var attempts = 0, |
| 229 | | calls = 0; |
| 230 | | |
| 231 | 3 | return function () { |
| 232 | 26 | var fargs = args || arguments, |
| 233 | | fbind = bind || fn; |
| 234 | | |
| 235 | 26 | if (++attempts < ntimes || calls >= max_executions) { |
| 236 | 17 | return; |
| 237 | | } |
| 238 | 9 | attempts = 0; |
| 239 | 9 | ++calls; |
| 240 | | |
| 241 | 9 | return fn.apply(fbind, fargs); |
| 242 | | }; |
| 243 | | }; |
| 244 | | |
| 245 | | /** |
| 246 | | * Returns a function that will be executed after being called n times |
| 247 | | * |
| 248 | | * @param {Number} ntimes |
| 249 | | */ |
| 250 | 1 | module.exports.after = function (fn, ntimes, bind, args) { |
| 251 | 1 | return __every(fn, ntimes, 1, bind, args); |
| 252 | | }; |
| 253 | | |
| 254 | | /** |
| 255 | | * Returns a function that will be executed ntimes with a given delay between them |
| 256 | | * If delay is false is cero will be executed right now |
| 257 | | * If first_delay is false is cero will be executed right now |
| 258 | | * |
| 259 | | * @note for delay setInterval is used to be sure that each function execution has the given delay |
| 260 | | * |
| 261 | | * @param {Number} ntimes how many times will be executed |
| 262 | | * @param {Number} delay delay between first and the next execution |
| 263 | | * @param {Number} first_delay delay between the call and first execution |
| 264 | | * @param {Function} last_func function to call when all is done with one arguments, an array of the returned |
| 265 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 266 | | * @param {Array} args The arguments passed |
| 267 | | */ |
| 268 | 1 | module.exports.nth = function (fn, ntimes, delay, first_delay, last_func, bind, args) { |
| 269 | 3 | var outputs = [], |
| 270 | | interval; |
| 271 | | |
| 272 | 3 | delay = delay === undefined ? false : delay; |
| 273 | 3 | first_delay = first_delay === undefined ? false : first_delay; |
| 274 | | |
| 275 | | |
| 276 | 3 | return function () { |
| 277 | 3 | var times = 1, |
| 278 | | fargs = args || arguments, |
| 279 | | fbind = bind || fn; |
| 280 | | |
| 281 | | // allow 0 |
| 282 | 3 | if (first_delay === false) { |
| 283 | 2 | outputs.push(fn.apply(fbind, fargs)); |
| 284 | | |
| 285 | 2 | if (delay === false) { |
| 286 | 1 | for (times = 1; times < ntimes; ++times) { |
| 287 | 4 | outputs.push(fn.apply(fbind, fargs)); |
| 288 | | } |
| 289 | | |
| 290 | 1 | last_func && last_func(outputs); |
| 291 | | } else { |
| 292 | 1 | interval = setInterval(function () { |
| 293 | 4 | outputs.push(fn.apply(fbind, fargs)); |
| 294 | 4 | if (++times === ntimes) { |
| 295 | 1 | clearInterval(interval); |
| 296 | | |
| 297 | 1 | last_func && last_func(outputs); |
| 298 | | } |
| 299 | | }, delay); |
| 300 | | } |
| 301 | | |
| 302 | | } else { |
| 303 | 1 | setTimeout(function () { |
| 304 | 1 | outputs.push(fn.apply(fbind, fargs)); |
| 305 | | |
| 306 | 1 | if (delay === false) { |
| 307 | 0 | for (times = 1; times < ntimes; ++times) { |
| 308 | 0 | outputs.push(fn.apply(fbind, fargs)); |
| 309 | | } |
| 310 | 0 | last_func && last_func(outputs); |
| 311 | | } else { |
| 312 | 1 | interval = setInterval(function () { |
| 313 | 4 | outputs.push(fn.apply(fbind, fargs)); |
| 314 | 4 | if (++times === ntimes) { |
| 315 | 1 | clearInterval(interval); |
| 316 | | |
| 317 | 1 | last_func && last_func(outputs); |
| 318 | | } |
| 319 | | }, delay); |
| 320 | | } |
| 321 | | }, first_delay); |
| 322 | | } |
| 323 | | }; |
| 324 | | }; |
| 325 | | |
| 326 | | /** |
| 327 | | * Create a function, when called invoke this. |
| 328 | | * If you called again and it's in execution, the execution is queued. So only (max) execution at time |
| 329 | | * A new argument is sent to your function, a callback no notify the execution ended |
| 330 | | * |
| 331 | | * @param {Number} max How many execution are allowed in parallel |
| 332 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 333 | | * @param {Array} args The arguments passed |
| 334 | | * @param {String} where append / prepend, the new callback argument |
| 335 | | */ |
| 336 | 1 | module.exports.funnel = __funnel = function (fn, max, bind, args, where) { |
| 337 | 2 | where = where || "append"; |
| 338 | | |
| 339 | 2 | var in_execution = 0, |
| 340 | | fifo = [], |
| 341 | | self, |
| 342 | | check = function () { |
| 343 | 9 | var el; |
| 344 | 9 | if (fifo.length === 0) { |
| 345 | 4 | --in_execution; |
| 346 | | } else { |
| 347 | 5 | el = fifo.shift(); |
| 348 | 5 | self.apply(el.bind, el.args); |
| 349 | | } |
| 350 | | }; |
| 351 | | |
| 352 | 2 | self = module.exports[where](fn, [check]); |
| 353 | | |
| 354 | 2 | return function () { |
| 355 | 9 | var fargs = args || (arguments.length ? slice.call(arguments) : []), |
| 356 | | fbind = bind || fn; |
| 357 | | |
| 358 | 9 | if (in_execution === max) { |
| 359 | 5 | fifo.push({ |
| 360 | | args: fargs, |
| 361 | | bind: fbind |
| 362 | | }); |
| 363 | 5 | return; |
| 364 | | } |
| 365 | | |
| 366 | 4 | ++in_execution; |
| 367 | 4 | self.apply(fbind, fargs); |
| 368 | | |
| 369 | | }; |
| 370 | | }; |
| 371 | | |
| 372 | | |
| 373 | | /** |
| 374 | | * Create a function that can only be call once in parallel, the followings will be queued |
| 375 | | * A new argument is sent to your function, a callback no notify the execution ended |
| 376 | | * |
| 377 | | * @param {Mixed} bind Changes the scope of this within the returned function |
| 378 | | * @param {Array} args The arguments passed |
| 379 | | * @param {String} where append or prepend, where the callback will be. |
| 380 | | */ |
| 381 | 1 | module.exports.single = function (fn, bind, args, where) { |
| 382 | 1 | return __funnel(fn, 1, bind, args, where); |
| 383 | | }; |
| 384 | | |
| 385 | | // from: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ |
| 386 | 1 | function hash(args) { |
| 387 | 32 | var str = JSON.stringify(args), |
| 388 | | ihash = 0, |
| 389 | | chc, |
| 390 | | i; |
| 391 | | |
| 392 | 32 | if (str.length === 0) { |
| 393 | 0 | return ihash; |
| 394 | | } |
| 395 | | |
| 396 | 32 | for (i = 0; i < str.length; i++) { |
| 397 | 240 | chc = str.charCodeAt(i); |
| 398 | 240 | ihash = ((ihash << 5) - ihash) + chc; |
| 399 | 240 | ihash = ihash & ihash; // Convert to 32bit integer |
| 400 | | } |
| 401 | | |
| 402 | 32 | return ihash; |
| 403 | | } |
| 404 | | |
| 405 | | //Function.prototype.cache |
| 406 | | /** |
| 407 | | * Creates a function that memoizes the result of func for a given time. |
| 408 | | * 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. |
| 409 | | * |
| 410 | | * @param {Number} cache_time_ms Cache expiration microseconds, -1 to infinite |
| 411 | | * @param {Mixed} bind The arguments passed |
| 412 | | * @param {function} hash_function function used to hash the arguments, most simple one: JSON.stringify |
| 413 | | */ |
| 414 | 1 | module.exports.cache = function (fn, cache_time_ms, bind, hash_function) { |
| 415 | 1 | var cache = {}; |
| 416 | | |
| 417 | 1 | hash_function = hash_function || hash; |
| 418 | | |
| 419 | 1 | return function () { |
| 420 | 32 | var args = [], |
| 421 | | hash_code, |
| 422 | | ts = cache_time_ms === -1 ? 0 : +(new Date()), |
| 423 | | fbind = bind || fn, |
| 424 | | i, |
| 425 | | max = arguments.length; |
| 426 | | |
| 427 | 32 | if (max) { |
| 428 | 16 | for (i = 0, max = arguments.length; i < max; ++i) { |
| 429 | 16 | args.push(arguments[i]); |
| 430 | | } |
| 431 | | |
| 432 | | } |
| 433 | | |
| 434 | 32 | hash_code = hash_function(args); |
| 435 | | |
| 436 | 32 | if (cache[hash_code] !== undefined) { |
| 437 | 30 | if (cache[hash_code].expire === -1 || cache[hash_code].expire > ts) { |
| 438 | 28 | return cache[hash_code].ret; |
| 439 | | } else { |
| 440 | 2 | delete cache[hash_code]; |
| 441 | | } |
| 442 | | } |
| 443 | | |
| 444 | 4 | cache[hash_code] = { |
| 445 | | ret: fn.apply(fbind, args), |
| 446 | | expire: ts + cache_time_ms |
| 447 | | }; |
| 448 | | |
| 449 | 4 | return cache[hash_code]; |
| 450 | | |
| 451 | | }; |
| 452 | | }; |
| 453 | | |
| 454 | | /** |
| 455 | | * Returns a function that is the composition of a list of functions, each |
| 456 | | * consuming the return value of the function that follows. |
| 457 | | * |
| 458 | | * credits - almost underscore |
| 459 | | * |
| 460 | | * @param {Function} any number of functions |
| 461 | | * @returns {Array} all returned values |
| 462 | | */ |
| 463 | 1 | module.exports.compose = function () { |
| 464 | 1 | var funcs = arguments; |
| 465 | | |
| 466 | 1 | return function () { |
| 467 | 1 | var i, |
| 468 | | ret = []; |
| 469 | | |
| 470 | 1 | for (i = 0; i < funcs.length; ++i) { |
| 471 | 3 | if (ret.length === 0) { |
| 472 | 1 | ret.push(funcs[i]()); |
| 473 | | } else { |
| 474 | 2 | ret.push(funcs[i](ret[ret.length - 1])); |
| 475 | | } |
| 476 | | } |
| 477 | | |
| 478 | 1 | return ret; |
| 479 | | }; |
| 480 | | }; |
| 481 | | |
| 482 | | /** |
| 483 | | * Returns a function that is the composition of a list of functions, |
| 484 | | * returns an array with all returned values by each function |
| 485 | | * |
| 486 | | * @param {Function} any number of functions |
| 487 | | * @returns {Array} all returned values |
| 488 | | */ |
| 489 | 1 | module.exports.sequencial = function () { |
| 490 | 1 | var funcs = arguments; |
| 491 | | |
| 492 | 1 | return function () { |
| 493 | 1 | var i, |
| 494 | | ret = []; |
| 495 | | |
| 496 | 1 | for (i = 0; i < funcs.length; ++i) { |
| 497 | 3 | ret.push(funcs[i]()); |
| 498 | | } |
| 499 | | |
| 500 | 1 | return ret; |
| 501 | | }; |
| 502 | | }; |
| 503 | | |
| 504 | | }()); |