-
Notifications
You must be signed in to change notification settings - Fork 4
/
function.js
48 lines (43 loc) · 1.56 KB
/
function.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
46
47
48
const hashStringToNumber = (string = '', hash = 0) => {
for (let index = 0, indexMax = string.length; index < indexMax; index++) {
// Convert to 32bit integer and drop the sign bit (for +/-), so result range will be: [0, 2^32-1]
hash = ((hash << 5) - hash + string.charCodeAt(index)) >>> 0
}
return hash
}
const reverseString = (string) => [ ...string ].reverse().join('')
const swapObfuscateString = (string = '') => {
const stringLength = string.length
const stringEndIndex = stringLength - 1
const indexMax = Math.floor(stringLength * 0.5)
const result = []
if (stringLength % 2) result[ indexMax ] = string.charAt(indexMax)
for (let index = 0; index < indexMax; index++) {
const pickIndex = (index % 2) ? index : stringEndIndex - index
result[ index ] = string.charAt(pickIndex)
result[ stringEndIndex - index ] = string.charAt(stringEndIndex - pickIndex)
}
return result.join('')
}
// always return a object/array, use this with object destructuring
const tryParseJSONObject = (text, defaultResult = {}) => {
try {
const result = JSON.parse(text)
if (typeof (result) === 'object' && result !== null) return result
} catch (error) { __DEV__ && console.log('[tryParseJSONObject] error', error) }
return defaultResult
}
const getValueByKeyList = (value, keyList) => {
for (const key of keyList) {
if (value && Object.prototype.hasOwnProperty.call(value, key)) value = value[ key ]
else return
}
return value
}
export {
hashStringToNumber,
reverseString,
swapObfuscateString,
tryParseJSONObject,
getValueByKeyList
}