Skip to content

Commit b177e73

Browse files
Copilotfregante
andcommitted
Rename wait to waitFor and export via utils only
- Renamed wait() to waitFor() as requested - Moved export to utils object instead of direct export - Reverted type assertion changes in index.test.ts - Updated README to show usage via utils.waitFor() - Updated JSDoc example to reflect new API Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
1 parent 4efdd75 commit b177e73

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

index.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
2222
continue;
2323
}
2424

25-
// Skip wait and other utility functions
26-
if (detectName === 'wait') {
27-
continue;
28-
}
29-
3025
const validURLs = getTests(detectName);
3126

3227
if (validURLs[0] === 'combinedTestOnly' || String(detect).startsWith('() =>')) {
3328
continue;
3429
}
3530

36-
// Type assertion for TypeScript to understand this is a detection function
37-
const detectionFn = detect as (url?: URL | HTMLAnchorElement | Location) => boolean;
38-
3931
test(detectName + ' has tests', () => {
4032
assert.ok(
4133
Array.isArray(validURLs),
@@ -50,7 +42,7 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
5042
for (const url of validURLs) {
5143
test(`${detectName} ${url.replace('https://github.com', '')}`, () => {
5244
assert.ok(
53-
detectionFn(new URL(url)),
45+
detect(new URL(url)),
5446
stripIndent(`
5547
Is this URL \`${detectName}\`?
5648
${url.replace('https://github.com', '')}
@@ -71,7 +63,7 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
7163
if (!validURLs.includes(url)) {
7264
test(`${detectName} NO ${url}`, () => {
7365
assert.equal(
74-
detectionFn(new URL(url)),
66+
detect(new URL(url)),
7567
false,
7668
stripIndent(`
7769
Is this URL \`${detectName}\`?
@@ -297,25 +289,25 @@ test('parseRepoExplorerTitle', () => {
297289
);
298290
});
299291

300-
test('wait - immediately true', async () => {
292+
test('waitFor - immediately true', async () => {
301293
const detection = () => true;
302-
const result = await pageDetect.wait(detection);
294+
const result = await pageDetect.utils.waitFor(detection);
303295
assert.equal(result, true);
304296
});
305297

306-
test('wait - becomes true', async () => {
298+
test('waitFor - becomes true', async () => {
307299
let callCount = 0;
308300
const detection = () => {
309301
callCount++;
310302
return callCount >= 3;
311303
};
312304

313-
const result = await pageDetect.wait(detection);
305+
const result = await pageDetect.utils.waitFor(detection);
314306
assert.equal(result, true);
315307
assert.ok(callCount >= 3);
316308
});
317309

318-
test('wait - false when document complete', async () => {
310+
test('waitFor - false when document complete', async () => {
319311
// Save original state
320312
const originalReadyState = Object.getOwnPropertyDescriptor(document, 'readyState');
321313

@@ -327,7 +319,7 @@ test('wait - false when document complete', async () => {
327319
});
328320

329321
const detection = () => false;
330-
const result = await pageDetect.wait(detection);
322+
const result = await pageDetect.utils.waitFor(detection);
331323
assert.equal(result, false);
332324

333325
// Restore original state

index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const exists = (selector: string) => Boolean($(selector));
1111
* @returns A promise that resolves to the final result of the detection
1212
* @example
1313
* ```
14-
* import {wait, isOrganizationProfile} from 'github-url-detection';
14+
* import {utils} from 'github-url-detection';
1515
*
1616
* async function init() {
17-
* if (!await wait(isOrganizationProfile)) {
17+
* if (!await utils.waitFor(isOrganizationProfile)) {
1818
* return;
1919
* }
2020
* // Do something when on organization profile
2121
* }
2222
* ```
2323
*/
24-
export async function wait(detection: () => boolean): Promise<boolean> {
24+
async function waitFor(detection: () => boolean): Promise<boolean> {
2525
// eslint-disable-next-line no-await-in-loop -- We need to wait on each frame
2626
while (!detection() && document.readyState !== 'complete') {
2727
// eslint-disable-next-line no-await-in-loop
@@ -991,4 +991,5 @@ export const utils = {
991991
getCleanGistPathname,
992992
getRepositoryInfo: getRepo,
993993
parseRepoExplorerTitle,
994+
waitFor,
994995
};

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ if (pageDetect.isOrganizationProfile()) {
6767
}
6868
```
6969

70-
### Async detections with `wait`
70+
### Async detections with `waitFor`
7171

72-
The `wait` helper function allows you to wait for a detection to become true by repeatedly checking it on each animation frame. This is useful for DOM-based detections that need to wait for elements to appear before the document is fully loaded.
72+
The `waitFor` helper function allows you to wait for a detection to become true by repeatedly checking it on each animation frame. This is useful for DOM-based detections that need to wait for elements to appear before the document is fully loaded.
7373

7474
```js
75-
import {wait, isOrganizationProfile} from 'github-url-detection';
75+
import {utils, isOrganizationProfile} from 'github-url-detection';
7676

7777
async function init() {
7878
// Wait for the detection to return true or for the document to be complete
79-
if (!await wait(isOrganizationProfile)) {
79+
if (!await utils.waitFor(isOrganizationProfile)) {
8080
return; // Not an organization profile
8181
}
8282

@@ -85,7 +85,7 @@ async function init() {
8585
}
8686
```
8787

88-
The `wait` function:
88+
The `waitFor` function:
8989
- Repeatedly calls the detection function on each animation frame
9090
- Stops when the detection returns `true` or when `document.readyState` is `'complete'`
9191
- Returns the final result of the detection

0 commit comments

Comments
 (0)