Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ module.exports = class Items extends Array {
for (const el of this) {
if (fn(el)) A.push(el);
}
if (this.i18n) {
const filteredNames = new Set(A.map((el) => el.uniqueName));
A.i18n = Object.fromEntries(Object.entries(this.i18n).filter(([key]) => filteredNames.has(key)));
}
Comment thread
mateo-leal marked this conversation as resolved.
Outdated
A.versions = this.versions;
return A;
}

Expand All @@ -175,7 +180,8 @@ module.exports = class Items extends Array {
map(fn) {
const a = [];
for (const el of this) a.push(fn(el));

a.i18n = this.i18n;
Comment thread
mateo-leal marked this conversation as resolved.
Outdated
a.versions = this.versions;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return a;
}
};
8 changes: 7 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ export default class Items extends Array {
for (const el of this) {
if (fn(el)) A.push(el);
}
if (this.i18n) {
const filteredNames = new Set(A.map((el) => el.uniqueName));
A.i18n = Object.fromEntries(Object.entries(this.i18n).filter(([key]) => filteredNames.has(key)));
}
Comment thread
mateo-leal marked this conversation as resolved.
Outdated
A.versions = this.versions;
return A;
}

Expand All @@ -174,7 +179,8 @@ export default class Items extends Array {
map(fn) {
const a = [];
for (const el of this) a.push(fn(el));

a.i18n = this.i18n;
Comment thread
mateo-leal marked this conversation as resolved.
Outdated
a.versions = this.versions;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return a;
}
}
19 changes: 19 additions & 0 deletions test/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ const test = (base) => {
const realLength = items.length;
assert(realLength === items.map((x) => x).length);
});
it('should preserve i18n and versions after filter and map', async () => {
const items = await wrapConstr({ category: ['Pets'], i18n: ['es'] });
const filtered = items.filter(() => true);
assert.deepStrictEqual(filtered.i18n, items.i18n, 'filter should preserve i18n');
assert.deepStrictEqual(filtered.versions, items.versions, 'filter should preserve versions');

const mapped = items.map((x) => x);
assert.deepStrictEqual(mapped.i18n, items.i18n, 'map should preserve i18n');
assert.deepStrictEqual(mapped.versions, items.versions, 'map should preserve versions');
});
it('should filter i18n entries to only matched items', async () => {
const targetUniqueName = '/Lotus/Types/Friendly/Pets/ZanukaPets/ZanukaPetParts/ZanukaPetPartHeadB';
const items = await wrapConstr({ category: ['Pets'], i18n: ['es'] });
const filtered = items.filter((i) => i.uniqueName === targetUniqueName);
assert.strictEqual(filtered.length, 1, 'should have exactly one result');
assert.ok(filtered.i18n, 'filtered result should have i18n');
assert.ok(filtered.i18n[targetUniqueName], 'i18n should contain the matched item');
assert.strictEqual(Object.keys(filtered.i18n).length, 1, 'i18n should only contain the matched item');
});
describe('helminth', async () => {
it('should only have drops', async () => {
const items = await wrapConstr({ category: ['Warframes'] });
Expand Down
Loading