Skip to content
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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint.format.enable": true,
"eslint.codeActionsOnSave.mode": "problems",
"eslint.codeActionsOnSave.rules": [
"import/order",
"@stylistic/arrow-parens",
"@stylistic/quotes",
"@stylistic/semi",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
"node": "^20.11.0 || ^22.16.0 || >=24.0.0",
"pnpm": ">=10.x"
},
"packageManager": "pnpm@10.18.2+sha512.9fb969fa749b3ade6035e0f109f0b8a60b5d08a1a87fdf72e337da90dcc93336e2280ca4e44f2358a649b83c17959e9993e777c2080879f3801e6f0d999ad3dd"
"packageManager": "pnpm@10.24.0+sha512.01ff8ae71b4419903b65c60fb2dc9d34cf8bb6e06d03bde112ef38f7a34d6904c424ba66bea5cdcf12890230bf39f9580473140ed9c946fef328b6e5238a345a"
}
12 changes: 8 additions & 4 deletions src/universal/get-last-element-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ export function getLastElementOf<T>(
if (withIndex === true) {
const index = arr.length - 1;

return {
el: arr[index],
index,
};
if (arr[index]) {
return {
el: arr[index],
index,
};
} else {
return undefined;
}
Comment on lines +32 to +39

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

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

This check introduces a bug. Using if (arr[index]) is a truthy check that will fail for valid falsy values like 0, false, "", or null. For example, getLastElementOf([1, 2, 0], {withIndex: true}) would incorrectly return undefined instead of {el: 0, index: 2}.

Since empty arrays are already handled at line 23-25, this additional check is unnecessary and should be removed. The original code was correct.

Suggested change
if (arr[index]) {
return {
el: arr[index],
index,
};
} else {
return undefined;
}
return {
el: arr[index],
index,
};

Copilot uses AI. Check for mistakes.
} else {
return arr[arr.length - 1];
}
Expand Down