-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
utils.js
45 lines (43 loc) · 1.43 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { latinLangs, getShopLang } from "/lib/api/helpers";
// dynamic import of slugiy/transliteration.slugify
let slugify;
async function lazyLoadSlugify() {
let mod;
// getting the shops base language
const lang = getShopLang();
// if slugify has been loaded but the language has changed
// to be a non latin based language then load transliteration
if (slugify && slugify.name === "replace" && latinLangs.indexOf(lang) === -1) {
mod = await import("transliteration");
} else if (slugify) {
// if slugify/transliteration is loaded and no lang change
return;
} else if (latinLangs.indexOf(lang) >= 0) {
// if the shops language use latin based chars load slugify else load transliterations's slugify
mod = await import("slugify");
} else {
mod = await import("transliteration");
}
// slugify is exported to modules.default while transliteration is exported to modules.slugify
slugify = mod.default || mod.slugify;
}
/**
* @name getSlug
* @summary Return a slugified string using "slugify" from transliteration
* @see https://www.npmjs.com/package/transliteration
* @method
* @memberof Helpers
* @locus Server
* @param {String} slugString - string to slugify
* @return {String} slugified string
*/
export function getSlug(slugString) {
let slug;
Promise.await(lazyLoadSlugify());
if (slugString && slugify) {
slug = slugify(slugString.toLowerCase());
} else {
slug = "";
}
return slug;
}