diff --git a/lib/utils/common.js b/lib/utils/common.js index 17f9d42..408f154 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -28,6 +28,22 @@ utils.common = { quote: function (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) { let tmp = {} let r = [] diff --git a/package.json b/package.json index 2f8cc9f..608f610 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "grunt test --verbose" }, "standard": { - "globals": ["random", "make", "utils", "logger", "MersenneTwister", "o"], + "globals": ["random", "make", "utils", "logger", "MersenneTwister", "o", "btoa", "atob"], "envs": ["browser"] } }