Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true
Additional features:
```ts
ip.isV4Format('255.255.255.256') // true
ip.isV6Format('127.0.0.1') // true

ip.setMode('strict') // or 'legacy'
ip.isV4Format('255.255.255.256') // false
ip.isV6Format('127.0.0.1') // false

// new methods are always strict
ip.isV4('255.255.255.256') // false
ip.isV6('127.0.0.1') // false
```

## License
Expand Down
16 changes: 14 additions & 2 deletions src/main/ts/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ export const V6_RE = /^(?=.+)(::)?(((\d{1,3}\.){3}\d{1,3})?|([0-9a-f]{0,4}:{0,2}
export const V4_S_RE = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
export const V6_S_RE = /(([\dA-Fa-f]{1,4}:){7}[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,7}:|([\dA-Fa-f]{1,4}:){1,6}:[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,5}(:[\dA-Fa-f]{1,4}){1,2}|([\dA-Fa-f]{1,4}:){1,4}(:[\dA-Fa-f]{1,4}){1,3}|([\dA-Fa-f]{1,4}:){1,3}(:[\dA-Fa-f]{1,4}){1,4}|([\dA-Fa-f]{1,4}:){1,2}(:[\dA-Fa-f]{1,4}){1,5}|[\dA-Fa-f]{1,4}:((:[\dA-Fa-f]{1,4}){1,6})|:((:[\dA-Fa-f]{1,4}){1,7}|:)|fe80:(:[\dA-Fa-f]{0,4}){0,4}%[\dA-Za-z]+|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)|([\dA-Fa-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d))$/

export const isV4Format = (ip: string): boolean => V4_RE.test(ip) // Legacy
export const isV6Format = (ip: string): boolean => V6_RE.test(ip) // Legacy
export const isV4Format = (ip: string): boolean => defs.V4_RE.test(ip) // Legacy
export const isV6Format = (ip: string): boolean => defs.V6_RE.test(ip) // Legacy
export const isV4 = (ip: string): boolean => V4_S_RE.test(ip)
export const isV6 = (ip: string): boolean => V6_S_RE.test(ip)

const defs = {
V4_RE,
V6_RE,
}

export const setMode = (mode: 'legacy' | 'strict'): void => {
if (mode === 'legacy') { Object.assign(defs, { V4_RE, V6_RE }); return }
if (mode === 'strict') { Object.assign(defs, { V4_RE: V4_S_RE, V6_RE: V6_S_RE }); return }

throw new Error('mode must be either "legacy" or "strict"')
}

export function readUInt16BE(buf: Buffer | Uint8Array | DataView, offset: number = 0): number {
if (typeof (buf as Buffer).readUInt16BE === 'function') {
// Node.js Buffer or feross/buffer polyfill
Expand Down
21 changes: 20 additions & 1 deletion src/test/ts/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
cidr,
cidrSubnet,
or,
not
not,
setMode
} from '../../main/ts/core.ts'

describe('core', () => {
Expand Down Expand Up @@ -372,4 +373,22 @@ describe('core', () => {
assert.equal(isPublic(input), !expected, `isPublic(${input}) === ${!expected}`)
}
})

describe('setMode()', () => {
test('sets ipv4/ipv6 mode', () => {
setMode('legacy')
assert.equal(isV4Format('999.999.999.999'), true)
assert.equal(isV4('999.999.999.999'), false)
assert.equal(isV6Format('127.0.0.1'), true)
assert.equal(isV6('127.0.0.1'), false)

setMode('strict')
assert.equal(isV4Format('999.999.999.999'), false)
assert.equal(isV4('999.999.999.999'), false)
assert.equal(isV6Format('127.0.0.1'), false)
assert.equal(isV6('127.0.0.1'), false)

setMode('legacy')
})
})
})
21 changes: 19 additions & 2 deletions target/cjs/core.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ __export(core_exports, {
not: () => not,
or: () => or,
readUInt16BE: () => readUInt16BE,
setMode: () => setMode,
subnet: () => subnet,
toBuffer: () => toBuffer,
toLong: () => toLong,
Expand All @@ -47,10 +48,25 @@ var V4_RE = /^(\d{1,3}(\.|$)){4}$/;
var V6_RE = /^(?=.+)(::)?(((\d{1,3}\.){3}\d{1,3})?|([0-9a-f]{0,4}:{0,2})){1,8}(::)?$/i;
var V4_S_RE = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/;
var V6_S_RE = /(([\dA-Fa-f]{1,4}:){7}[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,7}:|([\dA-Fa-f]{1,4}:){1,6}:[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,5}(:[\dA-Fa-f]{1,4}){1,2}|([\dA-Fa-f]{1,4}:){1,4}(:[\dA-Fa-f]{1,4}){1,3}|([\dA-Fa-f]{1,4}:){1,3}(:[\dA-Fa-f]{1,4}){1,4}|([\dA-Fa-f]{1,4}:){1,2}(:[\dA-Fa-f]{1,4}){1,5}|[\dA-Fa-f]{1,4}:((:[\dA-Fa-f]{1,4}){1,6})|:((:[\dA-Fa-f]{1,4}){1,7}|:)|fe80:(:[\dA-Fa-f]{0,4}){0,4}%[\dA-Za-z]+|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)|([\dA-Fa-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d))$/;
var isV4Format = (ip) => V4_RE.test(ip);
var isV6Format = (ip) => V6_RE.test(ip);
var isV4Format = (ip) => defs.V4_RE.test(ip);
var isV6Format = (ip) => defs.V6_RE.test(ip);
var isV4 = (ip) => V4_S_RE.test(ip);
var isV6 = (ip) => V6_S_RE.test(ip);
var defs = {
V4_RE,
V6_RE
};
var setMode = (mode) => {
if (mode === "legacy") {
Object.assign(defs, { V4_RE, V6_RE });
return;
}
if (mode === "strict") {
Object.assign(defs, { V4_RE: V4_S_RE, V6_RE: V6_S_RE });
return;
}
throw new Error('mode must be either "legacy" or "strict"');
};
function readUInt16BE(buf, offset = 0) {
if (typeof buf.readUInt16BE === "function") {
return buf.readUInt16BE(offset);
Expand Down Expand Up @@ -306,6 +322,7 @@ var isPublic = (addr) => !isPrivate(addr);
not,
or,
readUInt16BE,
setMode,
subnet,
toBuffer,
toLong,
Expand Down
1 change: 1 addition & 0 deletions target/dts/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export declare const isV4Format: (ip: string) => boolean;
export declare const isV6Format: (ip: string) => boolean;
export declare const isV4: (ip: string) => boolean;
export declare const isV6: (ip: string) => boolean;
export declare const setMode: (mode: "legacy" | "strict") => void;
export declare function readUInt16BE(buf: Buffer | Uint8Array | DataView, offset?: number): number;
export type Family = typeof IPV4 | typeof IPV6;
export declare function normalizeFamily(family?: string | number): Family;
Expand Down
1 change: 1 addition & 0 deletions target/dts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export declare const ip: {
isV6Format: (ip: string) => boolean;
isV4: (ip: string) => boolean;
isV6: (ip: string) => boolean;
setMode: (mode: "legacy" | "strict") => void;
normalizeAddress: (addr: string | number) => string;
normalizeToLong: (addr: string) => number;
isLoopback: (addr: string | number) => boolean;
Expand Down
20 changes: 18 additions & 2 deletions target/esm/core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ var V4_RE = /^(\d{1,3}(\.|$)){4}$/;
var V6_RE = /^(?=.+)(::)?(((\d{1,3}\.){3}\d{1,3})?|([0-9a-f]{0,4}:{0,2})){1,8}(::)?$/i;
var V4_S_RE = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/;
var V6_S_RE = /(([\dA-Fa-f]{1,4}:){7}[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,7}:|([\dA-Fa-f]{1,4}:){1,6}:[\dA-Fa-f]{1,4}|([\dA-Fa-f]{1,4}:){1,5}(:[\dA-Fa-f]{1,4}){1,2}|([\dA-Fa-f]{1,4}:){1,4}(:[\dA-Fa-f]{1,4}){1,3}|([\dA-Fa-f]{1,4}:){1,3}(:[\dA-Fa-f]{1,4}){1,4}|([\dA-Fa-f]{1,4}:){1,2}(:[\dA-Fa-f]{1,4}){1,5}|[\dA-Fa-f]{1,4}:((:[\dA-Fa-f]{1,4}){1,6})|:((:[\dA-Fa-f]{1,4}){1,7}|:)|fe80:(:[\dA-Fa-f]{0,4}){0,4}%[\dA-Za-z]+|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)|([\dA-Fa-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d)\.){3}(25[0-5]|(2[0-4]|1{0,1}\d){0,1}\d))$/;
var isV4Format = (ip) => V4_RE.test(ip);
var isV6Format = (ip) => V6_RE.test(ip);
var isV4Format = (ip) => defs.V4_RE.test(ip);
var isV6Format = (ip) => defs.V6_RE.test(ip);
var isV4 = (ip) => V4_S_RE.test(ip);
var isV6 = (ip) => V6_S_RE.test(ip);
var defs = {
V4_RE,
V6_RE
};
var setMode = (mode) => {
if (mode === "legacy") {
Object.assign(defs, { V4_RE, V6_RE });
return;
}
if (mode === "strict") {
Object.assign(defs, { V4_RE: V4_S_RE, V6_RE: V6_S_RE });
return;
}
throw new Error('mode must be either "legacy" or "strict"');
};
function readUInt16BE(buf, offset = 0) {
if (typeof buf.readUInt16BE === "function") {
return buf.readUInt16BE(offset);
Expand Down Expand Up @@ -263,6 +278,7 @@ export {
not,
or,
readUInt16BE,
setMode,
subnet,
toBuffer,
toLong,
Expand Down
Loading