Description
Several iteration functions use truthy checks for thisArg fallback, which means legitimate falsy values (0, '', false, null) are incorrectly overridden.
Affected locations:
lib/src/array/forEach.ts line 58
lib/src/iterator/forOf.ts line 71
lib/src/object/for_each_key.ts
Impact
When a user passes 0, empty string, or false as thisArg, the function silently replaces it with the array/object being iterated, leading to incorrect this context in callbacks.
Steps to Reproduce
javascript arrForEach([1,2,3], function(v) { console.log(this); // Expected: 0, Actual: the array }, 0);
Suggested Fix
Use nullish coalescing (??) or explicit undefined check instead of truthy check:
ypescript // Instead of: thisArg || theArray thisArg !== undefined ? thisArg : theArray
Severity
Medium — edge case but violates expected JavaScript semantics for thisArg.
Description
Several iteration functions use truthy checks for
thisArgfallback, which means legitimate falsy values (0,'',false,null) are incorrectly overridden.Affected locations:
lib/src/array/forEach.tsline 58lib/src/iterator/forOf.tsline 71lib/src/object/for_each_key.tsImpact
When a user passes
0, empty string, orfalseasthisArg, the function silently replaces it with the array/object being iterated, leading to incorrectthiscontext in callbacks.Steps to Reproduce
javascript arrForEach([1,2,3], function(v) { console.log(this); // Expected: 0, Actual: the array }, 0);Suggested Fix
Use nullish coalescing (
??) or explicitundefinedcheck instead of truthy check:ypescript // Instead of: thisArg || theArray thisArg !== undefined ? thisArg : theArraySeverity
Medium — edge case but violates expected JavaScript semantics for
thisArg.