diff --git a/src/array/replaceOrAppend.ts b/src/array/replaceOrAppend.ts index 9aac4b04..0ad0add5 100644 --- a/src/array/replaceOrAppend.ts +++ b/src/array/replaceOrAppend.ts @@ -19,10 +19,10 @@ export function replaceOrAppend( 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) { diff --git a/tests/array/replaceOrAppend.test.ts b/tests/array/replaceOrAppend.test.ts index a71f13d6..863631a6 100644 --- a/tests/array/replaceOrAppend.test.ts +++ b/tests/array/replaceOrAppend.test.ts @@ -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]) + }) })