Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Adding base64 encoding from web-editor and removed old encoding #339

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/base64.ts
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');
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially we are assuming that all supported browsers support btob/atob, since the alternative is the Node-specific Buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mwistrand yeah, and I think we are good on that account, https://caniuse.com/#search=atob . Supported back to IE 10.


/**
* 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');
};
335 changes: 0 additions & 335 deletions src/encoding.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as aspect from './aspect';
import DateObject from './DateObject';
import * as encoding from './encoding';
import Evented from './Evented';
import global from './global';
import IdentityRegistry from './IdentityRegistry';
import * as lang from './lang';
import * as base64 from './base64';
import load from './load';
import MatchRegistry from './MatchRegistry';
import on, { emit } from './on';
Expand All @@ -29,9 +29,9 @@ const async = {
export {
aspect,
async,
base64,
DateObject,
emit,
encoding,
Evented,
global,
IdentityRegistry,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import './async/iteration';
import './async/ExtensiblePromise';
import './async/Task';
import './async/timing';
import './base64';
import './Destroyable';
import './Evented';
import './encoding';
import './global';
import './IdentityRegistry';
import './instrument';
Expand Down
Loading