-
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.
* fix: glob imports * chore: linting
- Loading branch information
Showing
5 changed files
with
100 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export interface GlobModule { | ||
default: any; | ||
} | ||
|
||
export interface GlobState { | ||
getRandomImport: () => any; | ||
getImport: (key: string) => any; | ||
} | ||
|
||
export function useGlob(glob: Record<string, GlobModule>): GlobState { | ||
/** | ||
* Get a random import from the glob. | ||
* | ||
* @returns A random import from the glob. | ||
*/ | ||
function getRandomImport(): any { | ||
const keys = Object.keys(glob); | ||
const randomKey = keys[Math.floor(Math.random() * keys.length)]; | ||
return glob[randomKey].default; | ||
} | ||
|
||
/** | ||
* Get an import from the glob. | ||
* | ||
* @param key | ||
*/ | ||
function getImport(key: string): any { | ||
if (key in glob) { | ||
return glob[key].default; | ||
} | ||
|
||
return Object.values(glob).find((module) => { | ||
const parts = module.default.split('/'); | ||
return parts[parts.length - 1] === key; | ||
})?.default; | ||
} | ||
|
||
return { | ||
getRandomImport, | ||
getImport, | ||
}; | ||
} |
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