all files / commons/utils/ ramlUtil.js

100% Statements 35/35
100% Branches 19/19
100% Functions 5/5
100% Lines 35/35
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                                94× 94× 94× 94×   91×                                                        
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var faker = require("faker");
var utils_1 = require("./utils");
var propertiesName_1 = require("../constants/propertiesName");
/**
 * Class which holds helper tools when working with ramls.
 *
 * @export
 * @class RamlUtil
 */
var RamlUtil = (function () {
    function RamlUtil() {
    }
    /**
     * Generate fake data for a field name
     * @param {string} field - field name
     * @param {string} type - data raml type: can be number / string / Boolean / date etc
     * @param {?string[]} values - default values for attributes (eg. enum: 'Yes' | 'No')
     * @return {*} fake data
     */
    RamlUtil.prototype.generateFakeData = function (field, type, values) {
        if (values && values.length) {
            return values[Math.floor(Math.random() * values.length)];
        }
        for (var _i = 0, _a = Object.keys(propertiesName_1.default); _i < _a.length; _i++) {
            var key = _a[_i];
            var index = utils_1.default.indexOfIgnoreCase(propertiesName_1.default[key], field.toString());
            var firstPosition = 0;
            if (index >= firstPosition) {
                return this.parseRamlValue(faker[key][propertiesName_1.default[key][index]](), type);
            }
            if (key.toLowerCase() === field.toLowerCase()) {
                return this.parseRamlValue(faker[key][propertiesName_1.default[key][firstPosition]](), type);
            }
        }
        if (field === 'id' || field === '_id') {
            return this.generateId(type);
        }
        return this.parseRamlValue(faker.lorem.word(), type);
    };
    /**
     * Parse object value to a custom raml type
     * @param {*} value - value to be parsed
     * @param {string} type - raml type
     * @return {*} parsed value
     */
    RamlUtil.prototype.parseRamlValue = function (value, type) {
        var base = 10;
        switch (type) {
            case 'number':
                return parseInt(value, base);
            case 'string':
                return value.toString();
            case 'boolean':
                return Boolean(value);
            default:
                return value;
        }
    };
    /**
     * Generate id/_id field value
     * @param {string} type - can be number / string / boolean
     * @return {*} random id
     */
    RamlUtil.prototype.generateId = function (type) {
        switch (type) {
            case 'number':
                return faker.random.number(Number.MAX_SAFE_INTEGER);
            case 'string':
                return faker.random.uuid();
            default:
                throw new Error('Not implemented yet');
        }
    };
    return RamlUtil;
}());
exports.RamlUtil = RamlUtil;
exports.default = new RamlUtil();