Code coverage report for lib/priv.js

Statements: 100% (34 / 34)      Branches: 96.15% (25 / 26)      Functions: 100% (5 / 5)      Lines: 100% (34 / 34)      Ignored: none     

All files » lib/ » priv.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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591               1             17                           17                                                 1 39 14     25 4     21   21           1                                               1 8 6     2   2 1     1                                       1 13 6     7   7 1     6     6 5 5       1   1     1       1 1    
var Blocks = (function() {
'use strict';
 
/**
 * Blocks: Storage of blocks functions that generats BEMJSON
 * 
 * @constructor
 */
function Blocks() {
    /**
     * Объект для хранения блоков.
     * 
     * @type {Object}
     * @private
     */
    this._methods = {};
 
    /**
     * Неймспейс для библиотек.
     * Сюда можно писать различный функционал для дальнейшего использования.
     *
     * @example
     * ```javascript
     * blocks.lib.helpers = blocks.lib.helpers || {};
     * blocks.lib.helpers.inverse = blocks.lib.helpers.inverse || function(obj) { ... };
     * ```
     * 
     * @type {Object}
     */
    this.lib = {};
}
 
/**
 * Декларация блока по имени.
 *
 * @example
 * ```javascript
 * blocks.declare('header', function(data) {
 *     return {
 *         block: 'header',
 *         content: data.name
 *     }
 * });
 *
 * blocks.declare('utils', {
 *     format: fucntion(number) { ... },
 *     inverse: function(obj) { ... }
 * });
 * ```
 *
 * @param   {String} name   Block name
 * @param   {*}      method Block function
 * @returns {Blocks}
 */
Blocks.prototype.declare = function(name, method) {
    if (!name || typeof name !== 'string') {
        throw new TypeError('Argument `name` must be a string');
    }
 
    if (method === null || method === undefined) {
        throw new TypeError('Argument `method` must not be Null or undefined');
    }
 
    this._methods[name] = method;
 
    return this;
};
 
/**
 * Синоним функции declare
 */
Blocks.prototype.decl = Blocks.prototype.declare;
 
/**
 * Возвращает priv-функцию блока по названию.
 *
 * @example
 * ```javascript
 * blocks.declare('utils', {
 *     format: fucntion(number) { ... },
 *     inverse: function(obj) { ... }
 * });
 *
 * blocks.declare('price', function(data) {
 *     var utils = blocks.get('utils');
 *     return {
 *         block: 'price',
 *         content: utils.format(data.price) + 'руб.'
 *     }
 * });
 * ```
 *
 * @param   {String}    name    Block name
 * @returns {Function}
 */
Blocks.prototype.get = function(name) {
    if (!name || typeof name !== 'string') {
        throw new TypeError('Argument `name` must be a string');
    }
 
    var method = this._methods[name];
 
    if (!method) {
        throw new Error('Priv method `' + name + '` was not declared');
    }
 
    return method;
};
 
/**
 * Выполняет priv-функцию блока, возвращает результат выполнения.
 *
 * @example
 * ```javascript
 * blocks.declare('content', function(data) {
 *     return [
 *         { block: 'logo' },
 *         blocks.exec('header');
 *         blocks.exec('debug', data.debug);
 *     ]
 * });
 * ```
 *
 * @param   {String} name Block name
 * @returns {*}
 */
Blocks.prototype.exec = function(name) {
    if (!name || typeof name !== 'string') {
        throw new TypeError('Argument `name` must be a string');
    }
 
    var method = this._methods[name];
 
    if (!method) {
        throw new Error('Priv method `' + name + '` was not declared');
    }
 
    var methodType = typeof method,
        startsWithVowel;
 
    if (methodType !== 'function') {
        startsWithVowel = ['object', 'undefined'].indexOf(methodType);
        throw new TypeError('Can\'t exec priv method `' + name + '` because It\'s a' +
            (startsWithVowel ? 'n ' : ' ') + methodType + ' not a function');
    }
 
    var args = Array.prototype.slice.call(arguments, 1);
 
    return method.apply(this, args);
};
 
return Blocks;
 
})();
 
Eif (typeof module !== 'undefined') {
    module.exports = Blocks;
}