]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/Technique.js
basic content editing
[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         if (!tech) return '';
42         const direct = tech.translations.find(t => t.locale === lang);
43         if (direct) {
44                 return direct[prop];
45         }
46         const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
47         if (sameLang) {
48                 return sameLang[prop];
49         }
50         return tech[prop];
51 };
52
53 export const compareTranslation = (prop, lang) => (a, b) =>
54         getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
55
56 export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
57
58 export default {
59         compareTranslation,
60         getLanguages,
61         getRelations,
62         getTranslation,
63         hasRelations,
64         sorted,
65 };