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

Commit

Permalink
Adding base64 encoding from web-editor and removed old encoding (#339)
Browse files Browse the repository at this point in the history
* Adding base64 encoding from web-editor and removed old encoding

* Adding base64 to main exports
  • Loading branch information
rorticus authored Jun 16, 2017
1 parent f777ae6 commit f4f129e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 594 deletions.
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');
};

/**
* 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

0 comments on commit f4f129e

Please sign in to comment.