[firestore][js] blob uint8 coversion + blob comparator

This commit is contained in:
Salakar 2018-05-02 10:05:30 +01:00
parent ff3b0fd7ea
commit d180b40d19
1 changed files with 18 additions and 9 deletions

View File

@ -3,6 +3,9 @@ import Base64 from './utils/Base64';
type BlobFormat = 'string' | 'array';
export default class Blob {
_data: Uint8Array | string;
_type: BlobFormat;
constructor(data, type: BlobFormat) {
this._data = data;
this._type = type;
@ -14,7 +17,6 @@ export default class Blob {
* @param base64 string
*/
static fromBase64String(base64: string): Blob {
// TODO convert to Uint8Array?
return new Blob(base64, 'string');
}
@ -23,7 +25,7 @@ export default class Blob {
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#.fromUint8Array
* @param array Array
*/
static fromUint8Array(array: Array): Blob {
static fromUint8Array(array: Uint8Array): Blob {
return new Blob(array, 'array');
}
@ -34,9 +36,15 @@ export default class Blob {
* @returns boolean 'true' if this Blob is equal to the provided one.
*/
isEqual(blob: Blob): boolean {
// TODO comparison checks
console.log(blob);
return true;
let thisBlobBase64 = '';
if (this._type === 'string') thisBlobBase64 = this._data;
else thisBlobBase64 = this.toBase64();
let thatBlobBase64 = '';
if (blob._type === 'string') thatBlobBase64 = blob._data;
else thatBlobBase64 = blob.toBase64();
return thisBlobBase64 === thatBlobBase64;
}
/**
@ -63,9 +71,10 @@ export default class Blob {
*/
toUint8Array(): Uint8Array {
if (this._type === 'array') return this._data;
// TODO conversion
// TODO conversion
// TODO conversion
return new Uint8Array();
return new Uint8Array(
Base64.atob(this._data)
.split('')
.map(c => c.charCodeAt(0))
);
}
}