-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f16a81
commit cd9d588
Showing
19 changed files
with
179 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const asyncCompose = <T>(...fns: Array<(arg: T) => Promise<T> | T>) => { | ||
return async (initialValue: T): Promise<T> => { | ||
return fns.reduceRight(async (promise: Promise<T>, fn) => { | ||
const value = await promise; | ||
|
||
return Promise.resolve(fn(value)); | ||
}, Promise.resolve(initialValue)); | ||
}; | ||
}; |
68 changes: 68 additions & 0 deletions
68
packages/ui/src/common/utils/async-compose/async-compose.unit.test.ts
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,68 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { asyncCompose } from './async-compose'; | ||
|
||
describe('asyncCompose', () => { | ||
it('should compose async functions from right to left', async () => { | ||
const addOne = async (x: number) => x + 1; | ||
const multiplyByTwo = async (x: number) => x * 2; | ||
const subtractThree = async (x: number) => x - 3; | ||
|
||
const composed = asyncCompose(subtractThree, multiplyByTwo, addOne); | ||
const result = await composed(5); | ||
|
||
// ((5 + 1) * 2) - 3 = 9 | ||
expect(result).toBe(9); | ||
}); | ||
|
||
it('should work with mix of sync and async functions', async () => { | ||
const addOne = (x: number) => x + 1; | ||
const multiplyByTwo = async (x: number) => x * 2; | ||
const subtractThree = (x: number) => x - 3; | ||
|
||
const composed = asyncCompose(subtractThree, multiplyByTwo, addOne); | ||
const result = await composed(5); | ||
|
||
expect(result).toBe(9); | ||
}); | ||
|
||
it('should handle single function', async () => { | ||
const addOne = async (x: number) => x + 1; | ||
|
||
const composed = asyncCompose(addOne); | ||
const result = await composed(5); | ||
|
||
expect(result).toBe(6); | ||
}); | ||
|
||
it('should handle empty function array', async () => { | ||
const composed = asyncCompose(); | ||
const result = await composed(5); | ||
|
||
expect(result).toBe(5); | ||
}); | ||
|
||
it('should maintain function execution order', async () => { | ||
const executionOrder: number[] = []; | ||
|
||
const fn1 = async (x: number) => { | ||
executionOrder.push(1); | ||
|
||
return x; | ||
}; | ||
const fn2 = async (x: number) => { | ||
executionOrder.push(2); | ||
|
||
return x; | ||
}; | ||
const fn3 = async (x: number) => { | ||
executionOrder.push(3); | ||
|
||
return x; | ||
}; | ||
|
||
const composed = asyncCompose(fn1, fn2, fn3); | ||
await composed(5); | ||
|
||
expect(executionOrder).toEqual([3, 2, 1]); | ||
}); | ||
}); |
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 @@ | ||
export * from './async-compose'; |
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
4 changes: 2 additions & 2 deletions
4
...tField/hooks/useDocumentUpload/helpers/compose-path-to-file-id/compose-path-to-file-id.ts
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,5 +1,5 @@ | ||
export const composePathToFileId = ( | ||
documentIndex: number, | ||
pageProperty: string, | ||
pageIndex: number, | ||
pageProperty = 'ballerineFileId', | ||
pageIndex = 0, | ||
) => `[${documentIndex}].pages[${pageIndex}].${pageProperty}`; |
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
Oops, something went wrong.