cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
106 lines • 3.88 kB
JavaScript
// src/generate/index.ts
var Generate = {
/**
* Generate a random alphanumeric string of a given length.
* @param len Length of the string (default 4).
*/
alphanum(len = 4) {
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let result = "";
for (let i = 0; i < len; i++)
result += chars[Math.floor(Math.random() * chars.length)];
return result;
},
/**
* Generate a MongoDB-style ObjectId.
*/
objectId() {
return (Date.now() / 1e3 | 0).toString(16) + "xxxxxxxxxxxxxxxx".replace(/x/g, () => (Math.random() * 16 | 0).toString(16));
},
/**
* Generate a random integer in [min, max). Includes min, excludes max.
*/
int(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
},
/**
* Generate a random float in [min, max).
*/
random(min, max) {
return Math.random() * (max - min) + min;
},
/**
* Generate an array of random integers.
* @param len Length of the array.
* @param ops min/max bounds for each integer (default min=0, max=11).
*/
array(len, ops = {}) {
const { min = 0, max = 11 } = ops;
return Array.from({ length: len }, () => this.int(min, max));
},
/**
* Generate a random alphabetic string.
* @param len Length of the string (default 5).
* @param options lowercase/uppercase toggles.
*/
alphabate(len = 5, options = {}) {
const op = { lowercase: true, uppercase: true, ...options };
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const pool = (op.lowercase ? lower : "") + (op.uppercase ? upper : "");
let result = "";
for (let i = 0; i < len; i++)
result += pool[Math.floor(Math.random() * pool.length)];
return result;
},
/** @returns Current date as "YYYY-MM-DD" */
currentDate() {
const t = /* @__PURE__ */ new Date();
return `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()}`;
},
/** @returns Current time as "HH:mm:ss" */
currentTime() {
const t = /* @__PURE__ */ new Date();
return [t.getHours(), t.getMinutes(), t.getSeconds()].map((v) => String(v).padStart(2, "0")).join(":");
},
/** @returns Current datetime as "YYYY-MM-DD HH:mm:ss" */
currentDateTime() {
const t = /* @__PURE__ */ new Date();
const time = [t.getHours(), t.getMinutes(), t.getSeconds()].map((v) => String(v).padStart(2, "0")).join(":");
return `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()} ${time}`;
},
/**
* Generate a list of date strings matching specified days of the week.
* @param f From date "YYYY-MM-DD"
* @param t To date "YYYY-MM-DD"
* @param days Day indices 0–6 (Sunday=0, Saturday=6)
*/
listOfDateOfDays(f, t, days) {
const to = new Date(t).toISOString();
const cursor = new Date(f);
const rs = [];
while (cursor.toISOString() <= to) {
if (days.includes(cursor.getDay()))
rs.push(cursor.toISOString().slice(0, 10));
cursor.setDate(cursor.getDate() + 1);
}
return rs;
},
/**
* Fast RFC4122 v4 UUID generator.
* @author Jeff Ward (jcward.com) — MIT license
*/
uuidv4() {
const lut = [];
for (let i = 0; i < 256; i++) lut[i] = (i < 16 ? "0" : "") + i.toString(16);
const d0 = Math.random() * 4294967295 | 0;
const d1 = Math.random() * 4294967295 | 0;
const d2 = Math.random() * 4294967295 | 0;
const d3 = Math.random() * 4294967295 | 0;
return lut[d0 & 255] + lut[d0 >> 8 & 255] + lut[d0 >> 16 & 255] + lut[d0 >> 24 & 255] + "-" + lut[d1 & 255] + lut[d1 >> 8 & 255] + "-" + lut[d1 >> 16 & 15 | 64] + lut[d1 >> 24 & 255] + "-" + lut[d2 & 63 | 128] + lut[d2 >> 8 & 255] + "-" + lut[d2 >> 16 & 255] + lut[d2 >> 24 & 255] + lut[d3 & 255] + lut[d3 >> 8 & 255] + lut[d3 >> 16 & 255] + lut[d3 >> 24 & 255];
}
};
export {
Generate
};
//# sourceMappingURL=index.mjs.map