all files / commons/utils/ typeUtil.js

100% Statements 39/39
100% Branches 23/23
100% Functions 9/9
100% Lines 37/37
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                                                          27×     11×   16× 11×                                 25×           23×         17×                                     261×      
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
/**
 * TypeUtil util class.
 *
 * @export
 * @class TypeUtil
 */
var TypeUtil = (function () {
    function TypeUtil() {
        /**
         * Array that holds the number types
         *
         * @private
         * @type {string[]}
         * @memberof TypeUtil
         */
        this.numberTypes = ['int', 'tinyint', 'smallint', 'mediumint', 'bigint', 'decimal', 'float', 'double'];
        /**
         * Array that holds the string types
         *
         * @private
         * @type {string[]}
         * @memberof TypeUtil
         */
        this.stringTypes = [
            'char', 'varchar', 'blob', 'text', 'tinyblob', 'tinytext', 'mediumblob', 'mediumtext', 'longblob', 'longtext',
        ];
    }
    /**
     * Converts a given database type to the standard one.
     * @param {string} type db type.
     * @returns {string} standard type
     */
    TypeUtil.prototype.convertSqlType = function (type) {
        if (utils_1.default.indexOfIgnoreCase(this.numberTypes, type.toLowerCase()) > -1 ||
            (type.toLowerCase() === 'timestamp') ||
            (type.toLowerCase() === 'year')) {
            return 'number';
        }
        else if (utils_1.default.indexOfIgnoreCase(this.stringTypes, type.toLowerCase()) > -1) {
            return 'string';
        }
        else if (type.toLowerCase() === 'date') {
            return 'date-only';
        }
        else if (type.toLowerCase() === 'datetime') {
            return 'datetime';
        }
        else if (type.toLowerCase() === 'time') {
            return 'time-only';
        }
        else if (type.toLowerCase() === 'enum') {
            return 'enum';
        }
        throw new Error('Type not found');
    };
    /**
     * Converts no sql types to standard schema
     * @param {string} type - type to be converted
     */
    TypeUtil.prototype.convertNoSqlType = function (type) {
        throw new Error(type + " not yet implemented.");
    };
    /**
     * Converts raml types to standard schema
     * @param {{type: string; items: string}} type - type to be converted
     * @returns {{type: string; isArray: boolean; values?: string[]}} - converted type
     */
    TypeUtil.prototype.convertRamlTypes = function (type) {
        if (type.type.indexOf('|') > -1) {
            return {
                type: 'enum',
                isArray: false,
                values: type.type.split(' | '),
            };
        }
        else if (type.type === 'array') {
            return {
                type: type.items,
                isArray: true,
            };
        }
        return {
            type: type.type,
            isArray: false,
        };
    };
    /**
     * Converts Enum values to string.
     *
     * @param {DataType} type source datatype
     * @return {string} enum values as string
     */
    TypeUtil.prototype.getEnumValuesAsString = function (type) {
        if (type.type === 'enum' && type.values) {
            return type.values.map(function (value) { return "'" + value + "'"; }).join(', ');
        }
        return '';
    };
    /**
     * Checks if the provided type is a default one.
     *
     * @param {string} type - data type
     * @return {boolean} returns true if type is a default one.
     */
    TypeUtil.prototype.isDefaultType = function (type) {
        return ['enum', 'number', 'string'].some(function (item) { return type === item; });
    };
    return TypeUtil;
}());
exports.TypeUtil = TypeUtil;
exports.default = new TypeUtil();