-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
104 lines (87 loc) · 2.63 KB
/
index.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
export const $filter = (array: any[], text: string, key: string) => {
const txt = text.toLowerCase();
return array.filter((item) => {
return (key ? item[key] : item).toLowerCase().includes(txt);
});
};
export const $abc = () => {
return [...Array(26).keys()].map((_, i) => String.fromCharCode(i + 97));
};
export const $number = (from: number, to: number) => {
return [...Array(to - from + 1).keys()].map((number) => {
return number + from;
});
};
export const $getId = (length: number) => {
const symbols =
$abc().join("") + $abc().join("").toUpperCase() + $number(0, 9).join("");
const _length = length ? length : 10;
let id = "";
for (let i = 0; i < _length; i++) {
const random = symbols[Math.floor(Math.random() * symbols.length)];
id += random;
}
return id;
};
export const $percentage = (number: number, main: number) => {
return Math.floor((number / main) * 100);
};
export const $capitalize = (text: string) => {
return text.charAt(0).toUpperCase() + text.slice(1);
};
export const $randomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
export const $total = (array: any[], key: string) => {
let value = 0;
return array.reduce(
(accumulator, item) => (accumulator += key ? item[key] : item),
value
);
};
export const $formatNumber = (number: number) => {
return number.toLocaleString();
};
export const $shuffleArray = (array: any[]) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};
export const $truncateString = (text: string, length: number) => {
if (text.length <= length) return text;
return text.substring(0, length) + "...";
};
export const $is = {
undefined: (data: any) => {
return data === undefined;
},
null: (data: any) => {
return data === null;
},
odd: (number: number) => {
return number % 2 !== 0;
},
even: (number: number) => {
return number % 2 === 0;
},
};
export const $uniqueArray = (array: any[]) => {
return Array.from(new Set(array));
};
export const $reverseText = (text: string) => {
return text.split("").reverse().join("");
};
export const $padStart = (text: string, length: number, char: string) => {
return text.padStart(length, char);
};
export const $padEnd = (text: string, length: number, char: string) => {
return text.padEnd(length, char);
};
export const $randomItem = (array: any[]) => {
return array[Math.floor(Math.random() * array.length)];
};
export const $formatDate = (date: string) => {
return new Date(date).toDateString();
}