Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dozyio committed Jan 25, 2025
1 parent 2fdbaad commit d7ea08d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
20 changes: 5 additions & 15 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ export function parseIPv4Mapped(input: string): Uint8Array | undefined {

const ipv4 = parser.new(input).parseWith(() => parser.readIPv4Addr());
if (ipv4 === undefined) {
return undefined
return undefined;
}

return Uint8Array.from([
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0xff, 0xff,
ipv4[0], ipv4[1], ipv4[2], ipv4[3]
])
return Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, ipv4[0], ipv4[1], ipv4[2], ipv4[3]]);
}

/** Parse `input` into IPv6 bytes. */
Expand Down Expand Up @@ -58,17 +53,12 @@ export function parseIP(input: string, mapIPv4ToIPv6 = false): Uint8Array | unde

const addr = parser.new(input).parseWith(() => parser.readIPAddr());
if (!addr) {
return undefined
return undefined;
}

if (mapIPv4ToIPv6 && addr.length === 4) {
return Uint8Array.from([
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0xff, 0xff,
addr[0], addr[1], addr[2], addr[3]
]);
return Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, addr[0], addr[1], addr[2], addr[3]]);
}

return addr
return addr;
}
14 changes: 5 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ describe("parse IPv4 as IPv4-mapped IPv6 address", () => {
const testCase = [
{
input: "1.2.3.4",
output: Uint8Array.from([
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0xff, 0xff,
1, 2, 3, 4]),
}
]
output: Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 1, 2, 3, 4]),
},
];
for (const { input, output } of testCase) {
const r = parseIPv4Mapped(input)
const r = parseIPv4Mapped(input);
if (r === undefined) {
throw new Error('undefined address')
throw new Error("undefined address");
}

expect(r).to.deep.equal(output);
Expand Down

0 comments on commit d7ea08d

Please sign in to comment.