]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Technique.js
add language references to tech pages
[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 getLanguages = tech => ['en', ...tech.translations.map(t => t.locale)];
21
22 export const getTranslation = (tech, prop, lang) => {
23         const direct = tech.translations.find(t => t.locale === lang);
24         if (direct) {
25                 return direct[prop];
26         }
27         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
28         if (sameLang) {
29                 return sameLang[prop];
30         }
31         return tech[prop];
32 };
33
34 export const compareTranslation = (prop, lang) => (a, b) =>
35         getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
36
37 export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
38
39 export default {
40         compareTranslation,
41         getLanguages,
42         getRelations,
43         getTranslation,
44         hasRelations,
45         sorted,
46 };