react-native-firebase/lib/modules/firestore/Blob.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

import Base64 from './../../utils/Base64';
2018-05-01 21:20:55 +00:00
export default class Blob {
2018-05-03 19:13:51 +00:00
_binaryString: string;
constructor(binaryString: string) {
2018-05-03 19:13:51 +00:00
this._binaryString = binaryString;
2018-05-01 21:20:55 +00:00
}
/**
* Creates a new Blob from the given Base64 string
2018-05-03 19:13:51 +00:00
*
2018-05-01 21:20:55 +00:00
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#.fromBase64String
* @param base64 string
*/
static fromBase64String(base64: string): Blob {
if (typeof base64 !== 'string' || base64.length < 1) {
throw new Error(
'firestore.Blob.fromBase64String expects a string of at least 1 character in length'
);
}
2018-05-03 19:13:51 +00:00
return new Blob(Base64.atob(base64));
2018-05-01 21:20:55 +00:00
}
/**
* Creates a new Blob from the given Uint8Array.
2018-05-03 19:13:51 +00:00
*
2018-05-01 21:20:55 +00:00
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#.fromUint8Array
* @param array Array
*/
static fromUint8Array(array: Uint8Array): Blob {
2018-05-03 19:13:51 +00:00
if (!(array instanceof Uint8Array)) {
throw new Error(
'firestore.Blob.fromUint8Array expects an instance of Uint8Array'
);
}
return new Blob(
Array.prototype.map
.call(array, (char: number) => String.fromCharCode(char))
.join('')
);
2018-05-01 21:20:55 +00:00
}
/**
* Returns 'true' if this Blob is equal to the provided one.
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#isEqual
* @param {*} blob Blob The Blob to compare against. Value must not be null.
* @returns boolean 'true' if this Blob is equal to the provided one.
*/
isEqual(blob: Blob): boolean {
2018-05-03 19:13:51 +00:00
if (!(blob instanceof Blob)) {
throw new Error('firestore.Blob.isEqual expects an instance of Blob');
}
2018-05-03 19:13:51 +00:00
return this._binaryString === blob._binaryString;
2018-05-01 21:20:55 +00:00
}
/**
* Returns the bytes of a Blob as a Base64-encoded string.
2018-05-03 19:13:51 +00:00
*
2018-05-01 21:20:55 +00:00
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#toBase64
* @returns string The Base64-encoded string created from the Blob object.
*/
toBase64(): string {
2018-05-03 19:13:51 +00:00
return Base64.btoa(this._binaryString);
2018-05-01 21:20:55 +00:00
}
/**
* Returns the bytes of a Blob in a new Uint8Array.
2018-05-03 19:13:51 +00:00
*
2018-05-01 21:20:55 +00:00
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#toUint8Array
* @returns non-null Uint8Array The Uint8Array created from the Blob object.
*/
toUint8Array(): Uint8Array {
return new Uint8Array(
2018-05-03 19:13:51 +00:00
this._binaryString.split('').map(c => c.charCodeAt(0))
);
2018-05-01 21:20:55 +00:00
}
2018-05-03 19:13:51 +00:00
/**
* Returns a string representation of this blob instance
*
* @returns {string}
* @memberof Blob
*/
toString(): string {
return `firestore.Blob(base64: ${this.toBase64()})`;
}
2018-05-01 21:20:55 +00:00
}