-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (69 loc) · 1.9 KB
/
index.js
File metadata and controls
77 lines (69 loc) · 1.9 KB
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
import uuid from "./src/uuid.js";
import { typeValidation, valueValidation } from "./errorValidation.js";
const ro_uuid = uuid;
/**
* @typedef {"default" | "usa"} ChoosenDateFormat
*/
/**
* randomiseStr
* @param {number} len
* @returns {string}
*/
function randomiseStr(len) {
const string_list = Array.from({ length: 26 }, (_, i) =>
String.fromCharCode(97 + i)
);
let x = [];
for (let i = 1; i <= len; i++) {
const rdIdx = Math.floor(Math.random() * string_list.length);
x.push(string_list[rdIdx]);
}
return x.join("");
}
/**
* get_formatted_date
* @param {ChoosenDateFormat} chosen_date_format
* @returns {string}
*/
function get_formatted_date(chosen_date_format) {
const date = new Date().getDate();
const month = new Date().getMonth() + 1;
const year = new Date().getFullYear();
/**
* @type {ChoosenDateFormat[]} accepted_format
*/
const accepted_format = ["default", "usa"];
if (chosen_date_format === accepted_format[0]) {
return `${date}/${month}/${year}`;
}
if (chosen_date_format === accepted_format[1]) {
return `${month}/${date}/${year}`;
}
}
/**
* ro_formatted
* @param {number} str_length
* @param {ChoosenDateFormat | undefined} chosen_date_format
* @returns
*/
function ro_formatted(str_length = 6, chosen_date_format = "default") {
const validateType = typeValidation(str_length, chosen_date_format);
const validateValue = valueValidation(str_length, chosen_date_format);
if (validateType.length > 0) {
if (validateType.length === 2) {
throw new Error(validateType.join(" & "));
}
throw new Error(validateType);
}
if (validateValue.length > 0) {
if (validateValue.length === 2) {
throw new Error(validateValue.join(" & "));
}
throw new Error(validateValue);
}
return `${randomiseStr(str_length)}_${get_formatted_date(
chosen_date_format
)}`;
}
export { ro_uuid };
export default ro_formatted;