Skip to content

[BUGFIX] Fix EmberArray.reduce to match native behavior - #21168

Merged
NullVoxPopuli merged 1 commit into
emberjs:mainfrom
crazylogic03:fix/ember-array-reduce
Mar 4, 2026
Merged

[BUGFIX] Fix EmberArray.reduce to match native behavior#21168
NullVoxPopuli merged 1 commit into
emberjs:mainfrom
crazylogic03:fix/ember-array-reduce

Conversation

@crazylogic03

Copy link
Copy Markdown
Contributor

Description

Fixes a FIXME where EmberArray#reduce did not match the native Array.prototype.reduce behavior when no initialValue was provided. See packages/@ember/array/index.ts.

  • If no initialValue is provided and the array is empty, it now throws a TypeError (consistent with native).
  • If no initialValue is provided, it uses the first item as the initial value and begins iterating from index 1.

Tested with new test cases in reduce-test.js.

Verification

pnpm lint:format:fix and pnpm test were run to ensure formatting is correct and no regressions were introduced.

@crazylogic03

Copy link
Copy Markdown
Contributor Author

@NullVoxPopuli Please Have a Review On this PR and Let me Know if this requires any Update or changes

Comment thread .github/workflows/package-size.yml Outdated
- uses: actions/checkout@v6
- uses: ./.github/actions/setup
- uses: wyvox/pkg-size@029acee084efc5ab835fee5f515ae5c3fc7549c1
continue-on-error: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please undo this change -- i have a fix here: #21166

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done! I've reverted the package-size.yml changes in my latest commit.

@NullVoxPopuli

NullVoxPopuli commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

For my own context, any reason to use ember array instead of trackedArray?

Reason i ask is that we're aiming to eliminate EmberArray during v7, hopefully for removal in v8 i think.

If you need the reactivity, trackedArray should hopefully solve this as well.

What motivated this change to EmberArray?

}

for (let i = startIndex; i < this.length; i++) {
let item = this.objectAt(i) as T;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why objectAt instead of [] access? Oc .at()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since EmberArray is a mixin (used by ArrayProxy, etc.), it doesn't always guarantee that native [] bracket access or .at() will work to retrieve elements. objectAt is the safest and standard method defined by the Enumerable/EmberArray API for retrieving items.

this.assert.equal(res, obj);
}

'@test works without an initialValue'() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let obj = this.newObject([1, 2, 3]);
let res = obj.reduce((previousValue, item, index) => previousValue + index);
// starts at item 2 (index 1): 1 + 1 = 2
// then item 3 (index 2): 2 + 2 = 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TIL reduce without initialValue starts iteration at 1st index rather than 0 (because 0th index is used for initial value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tests match MDN's destribed behavior ✅

@crazylogic03

Copy link
Copy Markdown
Contributor Author

I noticed there was a long-standing FIXME comment right above the EmberArray#reduce method (packages/@ember/array/index.ts:1386) stating that the behavior didn't match native Array.prototype.reduce when called without an initialValue. I'm just exploring the codebase and trying to clean up some of these old FIXME/TODO comments!

@NullVoxPopuli

Copy link
Copy Markdown
Contributor

@crazylogic03 can you rebase this PR? the pkg-size update was merged so your change to that workflow isn't needed, and I fear would cause a conflict -- (or GH isn't diffing correctly, idk)

@crazylogic03
crazylogic03 force-pushed the fix/ember-array-reduce branch from 1ba1bb7 to b58a197 Compare March 4, 2026 18:33
@crazylogic03

Copy link
Copy Markdown
Contributor Author

@NullVoxPopuli Done! I've removed the pkg-size update and rebased the commits against the latest main. Everything should be clean now!

@kategengler kategengler changed the title [BUGFIX beta] Fix EmberArray.reduce to match native behavior [BUGFIX] Fix EmberArray.reduce to match native behavior Mar 4, 2026
@NullVoxPopuli
NullVoxPopuli merged commit 6a1bd45 into emberjs:main Mar 4, 2026
35 checks passed
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.

2 participants