Adjust for available ESM-only dependencies (#1581)

* Adjust for available ESM-only dependencies

* Flags for jest (sadly, still required mid-2022)
This commit is contained in:
Jaco
2022-07-06 12:50:36 +03:00
committed by GitHub
parent 1d71f67e2a
commit 4367bb0f68
9 changed files with 182 additions and 104 deletions
+32 -9
View File
@@ -1,9 +1,34 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import ipRegex from 'ip-regex';
// The regex patterns below were copied as-is from the ip-regex package 5.0.0,
// https://github.com/sindresorhus/ip-regex/blob/a2a44dfa7f776528158c2a5ff9d8a1be435ec1b9/index.js#L1
//
// MIT License
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
//
// Changes made:
// - boundary support option has been dropped
// - only exact matching is used (isIp always passed exact: true)
// - the newest ip-regex is ESM-only, we compile to all platforms
const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}';
const v6s = '[a-fA-F\\d]{1,4}';
const v6 = `
(?:
(?:${v6s}:){7}(?:${v6s}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
(?:${v6s}:){6}(?:${v4}|:${v6s}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
(?:${v6s}:){5}(?::${v4}|(?::${v6s}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
(?:${v6s}:){4}(?:(?::${v6s}){0,1}:${v4}|(?::${v6s}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
(?:${v6s}:){3}(?:(?::${v6s}){0,2}:${v4}|(?::${v6s}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
(?:${v6s}:){2}(?:(?::${v6s}){0,3}:${v4}|(?::${v6s}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
(?:${v6s}:){1}(?:(?::${v6s}){0,4}:${v4}|(?::${v6s}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
(?::(?:(?::${v6s}){0,5}:${v4}|(?::${v6s}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
`.replace(/\s*\/\/.*$/gm, '').replace(/\n/g, '').trim();
type IpTypes = 'v4' | 'v6';
const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
const v4exact = new RegExp(`^${v4}$`);
const v6exact = new RegExp(`^${v6}$`);
/**
* @name isIp
@@ -22,12 +47,10 @@ type IpTypes = 'v4' | 'v6';
* isIp('1:2:3:4:5:6:7:8', 'v4'); // => false
* ```
*/
export function isIp (value: string, type?: IpTypes): boolean {
if (type === 'v4') {
return ipRegex.v4({ exact: true }).test(value);
} else if (type === 'v6') {
return ipRegex.v6({ exact: true }).test(value);
export function isIp (value: string, type?: 'v4' | 'v6'): boolean {
switch (type) {
case 'v4': return v4exact.test(value);
case 'v6': return v6exact.test(value);
default: return v46Exact.test(value);
}
return ipRegex({ exact: true }).test(value);
}