Skip to content

Commit

Permalink
Merge pull request #99 from Lemoncode/bugfix/fix-reduce-async-error
Browse files Browse the repository at this point in the history
Bugfix/fix reduce async error
  • Loading branch information
nasdan authored Apr 28, 2022
2 parents c7fdc23 + bc778a1 commit ae71b40
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lemoncode/fonk",
"version": "1.5.2",
"version": "1.5.3",
"description": "Form schema validator library",
"main": "dist/@lemoncode/fonk.cjs.js",
"module": "dist/@lemoncode/fonk.esm.js",
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,24 @@ describe('safeObjectKeys', () => {
});

describe('reduceAsync', () => {
it('should return default value when it calls with array equals undefined', async () => {
// Arrange
const array = undefined;

const sumAsync = async (a, b): Promise<number> => {
return a + b;
};

const callback = jest.fn();
const defaultValue = 0;

// Act
const result = await reduceAsync(array, callback, defaultValue);

// Assert
expect(result).toEqual(defaultValue);
});

it('should return default value when it calls with array equals empty', async () => {
// Arrange
const array = [];
Expand Down
8 changes: 5 additions & 3 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export const reduceAsync = <Entity, Result>(
callback: (acc: Result, entity: Entity, index?: number) => Promise<Result>,
defaultResult: Result | Promise<Result>
): Promise<any> =>
collection.reduce<Promise<Result>>((promise, item, index) => {
return promise.then((result) => callback(result, item, index));
}, Promise.resolve(defaultResult));
Array.isArray(collection)
? collection.reduce<Promise<Result>>((promise, item, index) => {
return promise.then((result) => callback(result, item, index));
}, Promise.resolve(defaultResult))
: Promise.resolve(defaultResult);

export const isFieldIdInSchema = (
fieldId: string,
Expand Down

0 comments on commit ae71b40

Please sign in to comment.