]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Technique.js
tech relations
[alttp.git] / resources / js / helpers / Technique.js
1 import i18n from '../i18n';
2
3 export const getRelations = (tech, type) => {
4         const rs = (tech && tech.relations) || [];
5         return type ? rs.filter(r => r && r.pivot && r.pivot.type === type) : rs;
6 };
7
8 export const hasRelations = (tech, type) => getRelations(tech, type).length > 0;
9
10 export const getTranslation = (tech, prop, lang) => {
11         const direct = tech.translations.find(t => t.locale === lang);
12         if (direct) {
13                 return direct[prop];
14         }
15         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
16         if (sameLang) {
17                 return sameLang[prop];
18         }
19         return tech[prop];
20 };
21
22 export const compareTranslation = (prop, lang) => (a, b) =>
23         getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
24
25 export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
26
27 export default {
28         compareTranslation,
29         getRelations,
30         getTranslation,
31         hasRelations,
32         sorted,
33 };