-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
func: added save string base64 unicode encoding
Closes #1
- Loading branch information
Showing
2 changed files
with
25 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Encode unicode string to base64 | ||
* @param str string to be encoded | ||
* @returns encoded string | ||
*/ | ||
export function b64EncodeUnicode(str: string) { | ||
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { | ||
return String.fromCharCode(parseInt(p1, 16)) | ||
})) | ||
} | ||
|
||
/** | ||
* Decode unicode base64 string | ||
* @param str base64 string to be decoded | ||
* @returns decoded string | ||
*/ | ||
export function b64DecodeUnicode(str: string) { | ||
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) { | ||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) | ||
}).join('')) | ||
} |