Skip to content

Commit

Permalink
reorder funtions, add return type to toUint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
dankogai committed Sep 12, 2020
1 parent ddc76ee commit 7cec8b7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
10 changes: 5 additions & 5 deletions base64.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ declare const atobPolyfill: (asc: string) => string;
* @returns {string} binary string
*/
declare const _atob: (asc: string) => string;
/**
* converts a Base64 string to a Uint8Array.
*/
declare const toUint8Array: (a: string) => Uint8Array;
/**
* converts a Base64 string to a UTF-8 string.
* @param {String} src Base64 string. Both normal and URL-safe are supported
* @returns {string} UTF-8 string
*/
declare const decode: (src: string) => string;
/**
* converts a Base64 string to a Uint8Array.
*/
declare const toUint8Array: (a: string) => any;
/**
* extend String.prototype with relevant methods
*/
Expand Down Expand Up @@ -101,7 +101,7 @@ declare const gBase64: {
btou: (b: string) => string;
decode: (src: string) => string;
fromUint8Array: (u8a: Uint8Array, urlsafe?: boolean) => string;
toUint8Array: (a: string) => any;
toUint8Array: (a: string) => Uint8Array;
extendString: () => void;
extendUint8Array: () => void;
extendBuiltins: () => void;
Expand Down
20 changes: 11 additions & 9 deletions base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ const utob = (u) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s) => Buffer.from(s, 'utf8').toString('base64')
: _TE ? (s) => _fromUint8Array(_TE.encode(s))
: _TE
? (s) => _fromUint8Array(_TE.encode(s))
: (s) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
Expand Down Expand Up @@ -216,6 +217,15 @@ const atobPolyfill = (asc) => {
const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
: _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
: atobPolyfill;
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _decode = _hasBuffer
? (a) => Buffer.from(a, 'base64').toString('utf8')
: _TD
Expand All @@ -229,14 +239,6 @@ const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')
*/
const decode = (src) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down
20 changes: 11 additions & 9 deletions base64.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ const utob = (u) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s) => Buffer.from(s, 'utf8').toString('base64')
: _TE ? (s) => _fromUint8Array(_TE.encode(s))
: _TE
? (s) => _fromUint8Array(_TE.encode(s))
: (s) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
Expand Down Expand Up @@ -185,6 +186,15 @@ const atobPolyfill = (asc) => {
const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
: _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
: atobPolyfill;
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _decode = _hasBuffer
? (a) => Buffer.from(a, 'base64').toString('utf8')
: _TD
Expand All @@ -198,14 +208,6 @@ const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')
*/
const decode = (src) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down
20 changes: 11 additions & 9 deletions base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const utob = (u: string) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s: string) => Buffer.from(s, 'utf8').toString('base64')
: _TE ? (s: string) => _fromUint8Array(_TE.encode(s))
: _TE
? (s: string) => _fromUint8Array(_TE.encode(s))
: (s: string) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
Expand Down Expand Up @@ -192,6 +193,15 @@ const atobPolyfill = (asc: string) => {
const _atob = _hasatob ? (asc: string) => atob(_tidyB64(asc))
: _hasBuffer ? (asc: string) => Buffer.from(asc, 'base64').toString('binary')
: atobPolyfill;
//
const _toUint8Array = _hasBuffer
? (a: string) => _U8Afrom(Buffer.from(a, 'base64'))
: (a: string) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a: string): Uint8Array => _toUint8Array(_unURI(a));
//
const _decode = _hasBuffer
? (a: string) => Buffer.from(a, 'base64').toString('utf8')
: _TD
Expand All @@ -206,14 +216,6 @@ const _unURI = (a: string) =>
*/
const decode = (src: string) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a: string) => _U8Afrom(Buffer.from(a, 'base64'))
: (a: string) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a: string) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down

0 comments on commit 7cec8b7

Please sign in to comment.