-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
40 lines (35 loc) · 844 Bytes
/
utils.js
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
import { v4 } from "uuid";
/**
* Creates UUID
*/
export const UUID = () => v4().replace(/-/g, "");
/**
* Returns stringified object
* @param {string} obj Object to stringify
*/
export const stringify = (obj) => {
return JSON.stringify(obj, null, 2);
};
/**
* Returns valid Json object from data
* @param {string} data - Data to parse
*/
export const parse = (data) => {
try {
return JSON.parse(data);
} catch (err) {
console.log(`[ERR][Not a valid object]->${err}`);
}
return [];
};
/**
* Encodes string to base64
* @param {string} data - String
*/
export const encode = (data) =>
Buffer.from(JSON.stringify(data)).toString("base64");
/**
* Decodes base64 and returns a string
* @param {string} data - Base64 encoded data.
*/
export const decode = (data) => Buffer.from(data, "base64").toString("utf8");