Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,26 @@ module.exports = class Items extends Array {
*/
filter(fn) {
const A = [];
const filteredNames = this.i18n ? new Set() : null;

for (const el of this) {
if (fn(el)) A.push(el);
if (fn(el)) {
A.push(el);
if (filteredNames) filteredNames.add(el.uniqueName);
}
}

if (filteredNames) {
const filteredI18n = {};
for (const uniqueName of filteredNames) {
if (Object.prototype.hasOwnProperty.call(this.i18n, uniqueName)) {
filteredI18n[uniqueName] = this.i18n[uniqueName];
}
}
A.i18n = filteredI18n;
}

A.versions = this.versions;
return A;
}

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

if (this.i18n) a.i18n = this.i18n;
a.versions = this.versions;
return a;
}
};
23 changes: 20 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,39 @@ export default class Items extends Array {
*/
filter(fn) {
const A = [];
const filteredNames = this.i18n ? new Set() : null;

for (const el of this) {
if (fn(el)) A.push(el);
if (fn(el)) {
A.push(el);
if (filteredNames) filteredNames.add(el.uniqueName);
}
}

if (filteredNames) {
const filteredI18n = {};
for (const uniqueName of filteredNames) {
if (Object.prototype.hasOwnProperty.call(this.i18n, uniqueName)) {
filteredI18n[uniqueName] = this.i18n[uniqueName];
}
}
A.i18n = filteredI18n;
}

A.versions = this.versions;
return A;
}

/**
* @Override Array.prototype.filter
* @Override Array.prototype.map
*
* See filter override
*/
map(fn) {
const a = [];
for (const el of this) a.push(fn(el));

if (this.i18n) a.i18n = this.i18n;
a.versions = this.versions;
return a;
}
}
28 changes: 28 additions & 0 deletions test/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,34 @@ 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 items = await wrapConstr({ category: ['Pets'], i18n: ['es'] });
const targetUniqueName = items[0]?.uniqueName;
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');
});
it('edge case: should ignore i18n entry when is missing from original', async () => {
const items = await wrapConstr({ category: ['Pets'], i18n: ['es'] });
const targetUniqueName = items[0]?.uniqueName;
items.i18n = {}; // simulate missing i18n entries
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.strictEqual(Object.keys(filtered.i18n).length, 0, 'i18n should be empty when original is empty');
});
describe('helminth', async () => {
it('should only have drops', async () => {
const items = await wrapConstr({ category: ['Warframes'] });
Expand Down