Code coverage report for src/Common/I18N/String.js

Statements: 100% (23 / 23)      Branches: 100% (2 / 2)      Functions: 100% (3 / 3)      Lines: 100% (23 / 23)      Ignored: none     

All files » src/Common/I18N/ » String.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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106    1                     1                               1   255 255 255 255   255   255   255                                 1   104 104 104                         104   255   255       1           1             104           1 1   1  
"use strict";
 
var _       = require("lodash"),
    Safe    = require("../Safe");
 
 
/**
 *
 * Internal methdos
 *
 * @type {Object}
 *
 */
var Internal = {};
 
/**
 *
 * Apply the template. The scope of the function is the string.
 *
 * @throws {Error} If template data its missing or wrong
 *
 * @param  {String} name
 * @param  {Object} data
 * @param  {Object} messages
 * @param  {Object} customMessages
 *
 * @return {String}
 *
 */
Internal.applyTemplate = function(name, data, messages, customMessages){
 
    name            = Safe.string(name);
    data            = Safe.object(data);
    messages        = Safe.object(messages);
    customMessages  = Safe.object(customMessages);
 
    messages = _.extend({}, messages, customMessages);
 
    var tmplStr = Safe.string(messages[name]) || name;
 
    return _.template(tmplStr)(data);
 
};
 
/**
 *
 * Template String class. Each string is identified by a name. The template string
 * can be changed when calling toString.
 *
 * @param {String} name
 * @param {Object} data
 * @param {Object} messages
 *
 * @example
 * var str = new I18nString("asas", {}, {});
 *
 */
var I18NString = function(name, data, messages){
 
    name        = Safe.string(name);
    data        = Safe.object(data);
    messages    = Safe.object(messages);
 
    /**
     *
     * toString()
     *
     * @throws {Error} If template is not correct
     *
     * @param  {String} messages
     * @param  {Object} data
     * @return {String}
     *
     */
    this.toString = function(customMessages){
 
        try{
 
            return Internal.applyTemplate.apply(this, [ name, data, messages, customMessages ]);
 
        } catch(e){
 
            var error = _.template(
                "Error on template '<%= name %>': <%= error %>")({
                    name: name,
                    error: e.message
                });
 
            throw new Error(error);
 
        }
 
    };
 
    /// Initialize string by calling .super()
    String.call( this, this.toString() );
 
};
 
/// inherit from String
/* jshint -W053 */
I18NString.prototype = new String();
I18NString.prototype.constructor = I18NString;
 
module.exports = I18NString;