all files / generators/spec/ examples.js

100% Statements 33/33
88.89% Branches 16/18
100% Functions 4/4
100% Lines 33/33
5 statements, 8 branches Ignored     
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                                                                                                           
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../commons/utils/utils");
var ramlUtil_1 = require("../../commons/utils/ramlUtil");
var cacheUtil_1 = require("../../commons/utils/cacheUtil");
var schemaUtil_1 = require("../../commons/utils/schemaUtil");
var config_1 = require("../../configs/config");
/**
 * Class which holds the logic for generating example data.
 *
 * @export
 * @class ExamplesGenerator
 */
var ExamplesGenerator = (function () {
    function ExamplesGenerator() {
        /**
         * Array of default raml types supported.
         *
         * @private
         * @type {string[]}
         * @memberof ExamplesGenerator
         */
        this.defaultRamlTypes = [
            'number', 'boolean', 'string', 'date-only', 'datetime',
            'time-only', 'datetime-only', 'file', 'nil', 'union', 'enum',
        ];
        /**
         * Key used in storing and searching for cached elements.
         *
         * @public
         * @type {string}
         * @memberof ExamplesGenerator
         */
        this.PRIME_KEY = 'raml';
    }
    /**
     * Generate type example for a certain table
     * @param {*} schema - entire collection of tables
     * @param {*} table - table information
     * @param {int} depthLevel - columns depth level to be computed
     * @return {Example} object containing - type name(with titleCase)
     *                                       - table content object to be stringified
     */
    ExamplesGenerator.prototype.generateTypeExampleContent = function (schema, table, depthLevel) {
        var _this = this;
        var object = {
            type: utils_1.default.toTitleCase(table.name),
            data: {},
        };
        object.data = cacheUtil_1.default.get(this.PRIME_KEY, object.type) || {};
        if (Object.keys(object.data).length) {
            return object;
        }
        table.columns.map(function (column) {
            if (_this.defaultRamlTypes.indexOf(column.dataType.type) > -1) {
                object.data[column.name] = ramlUtil_1.default.generateFakeData(column.name, column.dataType.type, column.dataType.values);
            }
            else {
                var cachedObject = cacheUtil_1.default.get(_this.PRIME_KEY, "" + column.dataType.type + (column.dataType.isArray ? '[]' : ''));
                Eif (cachedObject) {
                    object.data[column.name] = cachedObject;
                }
                else {
                    /* istanbul ignore next */
                    if (depthLevel >= config_1.default.DEFAULT_MAX_DEPTH_LEVEL) {
                        object.data[column.name] = column.dataType.isArray ? [] : {};
                    }
                    else {
                        var normalizedTable = schemaUtil_1.default.getNormalizedTableByType(schema, column.dataType.type);
                        if (normalizedTable) {
                            object.data[column.name] = column.dataType.isArray ?
                                utils_1.default.fillArray(_this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config_1.default.DEPTH_INCREMENT).data, config_1.default.DEFAULT_ARRAY_LENGTH) :
                                _this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config_1.default.DEPTH_INCREMENT).data;
                        }
                    }
                }
            }
        });
        cacheUtil_1.default.add(this.PRIME_KEY, object.type, object.data);
        cacheUtil_1.default.add(this.PRIME_KEY, object.type + "[]", utils_1.default.fillArray(object.data, config_1.default.DEFAULT_ARRAY_LENGTH));
        return object;
    };
    return ExamplesGenerator;
}());
exports.ExamplesGenerator = ExamplesGenerator;
exports.default = new ExamplesGenerator();