Code coverage report for string/escapeUnicode.js

Statements: 100% (9 / 9)      Branches: 100% (4 / 4)      Functions: 100% (4 / 4)      Lines: 100% (8 / 8)     

All files » string/ » escapeUnicode.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221         1 5 5   29 14       15       1      
define(['../lang/toString'], function(toString) {
 
    /**
     * Escape string into unicode sequences
     */
    function escapeUnicode(str, shouldEscapePrintable){
        str = toString(str);
        return str.replace(/[\s\S]/g, function(ch){
            // skip printable ASCII chars if we should not escape them
            if (!shouldEscapePrintable && (/[\x20-\x7E]/).test(ch)) {
                return ch;
            }
            // we use "000" and slice(-4) for brevity, need to pad zeros,
            // unicode escape always have 4 chars after "\u"
            return '\\u'+ ('000'+ ch.charCodeAt(0).toString(16)).slice(-4);
        });
    }
 
    return escapeUnicode;
 
});