Skip to content

Commit a93e8e3

Browse files
committed
Apply formatting and style fixes
- Fix trailing whitespace in test descriptions - Use toBe() instead of toEqual() for primitive comparisons - Add parentheses to single-param arrow functions - Format long object literals across multiple lines - Use toBeDefined() instead of not.toBeUndefined() https://claude.ai/code/session_01UabaLo3R3sXovyxaZo2yCF
1 parent 3ff889d commit a93e8e3

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/ipdata.test.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ const MOCK_IP1_DATA = {
2626
company: { name: 'Cloudflare, Inc.', domain: 'cloudflare.com', network: '1.1.1.0/24', type: 'hosting' },
2727
languages: [{ name: 'English', native: 'English', code: 'en' }],
2828
currency: { name: 'US Dollar', code: 'USD', symbol: '$', native: '$', plural: 'US dollars' },
29-
time_zone: { name: 'America/Los_Angeles', abbr: 'PST', offset: '-0800', is_dst: false, current_time: '2024-01-01T00:00:00-08:00' },
29+
time_zone: {
30+
name: 'America/Los_Angeles',
31+
abbr: 'PST',
32+
offset: '-0800',
33+
is_dst: false,
34+
current_time: '2024-01-01T00:00:00-08:00',
35+
},
3036
threat: {
3137
is_tor: false,
3238
is_proxy: false,
@@ -85,7 +91,7 @@ describe('constructor()', () => {
8591

8692
it('should configure the cache by default', () => {
8793
const ipdata = new IPData(TEST_API_KEY);
88-
expect(ipdata.cache.max).toEqual(4096);
94+
expect(ipdata.cache.max).toBe(4096);
8995
expect(ipdata.cache.ttl).toEqual(1000 * 60 * 60 * 24);
9096
});
9197

@@ -99,12 +105,12 @@ describe('constructor()', () => {
99105

100106
it('should use the default base URL', () => {
101107
const ipdata = new IPData(TEST_API_KEY);
102-
expect(ipdata.baseUrl).toEqual('https://api.ipdata.co/');
108+
expect(ipdata.baseUrl).toBe('https://api.ipdata.co/');
103109
});
104110

105111
it('should accept a custom base URL', () => {
106112
const ipdata = new IPData(TEST_API_KEY, undefined, EU_BASE_URL);
107-
expect(ipdata.baseUrl).toEqual('https://eu-api.ipdata.co/');
113+
expect(ipdata.baseUrl).toBe('https://eu-api.ipdata.co/');
108114
});
109115
});
110116

@@ -192,7 +198,7 @@ describe('lookup()', () => {
192198
});
193199

194200
describe('selectField', () => {
195-
it('should throw an error for an invlaid field ', async () => {
201+
it('should throw an error for an invlaid field', async () => {
196202
const field = 'field';
197203
await expect(ipdata.lookup(undefined, field)).rejects.toThrow(`${field} is not a valid field.`);
198204
});
@@ -207,7 +213,7 @@ describe('lookup()', () => {
207213
});
208214

209215
describe('fields', () => {
210-
it('should throw an error for an invlaid field ', async () => {
216+
it('should throw an error for an invlaid field', async () => {
211217
const field = 'field';
212218
const fields = [field];
213219
await expect(ipdata.lookup(undefined, undefined, fields)).rejects.toThrow(`${field} is not a valid field.`);
@@ -227,7 +233,12 @@ describe('lookup()', () => {
227233

228234
describe('company field', () => {
229235
it('should accept company as a valid select field', async () => {
230-
const companyData = { name: 'Cloudflare, Inc.', domain: 'cloudflare.com', network: '1.1.1.0/24', type: 'hosting' };
236+
const companyData = {
237+
name: 'Cloudflare, Inc.',
238+
domain: 'cloudflare.com',
239+
network: '1.1.1.0/24',
240+
type: 'hosting',
241+
};
231242
mockFetch.mockReturnValueOnce(mockFetchResponse(companyData));
232243
const info = await ipdata.lookup(TEST_IP, 'company');
233244
expect(info).toHaveProperty('company');
@@ -308,7 +319,7 @@ describe('bulkLookup()', () => {
308319
const IP3 = '1.0.0.1';
309320
mockFetch.mockReturnValueOnce(mockFetchResponse(MOCK_IP1_DATA));
310321
await ipdata.lookup(IP1);
311-
expect(ipdata.cache.get(IP1)).not.toBeUndefined();
322+
expect(ipdata.cache.get(IP1)).toBeDefined();
312323
mockFetch.mockReturnValueOnce(mockFetchResponse([MOCK_IP2_DATA, MOCK_IP3_DATA]));
313324
const result = await ipdata.bulkLookup([IP1, IP2, IP3]);
314325
const info = keyBy(result);
@@ -321,7 +332,7 @@ describe('bulkLookup()', () => {
321332
});
322333

323334
describe('fields', () => {
324-
it('should throw an error for an invlaid field ', async () => {
335+
it('should throw an error for an invlaid field', async () => {
325336
const field = 'field';
326337
const fields = [field];
327338
await expect(ipdata.bulkLookup([IP1, IP2], fields)).rejects.toThrow(`${field} is not a valid field.`);
@@ -331,10 +342,12 @@ describe('bulkLookup()', () => {
331342
const field1 = 'ip';
332343
const field2 = 'is_eu';
333344
const fields = [field1, field2];
334-
mockFetch.mockReturnValueOnce(mockFetchResponse([
335-
{ ip: IP1, is_eu: false },
336-
{ ip: IP2, is_eu: true },
337-
]));
345+
mockFetch.mockReturnValueOnce(
346+
mockFetchResponse([
347+
{ ip: IP1, is_eu: false },
348+
{ ip: IP2, is_eu: true },
349+
]),
350+
);
338351
const result = await ipdata.bulkLookup([IP1, IP2], fields);
339352
const info = keyBy(result);
340353
expect(info[IP1]).toHaveProperty(field1, IP1);

src/ipdata.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function isValidFields(fields: string[]): boolean {
5454
throw new Error('Fields should be an array.');
5555
}
5656

57-
fields.forEach(field => {
57+
fields.forEach((field) => {
5858
const index = VALID_FIELDS.indexOf(field);
5959
if (index === -1) {
6060
throw new Error(`${field} is not a valid field.`);
@@ -186,11 +186,11 @@ export default class IPData {
186186
const response = await fetch(url.toString());
187187

188188
if (!response.ok) {
189-
const errorData = await response.json().catch(() => ({})) as object;
189+
const errorData = (await response.json().catch(() => ({}))) as object;
190190
return { ...errorData, status: response.status } as LookupResponse;
191191
}
192192

193-
const responseData = await response.json() as any;
193+
const responseData = (await response.json()) as any;
194194
let data: LookupResponse;
195195

196196
if (selectField) {
@@ -211,7 +211,7 @@ export default class IPData {
211211
throw new Error('Bulk Lookup requires more than 1 IP Address in the payload.');
212212
}
213213

214-
ips.forEach(ip => {
214+
ips.forEach((ip) => {
215215
if (!isValidIP(ip)) {
216216
throw new Error(`${ip} is an invalid IP address.`);
217217
}
@@ -241,12 +241,12 @@ export default class IPData {
241241
});
242242

243243
if (!response.ok) {
244-
const errorData = await response.json().catch(() => ({})) as object;
244+
const errorData = (await response.json().catch(() => ({}))) as object;
245245
return { ...errorData, status: response.status } as unknown as LookupResponse[];
246246
}
247247

248-
const responseData = await response.json() as any[];
249-
responseData.forEach(info => {
248+
const responseData = (await response.json()) as any[];
249+
responseData.forEach((info) => {
250250
this.cache.set(info.ip, { ...info, status: response.status });
251251
responses.push(this.cache.get(info.ip)!);
252252
});

0 commit comments

Comments
 (0)