Bug: Falsy thisArg (0, '', false) overridden in arrForEach/iterForOf/objForEachKey #561#566
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #566 +/- ##
=======================================
Coverage 99.43% 99.43%
=======================================
Files 150 150
Lines 4452 4460 +8
Branches 949 951 +2
=======================================
+ Hits 4427 4435 +8
Misses 25 25
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes falsy thisArg handling in core iteration helpers so 0, "", and false are passed through instead of being replaced by the iterated target.
Changes:
- Replaces truthy
thisArg || targetfallbacks with null/undefined checks. - Updates
arrForEachcallback typing for array-like inputs. - Adds regression tests for omitted, null/undefined, and falsy
thisArgvalues.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
lib/src/array/forEach.ts |
Updates arrForEach thisArg fallback and callback array parameter type. |
lib/src/iterator/forOf.ts |
Updates iterator callback thisArg fallback. |
lib/src/object/for_each_key.ts |
Updates object key iteration callback thisArg fallback. |
lib/test/src/common/helpers/array.test.ts |
Adds arrForEach regression coverage for thisArg behavior. |
lib/test/src/common/iterator/forOf.test.ts |
Adds iterForOf regression coverage for thisArg behavior. |
lib/test/src/common/object/for_each_key.test.ts |
Adds objForEachKey regression coverage for thisArg behavior. |
Comments suppressed due to low confidence (2)
lib/src/array/forEach.ts:59
- This evaluates the null/undefined fallback on every element even though
thisArgandtheArrayare loop-invariant.arrForEachis a hot utility used throughout the package, so computing the effective callbackthisonce before the loop would avoid an extra helper call per item.
if (callbackfn[CALL](isStrictNullOrUndefined(thisArg) ? theArray as any : thisArg, theArray[idx], idx, theArray) === -1) {
lib/src/object/for_each_key.ts:45
- The effective callback
thisvalue is recomputed for every property even though it does not change during iteration. Computing it once before thefor...inloop would avoid an extra helper call per own property.
if (callbackfn[CALL](isStrictNullOrUndefined(thisArg) ? theObject : thisArg, prop, theObject[prop as keyof T]) === -1) {
…objForEachKey #561 Truthy checks (`thisArg || target`) incorrectly overrode legitimate falsy values such as 0, '', false, and null with the array/iterator/object being iterated, violating standard JavaScript thisArg semantics. Replace the truthy fallback with `isStrictNullOrUndefined(thisArg)` so that only null and undefined trigger the default-to-target behaviour, while any other falsy value is passed through unchanged. Also correct the arrForEach callback signature: the third parameter is `ArrayLike<T>`, not `T[]`, matching the actual argument passed. Fixes #561 Affected files: - lib/src/array/forEach.ts - lib/src/iterator/forOf.ts - lib/src/object/for_each_key.ts
nevware21-bot
approved these changes
May 18, 2026
Contributor
nevware21-bot
left a comment
There was a problem hiding this comment.
Approved by nevware21-bot
nev21
added a commit
that referenced
this pull request
May 19, 2026
## Release v0.14.0 This PR increases the version to `0.14.0` and updates the changelog with all significant changes since v0.13.0. ### Summary of Changes #### Features - New array helpers and array-like detection (#525) - `strReplace` and `strReplaceAll` string helpers (#527) - `strCapitalizeWords` helper (#528) - `strTruncate`, `strCount`, `strAt`, `strMatchAll` helpers (#529, #530) - `arrFlatMap` with ES5 polyfill (#533) - Typing utilities and expanded TSDoc examples (#535) - `isAsyncIterable` and `isIntegerInRange` helpers (#536) - `strStartsWithAny`, `strEndsWithAny`, `strWrap`, `strUnwrap`, `strNormalizeNewlines` (#543) - New object utility helpers and prototype pollution hardening (#564, #565) #### Bug Fixes - Fix ES2015 built-in type errors in consumers by adding lib reference directive to published declarations (#558) - Fix `thisArg` binding in `polyArrFindIndex` / `polyArrFindLastIndex` polyfills (#562) - Fix falsy `thisArg` (0, `''`, `false`) being overridden in `arrForEach`, `iterForOf`, `objForEachKey` (#566) #### Repository Improvements - Drop Node.js 16 from CI matrix and add Node.js 24 (#549) - Upgrade Grunt devDependency to v1.6.2 (#552) - Add funding metadata to published package manifests (#554) ### Files Updated - `CHANGELOG.md` — Added v0.14.0 entry - `package.json` — Version bumped to 0.14.0 - `lib/package.json` — Version bumped to 0.14.0 - `README.md` — Updated recommended version specification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Truthy checks (
thisArg || target) incorrectly overrode legitimate falsy values such as 0, '', false, and null with the array/iterator/object being iterated, violating standard JavaScript thisArg semantics.Replace the truthy fallback with
isStrictNullOrUndefined(thisArg)so that only null and undefined trigger the default-to-target behaviour, while any other falsy value is passed through unchanged.Also correct the arrForEach callback signature: the third parameter is
ArrayLike<T>, notT[], matching the actual argument passed.Fixes #561
Affected files: