xref: /dokuwiki/lib/scripts/helpers.js (revision 193e22801d763e715f03f241b135a2e249c6132e)
1/**
2 * Various helper functions
3 */
4
5/**
6 * A PHP-style substr_replace
7 *
8 * Supports negative start and length and omitting length, but not
9 * str and replace arrays.
10 * See http://php.net/substr-replace for further documentation.
11 */
12function substr_replace(str, replace, start, length) {
13    var a2, b1;
14    a2 = (start < 0 ? str.length : 0) + start;
15    if (typeof length === 'undefined') {
16        length = str.length - a2;
17    } else if (length < 0 && start < 0 && length <= start) {
18        length = 0;
19    }
20    b1 = (length < 0 ? str.length : a2) + length;
21    return str.substring(0, a2) + replace + str.substring(b1);
22}
23
24/**
25 * Bind variables to a function call creating a closure
26 *
27 * Use this to circumvent variable scope problems when creating closures
28 * inside a loop
29 *
30 * @author  Adrian Lang <lang@cosmocode.de>
31 * @link    http://www.cosmocode.de/en/blog/gohr/2009-10/15-javascript-fixing-the-closure-scope-in-loops
32 * @param   functionref fnc - the function to be called
33 * @param   mixed - any arguments to be passed to the function
34 * @returns functionref
35 */
36function bind(fnc/*, ... */) {
37    var Aps = Array.prototype.slice,
38    // Store passed arguments in this scope.
39    // Since arguments is no Array nor has an own slice method,
40    // we have to apply the slice method from the Array.prototype
41        static_args = Aps.call(arguments, 1);
42
43    // Return a function evaluating the passed function with the
44    // given args and optional arguments passed on invocation.
45    return function (/* ... */) {
46        // Same here, but we use Array.prototype.slice solely for
47        // converting arguments to an Array.
48        return fnc.apply(this,
49                         static_args.concat(Aps.call(arguments, 0)));
50    };
51}
52