Skip to content

Commit

Permalink
fix: replace escape condition on falsy values (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Jun 26, 2023
1 parent 36df87e commit 28f8114
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const replace = <T>(
match: (item: T, idx: number) => boolean
): T[] => {
if (!list) return []
if (!newItem) return [...list]
if (newItem === undefined) return [...list]
for (let idx = 0; idx < list.length; idx++) {
const item = list[idx]
if (match(item, idx)) {
Expand Down
8 changes: 6 additions & 2 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,14 @@ describe('array module', () => {
)
assert.deepEqual(result, [])
})
test('returns the list for a null new item', () => {
const result = _.replace(['a'], null, () => false)
test('returns the list for an undefined new item', () => {
const result = _.replace(['a'], undefined, () => true)
assert.deepEqual(result, ['a'])
})
test('returns replaced item when value is null', () => {
const result = _.replace(['a'], null, i => i === 'a')
assert.deepEqual(result, [null])
})
test('returns replaced item by index', () => {
const result = _.replace(
['a', 'b', 'c', 'd'],
Expand Down

1 comment on commit 28f8114

@vercel
Copy link

@vercel vercel bot commented on 28f8114 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.