Skip to content

Instantly share code, notes, and snippets.

@dashcraft
Created December 26, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dashcraft/4d1e80c564206f93227ad1aceaf25357 to your computer and use it in GitHub Desktop.
Save dashcraft/4d1e80c564206f93227ad1aceaf25357 to your computer and use it in GitHub Desktop.
interface IStrategy {
id: string,
test: (item: string) => boolean,
transform: (item: string) => Promise<any> | string
}
const strategies: IStrategy[] = [
{
id: 'some-name',
test: (item: string) => true | false,
transform: (item: string) => {
return item
},
},
]
function getStrategy(item: string, strategies: IStrategy[]) {
return strategies.reduce((acc: IStrategy | null, strategy: IStrategy, index) => {
if (acc == null) {
return strategy.test(item) ? strategy : acc;
} else {
return acc;
}
}, null);
}
export async function testStrategy(item: string): Promise<any> {
const strategy: IStrategy | null = await getUriStrategy(item, strategies);
if(!strategy) return item
const _out = strategy.transform(item)
return _out || item
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment