Code coverage report for string/replace.js

Statements: 92.86% (13 / 14)      Branches: 83.33% (5 / 6)      Functions: 100% (3 / 3)      Lines: 92.31% (12 / 13)     

All files » string/ » replace.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331         1 7 7 7   7     7       7 7     10         7     1      
define(['../lang/toString', '../lang/toArray'], function (toString, toArray) {
 
    /**
     * Replace string(s) with the replacement(s) in the source.
     */
    function replace(str, search, replace) {
        str = toString(str);
        search = toArray(search);
        replace = toArray(replace);
 
        var searchLength = search.length,
            replaceLength = replace.length;
 
        Iif (replaceLength !== 1 && searchLength !== replace.length) {
            throw new Error('Unequal number of searches and replacements');
        }
 
        var i = -1;
        while (++i < searchLength) {
            // Use the first replacement for all searches if only one
            // replacement is provided
            str = str.replace(
                search[i],
                replace[(replaceLength === 1) ? 0 : i]);
        }
 
        return str;
    }
 
    return replace;
 
});