From adafd61c83a8a6071d291856cfdaff7ccc05f751 Mon Sep 17 00:00:00 2001 From: ztimson Date: Wed, 25 Jun 2025 11:48:49 -0400 Subject: [PATCH] Added IPv6 to v4 converter --- package.json | 2 +- src/misc.ts | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index af1c068..b93e8db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.25.17", + "version": "0.25.18", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/misc.ts b/src/misc.ts index 50577d4..bb29be9 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -1,6 +1,16 @@ import {PathEvent} from './path-events.ts'; import {md5} from './string'; +/** + * Escape any regex special characters to avoid misinterpretation during search + * + * @param {string} value String which should be escaped + * @return {string} New escaped sequence + */ +export function escapeRegex(value: string) { + return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&'); +} + /** * Run a stringified function with arguments asynchronously * @param {object} args Map of key/value arguments @@ -26,13 +36,15 @@ export function gravatar(email: string, def='mp') { } /** - * Escape any regex special characters to avoid misinterpretation during search - * - * @param {string} value String which should be escaped - * @return {string} New escaped sequence + * Convert IPv6 to v4 because who uses that, NAT4Life + * @param {string} ip IPv6 address, e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334 + * @returns {string | null} IPv4 address, e.g. 172.16.58.3 */ -export function escapeRegex(value: string) { - return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&'); +export function ipV6ToV4(ip: string) { + if(!ip) return null; + const ipv4 = ip.split(':').splice(-1)[0]; + if(ipv4 == '1') return '127.0.0.1'; + return ipv4; } /**