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