Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54aaa58990 | ||
|
|
59b03d5b51 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.30.9",
|
||||
"version": "0.30.11",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -48,11 +48,11 @@ export function reservedIp(ip: string): boolean {
|
||||
* @returns {null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)'} null if public, class name if reserved
|
||||
*/
|
||||
export function isReserved(ip: string): null | 'Invalid' | 'Host' | 'Loopback' | 'Private (Class A)' | 'Private (Class B)' | 'Link-Local' | 'IETF Protocol Assignments' | 'Documentation (TEST-NET-1)' | 'Benchmarking' | 'Documentation (TEST-NET-2)' | 'Documentation (TEST-NET-3)' | 'Multicast' | 'Reserved (Class E)' {
|
||||
const ipToNumber = (ip: string) => {
|
||||
return ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
||||
};
|
||||
const ipToNumber = (ip: string) => ip.split('.').reduce((acc: number, octet: string) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
|
||||
|
||||
const ipNum = ipToNumber(ip);
|
||||
const ipv4 = ip.includes(':') ? ipV6ToV4(ip) : ip;
|
||||
if(ipv4 === null || !ipv4.includes('.')) return 'Invalid';
|
||||
const ipNum = ipToNumber(ipv4);
|
||||
if(ipNum === null) return 'Invalid';
|
||||
|
||||
const reserved = [
|
||||
|
||||
@@ -165,6 +165,15 @@ export function encodeQuery(data: any): string {
|
||||
).join('&');
|
||||
}
|
||||
|
||||
/** Recursively walks obj/arrays, calling cb(dotPath, value) for every leaf+branch prop */
|
||||
export function everyNested(obj: any, cb: (key: string, value: any) => void, prefix = ''){
|
||||
Object.entries(obj).forEach(([k, v]) => {
|
||||
const key = prefix ? `${prefix}.${k}` : k;
|
||||
cb(key, v);
|
||||
if (v && typeof v === 'object') everyNested(v, cb, key);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively flatten a nested object, while maintaining key structure
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user