ayiin 2.0.16 → 2.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/cli.cjs +27 -0
- package/{bin → dist/bin}/cli.js +6 -8
- package/dist/cjs/index.cjs +14 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/methods/crypt.cjs +63 -0
- package/dist/cjs/methods/crypt.cjs.map +1 -0
- package/dist/cjs/methods/index.cjs +15 -0
- package/dist/cjs/methods/index.cjs.map +1 -0
- package/dist/cjs/methods/response.cjs +56 -0
- package/dist/cjs/methods/response.cjs.map +1 -0
- package/dist/cjs/methods/tools.cjs +139 -0
- package/dist/cjs/methods/tools.cjs.map +1 -0
- package/dist/cjs/package.json.cjs +11 -0
- package/dist/cjs/package.json.cjs.map +1 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/methods/crypt.js +61 -0
- package/dist/esm/methods/crypt.js.map +1 -0
- package/dist/esm/methods/index.js +13 -0
- package/dist/esm/methods/index.js.map +1 -0
- package/{methods → dist/esm/methods}/response.js +15 -15
- package/dist/esm/methods/response.js.map +1 -0
- package/dist/esm/methods/tools.js +137 -0
- package/dist/esm/methods/tools.js.map +1 -0
- package/dist/esm/package.json.js +6 -0
- package/dist/esm/package.json.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/package.json +20 -4
- package/index.d.ts +0 -5
- package/index.js +0 -12
- package/methods/crypt.js +0 -69
- package/methods/index.js +0 -15
- package/methods/tools.js +0 -151
- /package/{bin → dist/bin}/cli.d.ts +0 -0
- /package/{methods → dist/methods}/crypt.d.ts +0 -0
- /package/{methods → dist/methods}/index.d.ts +0 -0
- /package/{methods → dist/methods}/response.d.ts +0 -0
- /package/{methods → dist/methods}/tools.d.ts +0 -0
package/dist/bin/cli.cjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var version = "2.0.18";
|
|
5
|
+
var pkg = {
|
|
6
|
+
version: version};
|
|
7
|
+
|
|
8
|
+
// CLI entry point
|
|
9
|
+
console.log("Welcome to ayiin CLI!");
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
if (args.length === 0) {
|
|
12
|
+
console.log("Usage: ayiin <command>");
|
|
13
|
+
process.exit(0);
|
|
14
|
+
}
|
|
15
|
+
switch(args[0]){
|
|
16
|
+
case "hello":
|
|
17
|
+
console.log("Hai Ayiin, CLI sudah jalan!");
|
|
18
|
+
break;
|
|
19
|
+
case "version":
|
|
20
|
+
console.log(`ayiin CLI v${pkg.version}`);
|
|
21
|
+
break;
|
|
22
|
+
case "help":
|
|
23
|
+
console.log("Available commands: hello, version, help");
|
|
24
|
+
break;
|
|
25
|
+
default:
|
|
26
|
+
console.log(`Unknown command: ${args[0]}`);
|
|
27
|
+
}
|
package/{bin → dist/bin}/cli.js
RENAMED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const package_json_1 = __importDefault(require("../../package.json"));
|
|
2
|
+
var version = "2.0.18";
|
|
3
|
+
var pkg = {
|
|
4
|
+
version: version};
|
|
5
|
+
|
|
8
6
|
// CLI entry point
|
|
9
7
|
console.log("Welcome to ayiin CLI!");
|
|
10
8
|
const args = process.argv.slice(2);
|
|
@@ -12,12 +10,12 @@ if (args.length === 0) {
|
|
|
12
10
|
console.log("Usage: ayiin <command>");
|
|
13
11
|
process.exit(0);
|
|
14
12
|
}
|
|
15
|
-
switch
|
|
13
|
+
switch(args[0]){
|
|
16
14
|
case "hello":
|
|
17
15
|
console.log("Hai Ayiin, CLI sudah jalan!");
|
|
18
16
|
break;
|
|
19
17
|
case "version":
|
|
20
|
-
console.log(`ayiin CLI v${
|
|
18
|
+
console.log(`ayiin CLI v${pkg.version}`);
|
|
21
19
|
break;
|
|
22
20
|
case "help":
|
|
23
21
|
console.log("Available commands: hello, version, help");
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _package = require('./package.json.cjs');
|
|
4
|
+
var methods_index = require('./methods/index.cjs');
|
|
5
|
+
|
|
6
|
+
class Ayiin extends methods_index {
|
|
7
|
+
version = _package.default.version;
|
|
8
|
+
constructor(){
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = Ayiin;
|
|
14
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
class Crypt {
|
|
6
|
+
/**
|
|
7
|
+
* Performs Base encryption and decryption.
|
|
8
|
+
* @param {string | Buffer} input - The input data to be encrypted or decrypted.
|
|
9
|
+
* @param {string} key - The encryption/decryption key.
|
|
10
|
+
* @returns {Buffer} - The encrypted or decrypted data as a Buffer.
|
|
11
|
+
*/ baseCrypt = (input, key)=>{
|
|
12
|
+
const inputBuffer = Buffer.from(input, "ascii");
|
|
13
|
+
const keyBuffer = Buffer.from(key, "ascii");
|
|
14
|
+
const resultBuffer = Buffer.alloc(inputBuffer.length);
|
|
15
|
+
for(let i = 0; i < inputBuffer.length; i++){
|
|
16
|
+
resultBuffer[i] = inputBuffer[i] ^ keyBuffer[i % keyBuffer.length];
|
|
17
|
+
}
|
|
18
|
+
return resultBuffer;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Encrypts a plaintext message using the given key.
|
|
22
|
+
* @param {string} text - The plaintext message to be encrypted.
|
|
23
|
+
* @param {string} key - The encryption key.
|
|
24
|
+
* @returns {string} - The encrypted data in Hex format.
|
|
25
|
+
*/ encrypt = (text, key, bufferEncoding = "base64")=>{
|
|
26
|
+
const encryptedBuffer = this.baseCrypt(text, key);
|
|
27
|
+
return encryptedBuffer.toString(bufferEncoding);
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Decrypts an encrypted message using the given key.
|
|
31
|
+
* @param {string} encrypted - The encrypted data in Hex format.
|
|
32
|
+
* @param {string} key - The decryption key.
|
|
33
|
+
* @returns {string} - The decrypted plaintext message.
|
|
34
|
+
*/ decrypt = (encrypted, key, bufferEncoding = "base64")=>{
|
|
35
|
+
const encryptedBuffer = Buffer.from(encrypted, bufferEncoding);
|
|
36
|
+
const decryptedBuffer = this.baseCrypt(encryptedBuffer, key);
|
|
37
|
+
return decryptedBuffer.toString("ascii");
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Converts a Hex string to its corresponding ASCII representation.
|
|
41
|
+
* @param {string} hexString - The Hex string to be converted.
|
|
42
|
+
* @returns {string} - The ASCII representation of the Hex string.
|
|
43
|
+
*/ hexToAscii = (hexString)=>{
|
|
44
|
+
const hexBuffer = Buffer.from(hexString, "hex");
|
|
45
|
+
return hexBuffer.toString("ascii");
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Converts an ASCII string to its corresponding Hex representation.
|
|
49
|
+
* @param {string} asciiString - The ASCII string to be converted.
|
|
50
|
+
* @returns {string} - The Hex representation of the ASCII string.
|
|
51
|
+
*/ asciiToHex = (asciiString)=>{
|
|
52
|
+
const asciiBuffer = Buffer.from(asciiString, "ascii");
|
|
53
|
+
return asciiBuffer.toString("hex");
|
|
54
|
+
};
|
|
55
|
+
md5 = (data, encoding)=>{
|
|
56
|
+
if (!encoding) encoding = "hex";
|
|
57
|
+
let hasho = crypto.createHash("md5").update(data).digest(encoding);
|
|
58
|
+
return hasho;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = Crypt;
|
|
63
|
+
//# sourceMappingURL=crypt.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypt.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var methods_crypt = require('./crypt.cjs');
|
|
4
|
+
var methods_tools = require('./tools.cjs');
|
|
5
|
+
var methods_response = require('./response.cjs');
|
|
6
|
+
|
|
7
|
+
class Methods {
|
|
8
|
+
}
|
|
9
|
+
// copy instance properties
|
|
10
|
+
Object.assign(Methods.prototype, new methods_tools());
|
|
11
|
+
Object.assign(Methods.prototype, new methods_crypt());
|
|
12
|
+
Object.assign(Methods.prototype, new methods_response());
|
|
13
|
+
|
|
14
|
+
module.exports = Methods;
|
|
15
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class ResponseApi {
|
|
4
|
+
success = ({ caption, data, code })=>{
|
|
5
|
+
code = code ? Number(String(code) + "7400") : 2007400;
|
|
6
|
+
return {
|
|
7
|
+
responseCode: code,
|
|
8
|
+
responseSuccess: true,
|
|
9
|
+
responseMessage: `[Success] - ${caption}`,
|
|
10
|
+
responseData: data
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
badRequest = ({ caption, title, data })=>{
|
|
14
|
+
return {
|
|
15
|
+
responseCode: 4007400,
|
|
16
|
+
responseSuccess: false,
|
|
17
|
+
responseMessage: `[${title ? title : "Bad Request"}] - ${caption}`,
|
|
18
|
+
responseData: data
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
invalidFields = ({ caption, data })=>{
|
|
22
|
+
return {
|
|
23
|
+
responseCode: 4007401,
|
|
24
|
+
responseSuccess: false,
|
|
25
|
+
responseMessage: `[Invalid Fields] - ${caption}`,
|
|
26
|
+
responseData: data
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
notFound = ({ caption, data })=>{
|
|
30
|
+
return {
|
|
31
|
+
responseCode: 4047400,
|
|
32
|
+
responseSuccess: false,
|
|
33
|
+
responseMessage: `[Not Found] - ${caption}`,
|
|
34
|
+
responseData: data
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
unauthorized = ({ caption, data })=>{
|
|
38
|
+
return {
|
|
39
|
+
responseCode: 4017400,
|
|
40
|
+
responseSuccess: false,
|
|
41
|
+
responseMessage: `[Unauthorized] - ${caption}`,
|
|
42
|
+
responseData: data
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
internalServer = ({ caption, data })=>{
|
|
46
|
+
return {
|
|
47
|
+
responseCode: 5007400,
|
|
48
|
+
responseSuccess: false,
|
|
49
|
+
responseMessage: `[Internal Server Error] - ${caption}`,
|
|
50
|
+
responseData: data
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = ResponseApi;
|
|
56
|
+
//# sourceMappingURL=response.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class Tools {
|
|
4
|
+
/**
|
|
5
|
+
* Format money ke string sesuai locale
|
|
6
|
+
* @memberof Tools
|
|
7
|
+
* @param {number} amount - jumlah uang
|
|
8
|
+
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
9
|
+
* @param {Intl.NumberFormatOptions} options - default currency IDR
|
|
10
|
+
* @returns {string} string
|
|
11
|
+
*/ formatMoney = (amount, locales = "id-ID", options = {
|
|
12
|
+
style: "currency",
|
|
13
|
+
currency: "IDR",
|
|
14
|
+
minimumFractionDigits: 0
|
|
15
|
+
})=>{
|
|
16
|
+
return new Intl.NumberFormat(locales, options).format(amount);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Generate unique ID dengan pilihan tipe
|
|
20
|
+
* @param {"number"|"string"|"mixed"} type - tipe ID (angka, string, atau campuran)
|
|
21
|
+
* @param {number} length - panjang ID (default 8)
|
|
22
|
+
* @returns {string} ID unik acak
|
|
23
|
+
*/ uniqueId = ({ type = "mixed", length = 8, prefix })=>{
|
|
24
|
+
let chars;
|
|
25
|
+
switch(type){
|
|
26
|
+
case "number":
|
|
27
|
+
chars = "0123456789";
|
|
28
|
+
break;
|
|
29
|
+
case "string":
|
|
30
|
+
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
31
|
+
break;
|
|
32
|
+
default:
|
|
33
|
+
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
34
|
+
}
|
|
35
|
+
let id = "";
|
|
36
|
+
for(let i = 0; i < length; i++){
|
|
37
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
38
|
+
}
|
|
39
|
+
return prefix ? prefix + id : id;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Generate UUID v4 sederhana
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/ randomUuid = ()=>{
|
|
45
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c)=>{
|
|
46
|
+
const r = Math.random() * 16 | 0;
|
|
47
|
+
const v = c === "x" ? r : r & 0x3 | 0x8;
|
|
48
|
+
return v.toString(16);
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Pick a random from array
|
|
53
|
+
* @param {T[]} array
|
|
54
|
+
* @memberof Tools
|
|
55
|
+
* @returns {T} item acak dari array
|
|
56
|
+
*/ randomItem = (array)=>{
|
|
57
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Ambil beberapa item acak unik dari array
|
|
61
|
+
* @param {T[]} array
|
|
62
|
+
* @param {number} count - jumlah item
|
|
63
|
+
* @returns {T[]} array item acak unik dari array
|
|
64
|
+
*/ randomMany = (array, count)=>{
|
|
65
|
+
const copy = [
|
|
66
|
+
...array
|
|
67
|
+
];
|
|
68
|
+
for(let i = copy.length - 1; i > 0; i--){
|
|
69
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
70
|
+
[copy[i], copy[j]] = [
|
|
71
|
+
copy[j],
|
|
72
|
+
copy[i]
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
return copy.slice(0, count);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Validasi apakah string adalah email
|
|
79
|
+
* @param {string} str
|
|
80
|
+
* @returns {boolean} true jika string adalah email, false jika bukan
|
|
81
|
+
*/ isEmail = (str)=>/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
|
82
|
+
/**
|
|
83
|
+
* Validasi apakah string adalah URL
|
|
84
|
+
* @param {string} str
|
|
85
|
+
* @returns {boolean} true jika string adalah URL, false jika bukan
|
|
86
|
+
*/ isUrl = (str)=>{
|
|
87
|
+
try {
|
|
88
|
+
new URL(str);
|
|
89
|
+
return true;
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Format tanggal ke string sesuai locale
|
|
96
|
+
* @param {Date} date
|
|
97
|
+
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
98
|
+
* @returns {string} string
|
|
99
|
+
*/ formatDate = (date, locales = "id-ID")=>new Intl.DateTimeFormat(locales, {
|
|
100
|
+
year: "numeric",
|
|
101
|
+
month: "long",
|
|
102
|
+
day: "numeric",
|
|
103
|
+
hour: "2-digit",
|
|
104
|
+
minute: "2-digit"
|
|
105
|
+
}).format(date);
|
|
106
|
+
/**
|
|
107
|
+
* Hitung waktu relatif (misalnya "5 menit lalu")
|
|
108
|
+
* @param {Date} date
|
|
109
|
+
* @returns {string} string
|
|
110
|
+
*/ timeAgo = (date)=>{
|
|
111
|
+
const diff = Date.now() - date.getTime();
|
|
112
|
+
const seconds = Math.floor(diff / 1000);
|
|
113
|
+
if (seconds < 60) return `${seconds} detik lalu`;
|
|
114
|
+
const minutes = Math.floor(seconds / 60);
|
|
115
|
+
if (minutes < 60) return `${minutes} menit lalu`;
|
|
116
|
+
const hours = Math.floor(minutes / 60);
|
|
117
|
+
if (hours < 24) return `${hours} jam lalu`;
|
|
118
|
+
const days = Math.floor(hours / 24);
|
|
119
|
+
return `${days} hari lalu`;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Convert string menjadi camelCase
|
|
123
|
+
* @param {string} input - string yang akan diubah
|
|
124
|
+
* @returns {string} camelCase string
|
|
125
|
+
*/ toCamelCase = (input)=>{
|
|
126
|
+
return input.toLowerCase().replace(/[-_\s]+(.)?/g, (_, chr)=>chr ? chr.toUpperCase() : "");
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Capitalize string (huruf pertama jadi kapital)
|
|
130
|
+
* @param {string} input - string yang akan diubah
|
|
131
|
+
* @returns {string} string dengan huruf pertama kapital
|
|
132
|
+
*/ capitalize = (input)=>{
|
|
133
|
+
if (!input) return "";
|
|
134
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = Tools;
|
|
139
|
+
//# sourceMappingURL=tools.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.json.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
class Crypt {
|
|
4
|
+
/**
|
|
5
|
+
* Performs Base encryption and decryption.
|
|
6
|
+
* @param {string | Buffer} input - The input data to be encrypted or decrypted.
|
|
7
|
+
* @param {string} key - The encryption/decryption key.
|
|
8
|
+
* @returns {Buffer} - The encrypted or decrypted data as a Buffer.
|
|
9
|
+
*/ baseCrypt = (input, key)=>{
|
|
10
|
+
const inputBuffer = Buffer.from(input, "ascii");
|
|
11
|
+
const keyBuffer = Buffer.from(key, "ascii");
|
|
12
|
+
const resultBuffer = Buffer.alloc(inputBuffer.length);
|
|
13
|
+
for(let i = 0; i < inputBuffer.length; i++){
|
|
14
|
+
resultBuffer[i] = inputBuffer[i] ^ keyBuffer[i % keyBuffer.length];
|
|
15
|
+
}
|
|
16
|
+
return resultBuffer;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Encrypts a plaintext message using the given key.
|
|
20
|
+
* @param {string} text - The plaintext message to be encrypted.
|
|
21
|
+
* @param {string} key - The encryption key.
|
|
22
|
+
* @returns {string} - The encrypted data in Hex format.
|
|
23
|
+
*/ encrypt = (text, key, bufferEncoding = "base64")=>{
|
|
24
|
+
const encryptedBuffer = this.baseCrypt(text, key);
|
|
25
|
+
return encryptedBuffer.toString(bufferEncoding);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Decrypts an encrypted message using the given key.
|
|
29
|
+
* @param {string} encrypted - The encrypted data in Hex format.
|
|
30
|
+
* @param {string} key - The decryption key.
|
|
31
|
+
* @returns {string} - The decrypted plaintext message.
|
|
32
|
+
*/ decrypt = (encrypted, key, bufferEncoding = "base64")=>{
|
|
33
|
+
const encryptedBuffer = Buffer.from(encrypted, bufferEncoding);
|
|
34
|
+
const decryptedBuffer = this.baseCrypt(encryptedBuffer, key);
|
|
35
|
+
return decryptedBuffer.toString("ascii");
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Converts a Hex string to its corresponding ASCII representation.
|
|
39
|
+
* @param {string} hexString - The Hex string to be converted.
|
|
40
|
+
* @returns {string} - The ASCII representation of the Hex string.
|
|
41
|
+
*/ hexToAscii = (hexString)=>{
|
|
42
|
+
const hexBuffer = Buffer.from(hexString, "hex");
|
|
43
|
+
return hexBuffer.toString("ascii");
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Converts an ASCII string to its corresponding Hex representation.
|
|
47
|
+
* @param {string} asciiString - The ASCII string to be converted.
|
|
48
|
+
* @returns {string} - The Hex representation of the ASCII string.
|
|
49
|
+
*/ asciiToHex = (asciiString)=>{
|
|
50
|
+
const asciiBuffer = Buffer.from(asciiString, "ascii");
|
|
51
|
+
return asciiBuffer.toString("hex");
|
|
52
|
+
};
|
|
53
|
+
md5 = (data, encoding)=>{
|
|
54
|
+
if (!encoding) encoding = "hex";
|
|
55
|
+
let hasho = crypto.createHash("md5").update(data).digest(encoding);
|
|
56
|
+
return hasho;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { Crypt as default };
|
|
61
|
+
//# sourceMappingURL=crypt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypt.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Crypt from './crypt.js';
|
|
2
|
+
import Tools from './tools.js';
|
|
3
|
+
import ResponseApi from './response.js';
|
|
4
|
+
|
|
5
|
+
class Methods {
|
|
6
|
+
}
|
|
7
|
+
// copy instance properties
|
|
8
|
+
Object.assign(Methods.prototype, new Tools());
|
|
9
|
+
Object.assign(Methods.prototype, new Crypt());
|
|
10
|
+
Object.assign(Methods.prototype, new ResponseApi());
|
|
11
|
+
|
|
12
|
+
export { Methods as default };
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class ResponseApi {
|
|
4
|
-
success = ({ caption, data, code
|
|
2
|
+
success = ({ caption, data, code })=>{
|
|
5
3
|
code = code ? Number(String(code) + "7400") : 2007400;
|
|
6
4
|
return {
|
|
7
5
|
responseCode: code,
|
|
8
6
|
responseSuccess: true,
|
|
9
7
|
responseMessage: `[Success] - ${caption}`,
|
|
10
|
-
responseData: data
|
|
8
|
+
responseData: data
|
|
11
9
|
};
|
|
12
10
|
};
|
|
13
|
-
badRequest = ({ caption, title, data
|
|
11
|
+
badRequest = ({ caption, title, data })=>{
|
|
14
12
|
return {
|
|
15
13
|
responseCode: 4007400,
|
|
16
14
|
responseSuccess: false,
|
|
17
15
|
responseMessage: `[${title ? title : "Bad Request"}] - ${caption}`,
|
|
18
|
-
responseData: data
|
|
16
|
+
responseData: data
|
|
19
17
|
};
|
|
20
18
|
};
|
|
21
|
-
invalidFields = ({ caption, data })
|
|
19
|
+
invalidFields = ({ caption, data })=>{
|
|
22
20
|
return {
|
|
23
21
|
responseCode: 4007401,
|
|
24
22
|
responseSuccess: false,
|
|
25
23
|
responseMessage: `[Invalid Fields] - ${caption}`,
|
|
26
|
-
responseData: data
|
|
24
|
+
responseData: data
|
|
27
25
|
};
|
|
28
26
|
};
|
|
29
|
-
notFound = ({ caption, data })
|
|
27
|
+
notFound = ({ caption, data })=>{
|
|
30
28
|
return {
|
|
31
29
|
responseCode: 4047400,
|
|
32
30
|
responseSuccess: false,
|
|
33
31
|
responseMessage: `[Not Found] - ${caption}`,
|
|
34
|
-
responseData: data
|
|
32
|
+
responseData: data
|
|
35
33
|
};
|
|
36
34
|
};
|
|
37
|
-
unauthorized = ({ caption, data })
|
|
35
|
+
unauthorized = ({ caption, data })=>{
|
|
38
36
|
return {
|
|
39
37
|
responseCode: 4017400,
|
|
40
38
|
responseSuccess: false,
|
|
41
39
|
responseMessage: `[Unauthorized] - ${caption}`,
|
|
42
|
-
responseData: data
|
|
40
|
+
responseData: data
|
|
43
41
|
};
|
|
44
42
|
};
|
|
45
|
-
internalServer = ({ caption, data })
|
|
43
|
+
internalServer = ({ caption, data })=>{
|
|
46
44
|
return {
|
|
47
45
|
responseCode: 5007400,
|
|
48
46
|
responseSuccess: false,
|
|
49
47
|
responseMessage: `[Internal Server Error] - ${caption}`,
|
|
50
|
-
responseData: data
|
|
48
|
+
responseData: data
|
|
51
49
|
};
|
|
52
50
|
};
|
|
53
51
|
}
|
|
54
|
-
|
|
52
|
+
|
|
53
|
+
export { ResponseApi as default };
|
|
54
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
class Tools {
|
|
2
|
+
/**
|
|
3
|
+
* Format money ke string sesuai locale
|
|
4
|
+
* @memberof Tools
|
|
5
|
+
* @param {number} amount - jumlah uang
|
|
6
|
+
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
7
|
+
* @param {Intl.NumberFormatOptions} options - default currency IDR
|
|
8
|
+
* @returns {string} string
|
|
9
|
+
*/ formatMoney = (amount, locales = "id-ID", options = {
|
|
10
|
+
style: "currency",
|
|
11
|
+
currency: "IDR",
|
|
12
|
+
minimumFractionDigits: 0
|
|
13
|
+
})=>{
|
|
14
|
+
return new Intl.NumberFormat(locales, options).format(amount);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Generate unique ID dengan pilihan tipe
|
|
18
|
+
* @param {"number"|"string"|"mixed"} type - tipe ID (angka, string, atau campuran)
|
|
19
|
+
* @param {number} length - panjang ID (default 8)
|
|
20
|
+
* @returns {string} ID unik acak
|
|
21
|
+
*/ uniqueId = ({ type = "mixed", length = 8, prefix })=>{
|
|
22
|
+
let chars;
|
|
23
|
+
switch(type){
|
|
24
|
+
case "number":
|
|
25
|
+
chars = "0123456789";
|
|
26
|
+
break;
|
|
27
|
+
case "string":
|
|
28
|
+
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
32
|
+
}
|
|
33
|
+
let id = "";
|
|
34
|
+
for(let i = 0; i < length; i++){
|
|
35
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
36
|
+
}
|
|
37
|
+
return prefix ? prefix + id : id;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Generate UUID v4 sederhana
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/ randomUuid = ()=>{
|
|
43
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c)=>{
|
|
44
|
+
const r = Math.random() * 16 | 0;
|
|
45
|
+
const v = c === "x" ? r : r & 0x3 | 0x8;
|
|
46
|
+
return v.toString(16);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Pick a random from array
|
|
51
|
+
* @param {T[]} array
|
|
52
|
+
* @memberof Tools
|
|
53
|
+
* @returns {T} item acak dari array
|
|
54
|
+
*/ randomItem = (array)=>{
|
|
55
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Ambil beberapa item acak unik dari array
|
|
59
|
+
* @param {T[]} array
|
|
60
|
+
* @param {number} count - jumlah item
|
|
61
|
+
* @returns {T[]} array item acak unik dari array
|
|
62
|
+
*/ randomMany = (array, count)=>{
|
|
63
|
+
const copy = [
|
|
64
|
+
...array
|
|
65
|
+
];
|
|
66
|
+
for(let i = copy.length - 1; i > 0; i--){
|
|
67
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
68
|
+
[copy[i], copy[j]] = [
|
|
69
|
+
copy[j],
|
|
70
|
+
copy[i]
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
return copy.slice(0, count);
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Validasi apakah string adalah email
|
|
77
|
+
* @param {string} str
|
|
78
|
+
* @returns {boolean} true jika string adalah email, false jika bukan
|
|
79
|
+
*/ isEmail = (str)=>/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
|
80
|
+
/**
|
|
81
|
+
* Validasi apakah string adalah URL
|
|
82
|
+
* @param {string} str
|
|
83
|
+
* @returns {boolean} true jika string adalah URL, false jika bukan
|
|
84
|
+
*/ isUrl = (str)=>{
|
|
85
|
+
try {
|
|
86
|
+
new URL(str);
|
|
87
|
+
return true;
|
|
88
|
+
} catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Format tanggal ke string sesuai locale
|
|
94
|
+
* @param {Date} date
|
|
95
|
+
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
96
|
+
* @returns {string} string
|
|
97
|
+
*/ formatDate = (date, locales = "id-ID")=>new Intl.DateTimeFormat(locales, {
|
|
98
|
+
year: "numeric",
|
|
99
|
+
month: "long",
|
|
100
|
+
day: "numeric",
|
|
101
|
+
hour: "2-digit",
|
|
102
|
+
minute: "2-digit"
|
|
103
|
+
}).format(date);
|
|
104
|
+
/**
|
|
105
|
+
* Hitung waktu relatif (misalnya "5 menit lalu")
|
|
106
|
+
* @param {Date} date
|
|
107
|
+
* @returns {string} string
|
|
108
|
+
*/ timeAgo = (date)=>{
|
|
109
|
+
const diff = Date.now() - date.getTime();
|
|
110
|
+
const seconds = Math.floor(diff / 1000);
|
|
111
|
+
if (seconds < 60) return `${seconds} detik lalu`;
|
|
112
|
+
const minutes = Math.floor(seconds / 60);
|
|
113
|
+
if (minutes < 60) return `${minutes} menit lalu`;
|
|
114
|
+
const hours = Math.floor(minutes / 60);
|
|
115
|
+
if (hours < 24) return `${hours} jam lalu`;
|
|
116
|
+
const days = Math.floor(hours / 24);
|
|
117
|
+
return `${days} hari lalu`;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Convert string menjadi camelCase
|
|
121
|
+
* @param {string} input - string yang akan diubah
|
|
122
|
+
* @returns {string} camelCase string
|
|
123
|
+
*/ toCamelCase = (input)=>{
|
|
124
|
+
return input.toLowerCase().replace(/[-_\s]+(.)?/g, (_, chr)=>chr ? chr.toUpperCase() : "");
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Capitalize string (huruf pertama jadi kapital)
|
|
128
|
+
* @param {string} input - string yang akan diubah
|
|
129
|
+
* @returns {string} string dengan huruf pertama kapital
|
|
130
|
+
*/ capitalize = (input)=>{
|
|
131
|
+
if (!input) return "";
|
|
132
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export { Tools as default };
|
|
137
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.json.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/dist/index.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ayiin",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.18",
|
|
4
4
|
"description": "Library Pribadi Yang Gak Ada Isinya",
|
|
5
|
-
"main": "index.js",
|
|
6
5
|
"bin": {
|
|
7
|
-
"ayiin": "./bin/cli.js"
|
|
6
|
+
"ayiin": "./dist/bin/cli.js"
|
|
7
|
+
},
|
|
8
|
+
"main": "./dist/cjs/index.cjs",
|
|
9
|
+
"module": "./dist/esm/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/cjs/index.cjs",
|
|
15
|
+
"import": "./dist/esm/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
8
18
|
},
|
|
9
19
|
"scripts": {
|
|
10
|
-
"build": "tsc"
|
|
20
|
+
"build:esm": "tsc",
|
|
21
|
+
"build:cjs": "rollup -c rollup.config.mjs",
|
|
22
|
+
"build": "bun run build:esm && bun run build:cjs"
|
|
11
23
|
},
|
|
12
24
|
"repository": {
|
|
13
25
|
"type": "git",
|
|
@@ -22,7 +34,11 @@
|
|
|
22
34
|
],
|
|
23
35
|
"license": "GPL-3.0-only",
|
|
24
36
|
"devDependencies": {
|
|
37
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
38
|
+
"@types/bun": "^1.4.1",
|
|
25
39
|
"@types/node": "^26.3.0",
|
|
40
|
+
"rollup": "^4.63.1",
|
|
41
|
+
"rollup-plugin-swc3": "^0.12.1",
|
|
26
42
|
"typescript": "^7"
|
|
27
43
|
}
|
|
28
44
|
}
|
package/index.d.ts
DELETED
package/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const methods_1 = __importDefault(require("./methods"));
|
|
7
|
-
class Ayiin extends methods_1.default {
|
|
8
|
-
constructor() {
|
|
9
|
-
super();
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.default = Ayiin;
|
package/methods/crypt.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
7
|
-
class Crypt {
|
|
8
|
-
/**
|
|
9
|
-
* Performs Base encryption and decryption.
|
|
10
|
-
* @param {string | Buffer} input - The input data to be encrypted or decrypted.
|
|
11
|
-
* @param {string} key - The encryption/decryption key.
|
|
12
|
-
* @returns {Buffer} - The encrypted or decrypted data as a Buffer.
|
|
13
|
-
*/
|
|
14
|
-
baseCrypt = (input, key) => {
|
|
15
|
-
const inputBuffer = Buffer.from(input, "ascii");
|
|
16
|
-
const keyBuffer = Buffer.from(key, "ascii");
|
|
17
|
-
const resultBuffer = Buffer.alloc(inputBuffer.length);
|
|
18
|
-
for (let i = 0; i < inputBuffer.length; i++) {
|
|
19
|
-
resultBuffer[i] = inputBuffer[i] ^ keyBuffer[i % keyBuffer.length];
|
|
20
|
-
}
|
|
21
|
-
return resultBuffer;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Encrypts a plaintext message using the given key.
|
|
25
|
-
* @param {string} text - The plaintext message to be encrypted.
|
|
26
|
-
* @param {string} key - The encryption key.
|
|
27
|
-
* @returns {string} - The encrypted data in Hex format.
|
|
28
|
-
*/
|
|
29
|
-
encrypt = (text, key, bufferEncoding = "base64") => {
|
|
30
|
-
const encryptedBuffer = this.baseCrypt(text, key);
|
|
31
|
-
return encryptedBuffer.toString(bufferEncoding);
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Decrypts an encrypted message using the given key.
|
|
35
|
-
* @param {string} encrypted - The encrypted data in Hex format.
|
|
36
|
-
* @param {string} key - The decryption key.
|
|
37
|
-
* @returns {string} - The decrypted plaintext message.
|
|
38
|
-
*/
|
|
39
|
-
decrypt = (encrypted, key, bufferEncoding = "base64") => {
|
|
40
|
-
const encryptedBuffer = Buffer.from(encrypted, bufferEncoding);
|
|
41
|
-
const decryptedBuffer = this.baseCrypt(encryptedBuffer, key);
|
|
42
|
-
return decryptedBuffer.toString("ascii");
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Converts a Hex string to its corresponding ASCII representation.
|
|
46
|
-
* @param {string} hexString - The Hex string to be converted.
|
|
47
|
-
* @returns {string} - The ASCII representation of the Hex string.
|
|
48
|
-
*/
|
|
49
|
-
hexToAscii = (hexString) => {
|
|
50
|
-
const hexBuffer = Buffer.from(hexString, "hex");
|
|
51
|
-
return hexBuffer.toString("ascii");
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* Converts an ASCII string to its corresponding Hex representation.
|
|
55
|
-
* @param {string} asciiString - The ASCII string to be converted.
|
|
56
|
-
* @returns {string} - The Hex representation of the ASCII string.
|
|
57
|
-
*/
|
|
58
|
-
asciiToHex = (asciiString) => {
|
|
59
|
-
const asciiBuffer = Buffer.from(asciiString, "ascii");
|
|
60
|
-
return asciiBuffer.toString("hex");
|
|
61
|
-
};
|
|
62
|
-
md5 = (data, encoding) => {
|
|
63
|
-
if (!encoding)
|
|
64
|
-
encoding = "hex";
|
|
65
|
-
let hasho = crypto_1.default.createHash("md5").update(data).digest(encoding);
|
|
66
|
-
return hasho;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
exports.default = Crypt;
|
package/methods/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const crypt_1 = __importDefault(require("./crypt"));
|
|
7
|
-
const tools_1 = __importDefault(require("./tools"));
|
|
8
|
-
const response_1 = __importDefault(require("./response"));
|
|
9
|
-
class Methods {
|
|
10
|
-
}
|
|
11
|
-
// copy instance properties
|
|
12
|
-
Object.assign(Methods.prototype, new tools_1.default());
|
|
13
|
-
Object.assign(Methods.prototype, new crypt_1.default());
|
|
14
|
-
Object.assign(Methods.prototype, new response_1.default());
|
|
15
|
-
exports.default = Methods;
|
package/methods/tools.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class Tools {
|
|
4
|
-
/**
|
|
5
|
-
* Format money ke string sesuai locale
|
|
6
|
-
* @memberof Tools
|
|
7
|
-
* @param {number} amount - jumlah uang
|
|
8
|
-
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
9
|
-
* @param {Intl.NumberFormatOptions} options - default currency IDR
|
|
10
|
-
* @returns {string} string
|
|
11
|
-
*/
|
|
12
|
-
formatMoney = (amount, locales = "id-ID", options = {
|
|
13
|
-
style: "currency",
|
|
14
|
-
currency: "IDR",
|
|
15
|
-
minimumFractionDigits: 0,
|
|
16
|
-
}) => {
|
|
17
|
-
return new Intl.NumberFormat(locales, options).format(amount);
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Generate unique ID dengan pilihan tipe
|
|
21
|
-
* @param {"number"|"string"|"mixed"} type - tipe ID (angka, string, atau campuran)
|
|
22
|
-
* @param {number} length - panjang ID (default 8)
|
|
23
|
-
* @returns {string} ID unik acak
|
|
24
|
-
*/
|
|
25
|
-
uniqueId = ({ type = "mixed", length = 8, prefix, }) => {
|
|
26
|
-
let chars;
|
|
27
|
-
switch (type) {
|
|
28
|
-
case "number":
|
|
29
|
-
chars = "0123456789";
|
|
30
|
-
break;
|
|
31
|
-
case "string":
|
|
32
|
-
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
33
|
-
break;
|
|
34
|
-
default:
|
|
35
|
-
chars =
|
|
36
|
-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
37
|
-
}
|
|
38
|
-
let id = "";
|
|
39
|
-
for (let i = 0; i < length; i++) {
|
|
40
|
-
id += chars[Math.floor(Math.random() * chars.length)];
|
|
41
|
-
}
|
|
42
|
-
return prefix ? prefix + id : id;
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Generate UUID v4 sederhana
|
|
46
|
-
* @returns {string}
|
|
47
|
-
*/
|
|
48
|
-
randomUuid = () => {
|
|
49
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
50
|
-
const r = (Math.random() * 16) | 0;
|
|
51
|
-
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
|
52
|
-
return v.toString(16);
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Pick a random from array
|
|
57
|
-
* @param {T[]} array
|
|
58
|
-
* @memberof Tools
|
|
59
|
-
* @returns {T} item acak dari array
|
|
60
|
-
*/
|
|
61
|
-
randomItem = (array) => {
|
|
62
|
-
return array[Math.floor(Math.random() * array.length)];
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Ambil beberapa item acak unik dari array
|
|
66
|
-
* @param {T[]} array
|
|
67
|
-
* @param {number} count - jumlah item
|
|
68
|
-
* @returns {T[]} array item acak unik dari array
|
|
69
|
-
*/
|
|
70
|
-
randomMany = (array, count) => {
|
|
71
|
-
const copy = [...array];
|
|
72
|
-
for (let i = copy.length - 1; i > 0; i--) {
|
|
73
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
74
|
-
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
75
|
-
}
|
|
76
|
-
return copy.slice(0, count);
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Validasi apakah string adalah email
|
|
80
|
-
* @param {string} str
|
|
81
|
-
* @returns {boolean} true jika string adalah email, false jika bukan
|
|
82
|
-
*/
|
|
83
|
-
isEmail = (str) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
|
84
|
-
/**
|
|
85
|
-
* Validasi apakah string adalah URL
|
|
86
|
-
* @param {string} str
|
|
87
|
-
* @returns {boolean} true jika string adalah URL, false jika bukan
|
|
88
|
-
*/
|
|
89
|
-
isUrl = (str) => {
|
|
90
|
-
try {
|
|
91
|
-
new URL(str);
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
catch {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Format tanggal ke string sesuai locale
|
|
100
|
-
* @param {Date} date
|
|
101
|
-
* @param {Intl.LocalesArgument} locales - default "id-ID"
|
|
102
|
-
* @returns {string} string
|
|
103
|
-
*/
|
|
104
|
-
formatDate = (date, locales = "id-ID") => new Intl.DateTimeFormat(locales, {
|
|
105
|
-
year: "numeric",
|
|
106
|
-
month: "long",
|
|
107
|
-
day: "numeric",
|
|
108
|
-
hour: "2-digit",
|
|
109
|
-
minute: "2-digit",
|
|
110
|
-
}).format(date);
|
|
111
|
-
/**
|
|
112
|
-
* Hitung waktu relatif (misalnya "5 menit lalu")
|
|
113
|
-
* @param {Date} date
|
|
114
|
-
* @returns {string} string
|
|
115
|
-
*/
|
|
116
|
-
timeAgo = (date) => {
|
|
117
|
-
const diff = Date.now() - date.getTime();
|
|
118
|
-
const seconds = Math.floor(diff / 1000);
|
|
119
|
-
if (seconds < 60)
|
|
120
|
-
return `${seconds} detik lalu`;
|
|
121
|
-
const minutes = Math.floor(seconds / 60);
|
|
122
|
-
if (minutes < 60)
|
|
123
|
-
return `${minutes} menit lalu`;
|
|
124
|
-
const hours = Math.floor(minutes / 60);
|
|
125
|
-
if (hours < 24)
|
|
126
|
-
return `${hours} jam lalu`;
|
|
127
|
-
const days = Math.floor(hours / 24);
|
|
128
|
-
return `${days} hari lalu`;
|
|
129
|
-
};
|
|
130
|
-
/**
|
|
131
|
-
* Convert string menjadi camelCase
|
|
132
|
-
* @param {string} input - string yang akan diubah
|
|
133
|
-
* @returns {string} camelCase string
|
|
134
|
-
*/
|
|
135
|
-
toCamelCase = (input) => {
|
|
136
|
-
return input
|
|
137
|
-
.toLowerCase()
|
|
138
|
-
.replace(/[-_\s]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ""));
|
|
139
|
-
};
|
|
140
|
-
/**
|
|
141
|
-
* Capitalize string (huruf pertama jadi kapital)
|
|
142
|
-
* @param {string} input - string yang akan diubah
|
|
143
|
-
* @returns {string} string dengan huruf pertama kapital
|
|
144
|
-
*/
|
|
145
|
-
capitalize = (input) => {
|
|
146
|
-
if (!input)
|
|
147
|
-
return "";
|
|
148
|
-
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
exports.default = Tools;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|