Skip to content

Commit

Permalink
perf: reduce i18n by merging with i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerOrnstein committed Sep 26, 2024
1 parent 522f3b4 commit 16881a2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
13 changes: 7 additions & 6 deletions src/LoadOutInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export default class LoadOutInventory {
/**
*
* @param {Object} item The loadout data
* @param {string} [locale='en'] The locale to return loudout items in
*/
constructor(item) {
constructor(item, locale = 'en') {
/**
* Skins applied to weapons
* @type {WeaponSkin}
Expand All @@ -22,30 +23,30 @@ export default class LoadOutInventory {
* An array of the player's currently equiped Warframe (or powersuits)
* @type {LoadOutItem}
*/
this.suits = item.Suits.map((s) => new LoadOutItem(s));
this.suits = item.Suits.map((s) => new LoadOutItem(s, locale));

/**
* An array of the player's currently equiped secondary weapon
* @type {LoadOutItem | undefined}
*/
this.secondary = item.Pistols?.map((p) => new LoadOutItem(p));
this.secondary = item.Pistols?.map((p) => new LoadOutItem(p, locale));

/**
* An array of the player's currently equiped primary weapon
* @type {LoadOutItem | undefined}
*/
this.primary = item.LongGuns?.map((lg) => new LoadOutItem(lg));
this.primary = item.LongGuns?.map((lg) => new LoadOutItem(lg, locale));

/**
* An array of the player's currently equiped melee weapon
* @type {LoadOutItem | undefined}
*/
this.melee = item.Melee?.map((m) => new LoadOutItem(m));
this.melee = item.Melee?.map((m) => new LoadOutItem(m, locale));

/**
* Items that have counted towards the players mastery rank
* @type {XpInfo}
*/
this.xpInfo = item.XPInfo.map((xp) => new XpInfo(xp));
this.xpInfo = item.XPInfo.map((xp) => new XpInfo(xp, locale));
}
}
8 changes: 5 additions & 3 deletions src/LoadOutItem.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { colors, find } from 'warframe-items/utilities';
import { colors } from 'warframe-items/utilities';
import { parseDate, toTitleCase } from 'warframe-worldstate-data/utilities';

import ItemConfig from './ItemConfig.js';
import Polarity from './Polarity.js';
import find from './Utils.js';

/**
* An an item in LoadOutInventory
Expand All @@ -12,8 +13,9 @@ export default class LoadOutItem {
/**
*
* @param {Object} weapon The loadout item from LoadoutInventory
* @param {string} [locale='en'] The locale to return item in
*/
constructor(weapon) {
constructor(weapon, locale = 'en') {
/**
* Item ID
* @type {String}
Expand All @@ -26,7 +28,7 @@ export default class LoadOutItem {
*/
this.uniqueName = weapon.ItemType;

const item = find.findItem(weapon.ItemType);
const item = find(weapon.ItemType, locale);
if (item) {
/**
* Item in-game name
Expand Down
2 changes: 1 addition & 1 deletion src/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Profile {
* Current loadout
* @type {LoadOutInventory}
*/
this.loadout = new LoadOutInventory(profile.LoadOutInventory);
this.loadout = new LoadOutInventory(profile.LoadOutInventory, locale);

/**
* Railjack and drifter Intrinsics
Expand Down
7 changes: 4 additions & 3 deletions src/Skin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find } from 'warframe-items/utilities';
import find from './Utils.js';

/**
* A skin class
Expand All @@ -8,15 +8,16 @@ export default class Skin {
/**
*
* @param {Object} skin The skin data to parse
* @param {string} [locale='en'] The locale to return skin item in
*/
constructor(skin) {
constructor(skin, locale = 'en') {
/**
* Unique name
* @type {String}
*/
this.uniqueName = skin.ItemType;

const item = find.findItem(skin.ItemType);
const item = find(skin.ItemType, locale);
/**
* The Warframe item that matches the unique name
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Items from 'warframe-items';

const find = (name, locale = 'en') => {
const items = new Items({
category: ['Skins', 'Primary', 'Secondary', 'Melee', 'Arch-Melee', 'Arch-Gun', 'Warframes', 'Archwing'],
i18n: locale,
i18nOnObject: true,
});

const item = items.find((i) => i.uniqueName === name);

let itemClone = { ...item };
if (locale !== 'en' && itemClone.i18n[locale] && itemClone.i18n[locale]) {
itemClone = { ...itemClone, ...itemClone.i18n[locale] };

if (itemClone.abilities) {
itemClone.abilities = itemClone.abilities.map((ability) => ({
uniqueName: ability.abilityUniqueName || ability.uniqueName || undefined,
name: ability.abilityName || ability.name,
description: ability.abilityDescription || ability.description,
imageName: ability.imageName ?? undefined,
}));
}

delete itemClone.i18n;
return itemClone;
}

return item;
};

export default find;
7 changes: 4 additions & 3 deletions src/XpInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find } from 'warframe-items/utilities';
import find from './Utils.js';

/**
* An item that has contributed to a player's mastery rank
Expand All @@ -8,8 +8,9 @@ export default class XpInfo {
/**
*
* @param {Object} info The info for a given ranked item
* @param {string} locale langauge to return item in
*/
constructor(info) {
constructor(info, locale = 'en') {
/**
* Unique name
* @type {String}
Expand All @@ -26,6 +27,6 @@ export default class XpInfo {
* The item corrosponding to the unique name.
* @type {module:"warframe-items".Item | undefined}
*/
this.item = find.findItem(info.ItemType);
this.item = find(info.ItemType, locale);
}
}

0 comments on commit 16881a2

Please sign in to comment.