]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Technique.js
more alternate/canonical links
[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 getMatchedLocale = (tech, lang) => {
23         const direct = tech.translations.find(t => t.locale === lang);
24         if (direct) {
25                 return direct.locale;
26         }
27         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
28         if (sameLang) {
29                 return sameLang.locale;
30         }
31         return 'en';
32 };
33
34 export const getTranslation = (tech, prop, lang) => {
35         const direct = tech.translations.find(t => t.locale === lang);
36         if (direct) {
37                 return direct[prop];
38         }
39         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
40         if (sameLang) {
41                 return sameLang[prop];
42         }
43         return tech[prop];
44 };
45
46 export const compareTranslation = (prop, lang) => (a, b) =>
47         getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
48
49 export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
50
51 export default {
52         compareTranslation,
53         getLanguages,
54         getRelations,
55         getTranslation,
56         hasRelations,
57         sorted,
58 };