]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Technique.js
f3daae7d1dfd53453dd1166edaa639f27dcf167d
[alttp.git] / resources / js / helpers / Technique.js
1 import i18n from '../i18n';
2
3 export const getLink = tech => {
4         if (tech.type === 'dungeon') {
5                 return `/dungeons/${tech.name}`;
6         }
7         if (tech.type === 'location') {
8                 return `/locations/${tech.name}`;
9         }
10         if (tech.type === 'mode') {
11                 return `/modes/${tech.name}`;
12         }
13         if (tech.type === 'ruleset') {
14                 return `/rulesets/${tech.name}`;
15         }
16         return `/tech/${tech.name}`;
17 };
18
19 export const getRelations = (tech, type) => {
20         const rs = (tech && tech.relations) || [];
21         return type ? rs.filter(r => r && r.pivot && r.pivot.type === type) : rs;
22 };
23
24 export const hasRelations = (tech, type) => getRelations(tech, type).length > 0;
25
26 export const getLanguages = tech => ['en', ...tech.translations.map(t => t.locale)];
27
28 export const getMatchedLocale = (tech, lang) => {
29         const direct = tech.translations.find(t => t.locale === lang);
30         if (direct) {
31                 return direct.locale;
32         }
33         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
34         if (sameLang) {
35                 return sameLang.locale;
36         }
37         return 'en';
38 };
39
40 export const getTranslation = (tech, prop, lang) => {
41         const direct = tech.translations.find(t => t.locale === lang);
42         if (direct) {
43                 return direct[prop];
44         }
45         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
46         if (sameLang) {
47                 return sameLang[prop];
48         }
49         return tech[prop];
50 };
51
52 export const compareTranslation = (prop, lang) => (a, b) =>
53         getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
54
55 export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
56
57 export default {
58         compareTranslation,
59         getLanguages,
60         getRelations,
61         getTranslation,
62         hasRelations,
63         sorted,
64 };