Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/array/replaceOrAppend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function replaceOrAppend<T>(
newItem: T,
match: (a: T, idx: number) => boolean,
): T[] {
if (!array && !newItem) {
if (!array && newItem == null) {
return []
}
if (!newItem) {
if (newItem == null) {
return [...array]
}
if (!array) {
Expand Down
7 changes: 7 additions & 0 deletions tests/array/replaceOrAppend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ describe('replaceOrAppend', () => {
const result = _.replaceOrAppend(letters, 'XX', x => x === 'x')
expect(result).toEqual(lettersXX)
})
test('replaces a falsy new item instead of treating it as missing', () => {
expect(_.replaceOrAppend([1, 2, 3], 0, n => n === 2)).toEqual([1, 0, 3])
expect(_.replaceOrAppend([true], false, x => x === true)).toEqual([false])
})
test('appends a falsy new item when nothing matches', () => {
expect(_.replaceOrAppend([1, 2, 3], 0, n => n > 100)).toEqual([1, 2, 3, 0])
})
})
Loading