Created
December 26, 2022 20:21
-
-
Save dashcraft/4d1e80c564206f93227ad1aceaf25357 to your computer and use it in GitHub Desktop.
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
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