-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
127 lines (114 loc) · 3 KB
/
utils.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
export interface CaptchaOptions {
width: number
height: number
backgroundColor: string
font: string
fontColor: string
fontSize: number
fontWeight: number | string
lineColor: string
lineAmount: number
lineWidth: number
}
export interface CaptchaSyncOptions {
width: number
height: number
backgroundColor: string
font: string
fontColor: string
lineColor: string
lineAmount: number
lineWidth: number
}
export interface GeneratedCaptcha {
answer: string
captcha: Buffer
}
export interface GeneratedCaptchaSync {
answer: string
captcha: string
}
export interface ValidateOptions {
caseSensitive: boolean
}
export interface GetBuilderOptions {
width: number,
height: number,
backgroundColor: string
}
export interface GetLinePositionOptions {
width: number,
height: number,
}
export interface GetCharPositionOptions {
captchaLenght: number,
width: number,
height: number,
index: number
}
export const getImageGenerationOptions = (input?: Partial<CaptchaOptions>): CaptchaOptions => ({
width: 345,
height: 96,
backgroundColor: "#FFFFFF",
font: "Arial",
fontSize: 30,
fontWeight: 600,
fontColor: "#777777",
lineColor: "#777777",
lineAmount: 10,
lineWidth: 1,
...input
})
export const getImageSyncGenerationOptions = (input?: Partial<CaptchaSyncOptions>): CaptchaSyncOptions => ({
width: 345,
height: 96,
backgroundColor: "#FFFFFF",
font: "Bold 30px Arial",
fontColor: "#777777",
lineColor: "#777777",
lineAmount: 10,
lineWidth: 1,
...input
})
export const generateCaptcha = (charAmmount: number): string => {
let captcha = "";
let chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < charAmmount; i++) {
captcha += chars.charAt(Math.floor(Math.random() * chars.length));
}
return captcha;
};
export const generateRandomColor = () =>
"rgb(" +
Math.round(Math.random() * 255) +
"," +
Math.round(Math.random() * 255) +
"," +
Math.round(Math.random() * 255) +
")";
export const validate = (
input: string,
answer: string,
validateOptions: Partial<ValidateOptions>
) => {
const options = {
caseSensitive: true,
...validateOptions
}
const { caseSensitive } = options
return (
(caseSensitive && input === answer) ||
(!caseSensitive && input?.toLowerCase() === answer.toLowerCase())
);
};
export const getCharPosition = ({ width, height, captchaLenght, index }: GetCharPositionOptions) => ({
x: (width / captchaLenght) * index + ((width / captchaLenght) / 2),
y: height / 2,
angle: (Math.random() - 0.5) * (Math.PI / 4)
})
export const getLinePosition = ({ height: builderHeight, width: builderWidth }: GetLinePositionOptions) => ({
x1: Math.random() * builderWidth,
y1: Math.random() * builderHeight,
x2: Math.random() * builderWidth,
y2: Math.random() * builderHeight,
})