Skip to content

Commit ac8c4af

Browse files
authored
Align query API types with runtime behavior (#25)
1 parent c0a2182 commit ac8c4af

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export function getByCountry(country: string): readonly string[] {
2828
return byCountry[country] ?? emptyIps;
2929
}
3030

31-
export function has(ip: string): boolean {
31+
export function has(ip: unknown): boolean {
3232
return typeof ip === "string" && ipToCountry.has(ip);
3333
}
3434

35-
export function findCountry(ip: string): string | undefined {
35+
export function findCountry(ip: unknown): string | undefined {
3636
if (typeof ip !== "string") {
3737
return undefined;
3838
}

test/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getByCountry,
88
has,
99
ips,
10+
random,
1011
} from "../src";
1112

1213
test("exports the generated IP list", () => {
@@ -29,8 +30,23 @@ test("groups IPs by country", () => {
2930
test("looks up IP membership and country", () => {
3031
assert.equal(has("93.123.23.1"), true);
3132
assert.equal(has("127.0.0.1"), false);
33+
assert.equal(has(127001), false);
3234
assert.equal(findCountry("118.174.25.251"), "Thailand");
3335
assert.equal(findCountry("127.0.0.1"), undefined);
36+
assert.equal(findCountry(null), undefined);
37+
});
38+
39+
test("returns random IPs from all data or a country", () => {
40+
const originalRandom = Math.random;
41+
Math.random = () => 0;
42+
43+
try {
44+
assert.equal(random(), "93.123.23.1");
45+
assert.equal(random("Bulgaria"), "93.123.23.1");
46+
assert.equal(random("Unknown"), undefined);
47+
} finally {
48+
Math.random = originalRandom;
49+
}
3450
});
3551

3652
test("returns immutable data", () => {
@@ -41,3 +57,9 @@ test("returns immutable data", () => {
4157
(byCountry.Bulgaria as string[]).push("127.0.0.1");
4258
}, TypeError);
4359
});
60+
61+
test("validates country input", () => {
62+
assert.throws(() => {
63+
getByCountry(123 as unknown as string);
64+
}, TypeError);
65+
});

0 commit comments

Comments
 (0)