Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1be2c1118f | |||
| ead8fcffc0 | |||
| 367b026cea |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.28.5",
|
"version": "0.28.8",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -113,6 +113,18 @@ export class NotAcceptableError extends CustomError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class TooManyRequestsError extends CustomError {
|
||||||
|
static code = 429;
|
||||||
|
|
||||||
|
constructor(message: string = 'Rate Limit Reached') {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static instanceof(err: Error) {
|
||||||
|
return (<any>err).constructor.code == this.code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class InternalServerError extends CustomError {
|
export class InternalServerError extends CustomError {
|
||||||
static code = 500;
|
static code = 500;
|
||||||
|
|
||||||
|
|||||||
17
src/misc.ts
17
src/misc.ts
@@ -76,6 +76,23 @@ export function gravatar(email: string, def='mp') {
|
|||||||
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
|
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if IP address falls within CIDR range
|
||||||
|
* @param {string} ip IPV4 to check (192.168.0.12)
|
||||||
|
* @param {string} cidr IP range to check against (example: 192.168.0.0/24)
|
||||||
|
* @returns {boolean} Whether IP address is within range
|
||||||
|
*/
|
||||||
|
export function matchesCidr(ip: string, cidr: string): boolean {
|
||||||
|
if(!cidr) return true;
|
||||||
|
if(!ip) return false;
|
||||||
|
if(!cidr?.includes('/')) return ip === cidr; // Single IP
|
||||||
|
const [range, bits] = cidr.split('/');
|
||||||
|
const mask = ~(2 ** (32 - parseInt(bits)) - 1);
|
||||||
|
const ipToInt = (str: string) => str.split('.')
|
||||||
|
.reduce((int, octet) => (int << 8) + parseInt(octet), 0) >>> 0;
|
||||||
|
return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert IPv6 to v4 because who uses that, NAT4Life
|
* Convert IPv6 to v4 because who uses that, NAT4Life
|
||||||
* @param {string} ip IPv6 address, e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
* @param {string} ip IPv6 address, e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
||||||
|
|||||||
@@ -135,7 +135,15 @@ export function pascalCase(str?: string): string {
|
|||||||
.join('');
|
.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all emojis from a string
|
||||||
|
* @param {string} str Input string with emojis
|
||||||
|
* @returns {string} Sanitized string without emojis
|
||||||
|
*/
|
||||||
|
function removeEmojis(str: string): string {
|
||||||
|
const emojiRegex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud83c[\udde6-\uddff]|[\ud83d[\ude00-\ude4f]|[\ud83d[\ude80-\udeff]|[\ud83c[\udd00-\uddff]|[\ud83d[\ude50-\ude7f]|[\u2600-\u26ff]|[\u2700-\u27bf]|[\ud83e[\udd00-\uddff]|[\ud83c[\udf00-\uffff]|[\ud83d[\ude00-\udeff]|[\ud83c[\udde6-\uddff])/g;
|
||||||
|
return str.replace(emojiRegex, '');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random hexadecimal value
|
* Generate a random hexadecimal value
|
||||||
|
|||||||
Reference in New Issue
Block a user