Add unicode safe, base64 encode/decode

This commit is contained in:
pyoor 2017-06-08 12:05:06 -04:00
parent 3ad70863f1
commit 2cf47dba1d
2 changed files with 17 additions and 1 deletions

View file

@ -28,6 +28,22 @@ utils.common = {
quote: function (obj) { quote: function (obj) {
return JSON.stringify(obj) return JSON.stringify(obj)
}, },
b64encode: function (str) {
// Unicode safe b64 encoding
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes (match, p1) {
// noinspection JSCheckFunctionSignatures
return String.fromCharCode('0x' + p1)
}))
},
b64decode: function (str) {
// Unicode safe b64 decoding
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
},
uniqueList: function (list) { uniqueList: function (list) {
let tmp = {} let tmp = {}
let r = [] let r = []

View file

@ -17,7 +17,7 @@
"test": "grunt test --verbose" "test": "grunt test --verbose"
}, },
"standard": { "standard": {
"globals": ["random", "make", "utils", "logger", "MersenneTwister", "o"], "globals": ["random", "make", "utils", "logger", "MersenneTwister", "o", "btoa", "atob"],
"envs": ["browser"] "envs": ["browser"]
} }
} }