Skip to content

fix: preserve i18n and versions in filter method#900

Open
mateo-leal wants to merge 3 commits intoWFCD:masterfrom
mateo-leal:filter-map-preserve-i18n
Open

fix: preserve i18n and versions in filter method#900
mateo-leal wants to merge 3 commits intoWFCD:masterfrom
mateo-leal:filter-map-preserve-i18n

Conversation

@mateo-leal
Copy link
Copy Markdown

@mateo-leal mateo-leal commented Apr 10, 2026

What did you fix?

Using .filter function now keeps the i18n object


Reproduction steps


Evidence/screenshot/link to line

Considerations

  • Does this contain a new dependency? No
  • Does this introduce opinionated data formatting or manual data entry? No
  • Does this PR include updated data files in a separate commit that can be reverted for a clean code-only PR? No
  • Have I run the linter? Yes
  • Is a bug fix, feature request, or enhancement? Bug Fix/Enhancement

Summary by CodeRabbit

  • Enhancements

    • Filter operations now preserve and propagate localization (i18n) and version metadata from the source collection, and trim the localization to only entries remaining after filtering.
  • Tests

    • Added tests verifying that filtering preserves localization and version metadata, that filtered localization contains only retained entries, and that an empty source localization yields an empty result.

@mateo-leal mateo-leal requested a review from a team as a code owner April 10, 2026 07:19
@mateo-leal mateo-leal requested review from TobiTenno and Copilot April 10, 2026 07:19
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

📝 Walkthrough

Walkthrough

Items.filter(fn) now, when this.i18n exists, builds a new i18n object containing only entries for the kept items and the returned array now carries versions copied from the source. No public signatures changed; map() was not functionally altered.

Changes

Cohort / File(s) Summary
Core Implementation
index.js, index.mjs
Items.filter(fn) collects uniqueName of matched elements and, if this.i18n is present, constructs A.i18n containing only those keys; it also sets A.versions = this.versions. No exported signatures changed.
Test Suite
test/index.spec.mjs
Added tests validating that filter() preserves versions, narrows i18n to matched uniqueName keys, and handles an edge case where items.i18n is emptied before filtering.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰
I hopped the list and kept a few,
I trimmed the names to just the true.
Versions tucked snug, i18n made small,
A tiny hop, metadata tall. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title accurately summarizes the main changes: preserving i18n and versions properties in the filter method, which aligns with the core modifications across index.js, index.mjs, and test files.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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 metadata loss when using Items.filter() and Items.map() by ensuring the returned arrays retain the i18n and versions data that the Items wrapper provides.

Changes:

  • Preserve versions on arrays returned from filter() and map().
  • Preserve i18n on arrays returned from filter() and map(), and narrow i18n to only matched items for filter().
  • Add tests to validate i18n/versions preservation and i18n narrowing behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
test/index.spec.mjs Adds regression tests for i18n/versions preservation across filter()/map() and correct i18n narrowing on filtered results.
index.mjs Updates ESM Items.filter()/map() to carry i18n/versions onto returned arrays (and filter i18n entries).
index.js Mirrors the same filter()/map() metadata preservation behavior for the CJS build.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread index.mjs Outdated
Comment thread index.mjs Outdated
Comment thread index.js Outdated
Comment thread index.js Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
test/index.spec.mjs (1)

335-343: Avoid hard-coded uniqueName test fixtures.

Line 336 can become stale when data files update, causing flaky failures. Prefer selecting a target from loaded test data.

Suggested patch
-      const targetUniqueName = '/Lotus/Types/Friendly/Pets/ZanukaPets/ZanukaPetParts/ZanukaPetPartHeadB';
       const items = await wrapConstr({ category: ['Pets'], i18n: ['es'] });
+      const targetUniqueName = items[0]?.uniqueName;
+      assert.ok(targetUniqueName, 'should have at least one pet item');
       const filtered = items.filter((i) => i.uniqueName === targetUniqueName);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/index.spec.mjs` around lines 335 - 343, The test uses a hard-coded
uniqueName (targetUniqueName) which can become stale; instead pick a target from
the loaded items returned by wrapConstr so the fixture is stable—e.g., select a
candidate from the items array (like items[0] or items.find(...) ensuring it has
an i18n entry), then use that item's uniqueName as the target, and adjust the
subsequent assertions to reference the dynamically selected uniqueName and
filtered result (functions/locals to edit: wrapConstr, items, filtered,
targetUniqueName).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@index.js`:
- Around line 183-184: The map metadata assignment always sets a.i18n even when
this.i18n is undefined; change it to match the filter behavior by only assigning
a.i18n when the source i18n is present (e.g., check this.i18n !== undefined or
truthiness before setting a.i18n = this.i18n) while leaving a.versions
assignment as-is; update the assignment in the block that references variable a
and this.i18n so the property is not added with an explicit undefined value.

