Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace escape condition on falsy values #309

Merged
merged 2 commits into from
Jun 26, 2023
Merged
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
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
Loading