Commit 56536e96 authored by nanahira's avatar nanahira

add toUint8Array

parent c2edbd5f
import { countItems } from './src/utils';
import { Base64 } from 'js-base64';
import { Base64, fromUint8Array } from 'js-base64';
export default class YGOProDeck {
main: number[] = [];
......@@ -17,7 +17,7 @@ export default class YGOProDeck {
return counted.reduce((a, b) => a + b.size * 4, 0);
}
toEncodedString() {
toUint8Array() {
const counted = [this.main, this.extra, this.side].map(countItems);
const buf = new Uint8Array(counted.reduce((a, b) => a + b.size * 4, 0));
let pointer = 0;
......@@ -38,15 +38,18 @@ export default class YGOProDeck {
}
};
counted.forEach(writeCards);
return Base64.fromUint8Array(buf, true);
return buf;
}
toEncodedString() {
return Base64.fromUint8Array(this.toUint8Array(), true);
}
toString() {
return this.toEncodedString();
}
fromEncodedString(str: string) {
const buf = Base64.toUint8Array(str);
fromUint8Array(buf: Uint8Array) {
for (let i = 0; i < buf.length - 3; i += 4) {
const value =
buf[i] | (buf[i + 1] << 8) | (buf[i + 2] << 16) | (buf[i + 3] << 24);
......@@ -62,6 +65,14 @@ export default class YGOProDeck {
return this;
}
static fromUint8Array(buf: Uint8Array) {
return new YGOProDeck().fromUint8Array(buf);
}
fromEncodedString(str: string) {
return this.fromUint8Array(Base64.toUint8Array(str));
}
static fromEncodedString(str: string) {
return new YGOProDeck().fromEncodedString(str);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment