Code coverage report for lib/util.js

Statements: 100% (16 / 16)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (16 / 16)      Ignored: none     

All files » lib/ » util.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 33 34 35 36 37 38 39 40 41 42 43 44 45            1 1 3 2     1       1 1 6 6         1 16       1 7       1 8     1            
/**
 * @file util methods
 * @author nighca<nighca@live.cn>
 */
 
// copy properties from src to target
var extend = function(target, src){
    for(var key in src){
        if(src.hasOwnProperty(key)){
            target[key] = src[key];
        }
    }
    return target;
};
 
// 'a${x}c', {x:'b'} -> 'abc'
var format = function(template, vars) {
    return template.replace(/\$\{([^\{\}]*)\}/g, function(_, name) {
        var value = vars[name.trim()];
        return value == null ? '' : value + '';
    });
};
 
// repeat a string in given times
var repeat = function(str, num){
    return Array.prototype.join.call({ length: num + 1 }, str);
};
 
// generate indent content
var indent = function(level, type, size){
    return repeat(type === 'tab' ? '\t' : repeat(' ', size), level);
};
 
// is in an array
var isIn = function(obj, arr){
    return arr.indexOf(obj) >= 0;
};
 
module.exports = {
    extend: extend,
    format: format,
    repeat: repeat,
    indent: indent,
    isIn: isIn
};