In `@index.mjs`:
- Around line 182-183: The assignment a.i18n = this.i18n sets an explicit
undefined property when i18n is disabled; mirror the guard used in filter by
only assigning a.i18n when this.i18n is present (truthy/non-null), e.g. wrap the
assignment in the same conditional used for filter so that when i18n is off the
property is not created; update the code around the a.i18n assignment (where a
and this.i18n are referenced alongside a.versions) to use that guard.

---

Nitpick comments:
In `@test/index.spec.mjs`:
- Around line 335-343: The test uses a hard-coded uniqueName (targetUniqueName)
which can become stale; instead pick a target from the loaded items returned by
wrapConstr so the fixture is stable—e.g., select a candidate from the items
array (like items[0] or items.find(...) ensuring it has an i18n entry), then use
that item's uniqueName as the target, and adjust the subsequent assertions to
reference the dynamically selected uniqueName and filtered result
(functions/locals to edit: wrapConstr, items, filtered, targetUniqueName).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cf9eb6de-cc46-4367-bb0c-84168cb2390e

📥 Commits

Reviewing files that changed from the base of the PR and between d785728 and 70d1b10.

📒 Files selected for processing (3)
  • index.js
  • index.mjs
  • test/index.spec.mjs

Comment thread index.js Outdated
Comment thread index.mjs Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
index.js (1)

161-169: Align overridden filter/map callback behavior with native Array semantics.

Both overrides currently call callbacks as fn(el) only. Native Array methods pass (element, index, array) and support thisArg for consistency. While current usages in the codebase don't rely on these parameters, implementing full parity would prevent unexpected behavior if callbacks are written expecting standard Array semantics.

Proposed parity refactor
-  filter(fn) {
+  filter(fn, thisArg) {
     const A = [];
     const filteredNames = this.i18n ? new Set() : null;

-    for (const el of this) {
-      if (fn(el)) {
+    for (let i = 0; i < this.length; i++) {
+      const el = this[i];
+      if (fn.call(thisArg, el, i, this)) {
         A.push(el);
         if (filteredNames) filteredNames.add(el.uniqueName);
       }
     }
@@
-  map(fn) {
+  map(fn, thisArg) {
     const a = [];
-    for (const el of this) a.push(fn(el));
+    for (let i = 0; i < this.length; i++) {
+      a.push(fn.call(thisArg, this[i], i, this));
+    }
     if (this.i18n) a.i18n = this.i18n;
     a.versions = this.versions;
     return a;
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@index.js` around lines 161 - 169, The custom Collection.prototype.filter
currently calls the callback as fn(el) which diverges from native Array
semantics; update filter (and the similar map override) to accept an optional
thisArg parameter and invoke the callback with fn.call(thisArg, el, i, this) (or
equivalent) so the callback receives (element, index, array) and honors thisArg;
keep the existing logic that updates filteredNames/adds to A but pass the
current index variable (i) and the collection reference (this) into the callback
invocation to maintain full parity with native Array.filter/Array.map.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@index.js`:
- Around line 161-169: The custom Collection.prototype.filter currently calls
the callback as fn(el) which diverges from native Array semantics; update filter
(and the similar map override) to accept an optional thisArg parameter and
invoke the callback with fn.call(thisArg, el, i, this) (or equivalent) so the
callback receives (element, index, array) and honors thisArg; keep the existing
logic that updates filteredNames/adds to A but pass the current index variable
(i) and the collection reference (this) into the callback invocation to maintain
full parity with native Array.filter/Array.map.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c7f3e5ca-49a3-4bd5-aa02-87b2ff026372

📥 Commits

Reviewing files that changed from the base of the PR and between 70d1b10 and f19af52.

📒 Files selected for processing (3)
  • index.js
  • index.mjs
  • test/index.spec.mjs
✅ Files skipped from review due to trivial changes (1)
  • test/index.spec.mjs
🚧 Files skipped from review as they are similar to previous changes (1)
  • index.mjs

@mateo-leal mateo-leal changed the title fix: preserve i18n and versions in filter and map methods fix: preserve i18n and versions in filter method Apr 10, 2026
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