Skip to content

Bug: Falsy thisArg (0, '', false) overridden in arrForEach/iterForOf/objForEachKey #561#566

Merged
nev21 merged 1 commit into
mainfrom
nev21/Bugs
May 18, 2026
Merged

Bug: Falsy thisArg (0, '', false) overridden in arrForEach/iterForOf/objForEachKey #561#566
nev21 merged 1 commit into
mainfrom
nev21/Bugs

Conversation

@nev21
Copy link
Copy Markdown
Contributor

@nev21 nev21 commented May 18, 2026

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

@nev21 nev21 added this to the 0.14.0 milestone May 18, 2026
@nev21 nev21 requested a review from a team as a code owner May 18, 2026 03:45
Copilot AI review requested due to automatic review settings May 18, 2026 03:45
@nev21 nev21 requested a review from a team as a code owner May 18, 2026 03:45
@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.43%. Comparing base (9362b6e) to head (fa10045).

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           
Files with missing lines Coverage Δ
lib/src/array/forEach.ts 100.00% <100.00%> (ø)
lib/src/iterator/forOf.ts 100.00% <100.00%> (ø)
lib/src/object/for_each_key.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 || target fallbacks with null/undefined checks.
  • Updates arrForEach callback typing for array-like inputs.
  • Adds regression tests for omitted, null/undefined, and falsy thisArg values.

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 thisArg and theArray are loop-invariant. arrForEach is a hot utility used throughout the package, so computing the effective callback this once 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 this value is recomputed for every property even though it does not change during iteration. Computing it once before the for...in loop would avoid an extra helper call per own property.
                if (callbackfn[CALL](isStrictNullOrUndefined(thisArg) ? theObject : thisArg, prop, theObject[prop as keyof T]) === -1) {

Comment thread lib/src/array/forEach.ts Outdated
Comment thread lib/src/iterator/forOf.ts
Comment thread lib/src/object/for_each_key.ts
…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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nev21 nev21 enabled auto-merge (squash) May 18, 2026 04:22
Copy link
Copy Markdown
Contributor

@nevware21-bot nevware21-bot left a comment

Choose a reason for hiding this comment

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

Approved by nevware21-bot

@nev21 nev21 merged commit 0a486d3 into main May 18, 2026
13 of 14 checks passed
@nev21 nev21 deleted the nev21/Bugs branch May 18, 2026 04:23
nev21 added a commit that referenced this pull request May 19, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Falsy thisArg (0, '', false) overridden in arrForEach/iterForOf/objForEachKey

3 participants