This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding base64 encoding from web-editor and removed old encoding (#339)
* Adding base64 encoding from web-editor and removed old encoding * Adding base64 to main exports
- Loading branch information
Showing
6 changed files
with
57 additions
and
594 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import global from './global'; | ||
import has, { add as hasAdd } from '@dojo/has/has'; | ||
|
||
hasAdd('btoa', 'btoa' in global); | ||
hasAdd('atob', 'atob' in global); | ||
|
||
/** | ||
* Take a string encoded in base64 and decode it | ||
* @param encodedString The base64 encoded string | ||
*/ | ||
export const decode: (encodedString: string) => string = has('atob') ? function (encodedString: string) { | ||
/* this allows for utf8 characters to be decoded properly */ | ||
return decodeURIComponent(Array.prototype.map.call(atob(encodedString), (char: string) => '%' + ('00' + char.charCodeAt(0).toString(16)).slice(-2)).join('')); | ||
} : function (encodedString: string): string { | ||
return new Buffer(encodedString.toString(), 'base64').toString('utf8'); | ||
}; | ||
|
||
/** | ||
* Take a string and encode it to base64 | ||
* @param rawString The string to encode | ||
*/ | ||
export const encode: (rawString: string) => string = has('btoa') ? function (decodedString: string) { | ||
/* this allows for utf8 characters to be encoded properly */ | ||
return btoa(encodeURIComponent(decodedString).replace(/%([0-9A-F]{2})/g, (match, code: string) => String.fromCharCode(Number('0x' + code)))); | ||
} : function (rawString: string): string { | ||
return new Buffer(rawString.toString(), 'utf8').toString('base64'); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.