-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #159
- Loading branch information
Showing
12 changed files
with
55 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, it, expect } from 'bun:test'; | ||
|
||
import { getUrl } from 'utils/getURL'; | ||
|
||
describe('getUrl', () => { | ||
it('should work with simple urls', () => { | ||
expect(getUrl('https://www.google.fr')).toEqual(new URL('https://www.google.fr')); | ||
expect(getUrl('https://github.com/e-krebs/pile')).toEqual( | ||
new URL('https://github.com/e-krebs/pile') | ||
); | ||
}); | ||
|
||
it('should work with known edge cases', () => { | ||
expect(getUrl('svg.wtf')).toEqual(new URL('https://svg.wtf')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
import { get } from './get'; | ||
import { getUrl } from './getURL'; | ||
import type { Service } from './services'; | ||
import { ListItem } from './typings'; | ||
import { beautifyUrl } from './url'; | ||
|
||
export const getMatchingListItem = async ( | ||
currentUrl: URL, | ||
currentUrl: URL | string, | ||
services: Service[] | ||
): Promise<ListItem | undefined> => { | ||
const currentURL = typeof currentUrl === 'string' ? getUrl(currentUrl) : currentUrl; | ||
const matching = await Promise.all( | ||
services | ||
.map(async (service) => { | ||
const isConnected = await service.isConnected(); | ||
if (!isConnected) return; | ||
const { data } = await get(service); | ||
return data.find((item) => { | ||
const url = new URL(item.url); | ||
return urlsAreMatching(url, currentUrl); | ||
}); | ||
return data.find((item) => urlsAreMatching(item.url, currentURL)); | ||
}) | ||
.flat() | ||
); | ||
return matching.find((item) => item !== undefined); | ||
}; | ||
|
||
export const currentUrlIsMatching = async (currentUrl: URL, services: Service[]): Promise<boolean> => { | ||
export const currentUrlIsMatching = async ( | ||
currentUrl: URL | string, | ||
services: Service[] | ||
): Promise<boolean> => { | ||
const currentURL = typeof currentUrl === 'string' ? getUrl(currentUrl) : currentUrl; | ||
const matches = await Promise.all( | ||
services.map(async (service) => { | ||
const isConnected = await service.isConnected(); | ||
if (!isConnected) return false; | ||
const { data } = await get(service); | ||
const matchingUrls = data | ||
.map((i) => new URL(i.url)) | ||
.filter((iUrl) => urlsAreMatching(iUrl, currentUrl)); | ||
const matchingUrls = data.filter(({ url }) => urlsAreMatching(url, currentURL)); | ||
return matchingUrls.length > 0; | ||
}) | ||
); | ||
const isMatching = matches.reduce((a, b) => a || b); | ||
return isMatching; | ||
}; | ||
|
||
export const urlsAreMatching = (url1: URL, url2: URL): boolean => | ||
beautifyUrl(url1.origin) === beautifyUrl(url2.origin) && | ||
beautifyUrl(url1.pathname) === beautifyUrl(url2.pathname) && | ||
url1.search === url2.search; | ||
export const urlsAreMatching = (url1: URL | string, url2: URL): boolean => { | ||
const URL1 = typeof url1 === 'string' ? getUrl(url1) : url1; | ||
return ( | ||
beautifyUrl(URL1.origin) === beautifyUrl(url2.origin) && | ||
beautifyUrl(URL1.pathname) === beautifyUrl(url2.pathname) && | ||
url1.search === url2.search | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const getUrl = (url: string): URL => { | ||
try { | ||
return new URL(url); | ||
} catch { | ||
// very basic, should probably be improved | ||
return new URL(`https://${url}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters