MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / isLoopback

Function isLoopback

client/src/components/formHelpers/form-ip-utils.ts:53–68  ·  view source on GitHub ↗
(addr: string)

Source from the content-addressed store, hash-verified

51}
52
53function isLoopback(addr: string) {
54 // If addr is an IPv4 address in long integer form (no dots and no colons), convert it
55 if (!/\./.test(addr) && !/:/.test(addr)) {
56 addr = fromLong(Number(addr));
57 }
58
59 return (
60 /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) ||
61 /^0177\./.test(addr) ||
62 /^0x7f\./i.test(addr) ||
63 /^fe80::1$/i.test(addr) ||
64 /^::1$/.test(addr) ||
65 /^\[::1\]/.test(addr) ||
66 /^::$/.test(addr)
67 );
68}
69
70function fromLong(ipl: number) {
71 return `${ipl >>> 24}.${(ipl >> 16) & 255}.${(ipl >> 8) & 255}.${ipl & 255}`;

Callers 1

isPrivateFunction · 0.85

Calls 1

fromLongFunction · 0.85

Tested by

no test coverage detected