forked from GeyserMC/GeyserDiscordBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
67 lines (60 loc) · 1.91 KB
/
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
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
const cleanTextUtils = require('clean-text-utils')
const axios = require('axios')
/**
* Perform a GET request and return the status
* code and response data
*
* @param {String} url The URL to request data from
*/
exports.getContents = async (url) => {
let response
try {
response = await axios.get(url)
} catch (error) {
console.error(error)
return { status: error.response !== undefined ? error.response.status : error.code, data: error.response !== undefined ? error.response.data : '' }
}
return { status: response.status, data: response.data }
}
/**
* Perform a POST request and return the status
* code and response data with the passed contents
*
* @param {String} url The URL to post data to
* @param {Object} contents The data to post
*/
exports.postContents = async (url, contents) => {
let response
try {
response = await axios.post(url, contents)
} catch (error) {
console.error(error)
return { status: error.response !== undefined ? error.response.status : error.code, data: error.response !== undefined ? error.response.data : '' }
}
return { status: response.status, data: response.data }
}
/**
* Used to sanitise the given string to make it safe to be part of a regex match
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
*
* @param {String} string The string to escape regex for
*/
exports.escapeRegExp = (string) => {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
}
/**
* Util function to clean any symbols from a message
*
* @param {String} string String to sanitise
*/
exports.cleanText = (string) => {
return string.replace(/[^\p{N}\p{L} ]/gu, '')
}
/**
* Util function to normalise any unicode characters from a message
*
* @param {String} string String to sanitise
*/
exports.normaliseText = (string) => {
return cleanTextUtils.replace.exoticChars(string)
}