-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.js
29 lines (23 loc) · 959 Bytes
/
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
module.exports = {
// Ex: keep only 'fr' in 'fr_FR'
shortLang: language => language.toLowerCase().split(/[^a-z]/)[0],
// a polymorphism helper:
// accept either a string or an array and return an array
forceArray: array => {
if (typeof array === 'string') array = [ array ]
return array || []
},
// simplistic implementation to filter-out arrays
isPlainObject: obj => {
if (!obj || typeof obj !== 'object' || obj instanceof Array) return false
return true
},
// encodeURIComponent ignores !, ', (, ), and *
// cf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Description
fixedEncodeURIComponent: str => {
return encodeURIComponent(str).replace(/[!'()*]/g, encodeCharacter)
},
replaceSpaceByUnderscores: str => str.replace(/\s/g, '_'),
uniq: array => Array.from(new Set(array))
}
const encodeCharacter = char => '%' + char.charCodeAt(0).toString(16)