fix(dropWhile): dropWhile returns the whole array when every element does not match the predicate (#49)

* fix: bug in dropWhile

* test: adds testcase for dropRightWhile

* Update src/array/dropWhile.ts

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
This commit is contained in:
Changwoo Yoo 2024-06-15 11:51:54 +09:00 committed by GitHub
parent 74ceaf47ec
commit 212f9709ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 0 deletions

View File

@ -21,5 +21,7 @@ describe('dropRightWhile', () => {
enabled: true,
},
]);
expect(dropRightWhile([1, 2, 3], x => x < 4)).toEqual([]);
});
});

View File

@ -18,5 +18,7 @@ describe('dropWhile', () => {
},
{ id: 3, enabled: false },
]);
expect(dropWhile([1, 2, 3], x => x < 4)).toEqual([]);
});
});

View File

@ -17,5 +17,9 @@
*/
export function dropWhile<T>(arr: readonly T[], canContinueDropping: (item: T) => boolean): T[] {
const dropEndIndex = arr.findIndex(item => !canContinueDropping(item));
if (dropEndIndex === -1) {
return [];
}
return arr.slice(dropEndIndex);
}