@rashedmakkouk/dev-utils
Version:
Utility library.
45 lines (44 loc) • 1.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const isString_1 = __importDefault(require("lodash/isString"));
const moment_1 = __importDefault(require("moment"));
const uuid_1 = require("uuid");
const utils_1 = require("../utils");
function random(type, options = {}) {
const timeFormat = 'YYYY-MM-DD_HH-mm-ss';
switch (type) {
case 'uuid':
return (0, uuid_1.v4)();
case 'number': {
const { decimal = false, max = 1, min = 0, precision = 0, } = options;
const randomNumber = Math.random() * (max - min + min);
if (decimal === false) {
return Math.trunc(randomNumber);
}
return precision == null || !utils_1.toFixedRange.includes(precision)
? randomNumber
: Number(randomNumber.toFixed(precision));
}
case 'filename':
case 'temp':
case 'title': {
const { prefix, suffix } = options;
const randomString = [];
prefix && (0, isString_1.default)(prefix) && randomString.push(prefix);
const timestamp = (0, moment_1.default)().format(timeFormat);
if (type === 'title') {
randomString.push(timestamp);
}
else {
randomString.push(timestamp, (0, uuid_1.v4)());
}
suffix && (0, isString_1.default)(suffix) && randomString.push(suffix);
return randomString.join('_');
}
}
}
exports.default = random;