Skip to content

Commit

Permalink
Export FormatType
Browse files Browse the repository at this point in the history
Closes #3.
  • Loading branch information
beheh committed Jul 13, 2018
1 parent dcfecd6 commit 3f14f75
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ $ yarn add deckstrings
## Usage

```javascript
import {encode, decode} from "deckstrings";
import { encode, decode, FormatType } from "deckstrings";

const deck = {
cards: [[1, 2], [2, 2], [3, 2], [4, 1]], // [dbfid, count] pairs
heroes: [7], // Garrosh Hellscream
format: 1, // 1 for Wild, 2 for Standard
format: FormatType.FT_WILD, // or 1 for Wild, 2 for Standard
};

const deckstring = encode(deck);
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FormatType as FormatTypeType } from "../types";

export const DECKSTRING_VERSION = 1;

export const FormatType: { [key: string]: FormatTypeType } = {
FT_WILD: 1,
FT_STANDARD: 2,
};
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BufferReader, BufferWriter } from "./buffer";
import { DeckDefinition, DeckList } from "../types";

const DECKSTRING_VERSION = 1;
import { DECKSTRING_VERSION, FormatType } from "./constants";

function verifyDbfId(id: any, name?: string): void {
name = name ? name : "dbf id";
Expand Down Expand Up @@ -54,7 +53,8 @@ function trisort_cards(cards: DeckList): any {
export function encode(deck: DeckDefinition): string {
if (
typeof deck !== "object" ||
(deck.format !== 1 && deck.format !== 2) ||
(deck.format !== FormatType.FT_WILD &&
deck.format !== FormatType.FT_STANDARD) ||
!Array.isArray(deck.heroes) ||
!Array.isArray(deck.cards)
) {
Expand Down Expand Up @@ -103,7 +103,7 @@ export function decode(deckstring: string): DeckDefinition {
}

const format = reader.nextVarint();
if (format !== 1 && format !== 2) {
if (format !== FormatType.FT_WILD && format !== FormatType.FT_STANDARD) {
throw new Error(`Unsupported format ${format} in deckstring`);
}

Expand All @@ -130,3 +130,5 @@ export function decode(deckstring: string): DeckDefinition {
format,
};
}

export { FormatType };
6 changes: 3 additions & 3 deletions test/DeckstringsTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*#if _PLATFORM === "browser"
import { decode, encode } from "../dist/browser.mjs";
import { decode, encode, FormatType } from "../dist/browser.mjs";
//#else */
const { decode, encode } = require("../dist/index");
const { decode, encode, FormatType } = require("../dist/index");
const { expect } = require("chai");
//#endif

Expand Down Expand Up @@ -33,7 +33,7 @@ const CANONICAL_DEFINITION = {
[1662, 2], // Eaglehorn Bow
], // pairs of [dbfid, count], by ascending dbfId
heroes: [31], // Rexxar
format: 2, // 1 for Wild, 2 for Standard
format: FormatType.FT_STANDARD, // 1 for Wild, 2 for Standard
};

const NONCANONICAL_DEFINITION = Object.assign({}, CANONICAL_DEFINITION, {
Expand Down
4 changes: 3 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export type FormatType = 1 | 2;

export type DeckList = [number, number][]; // [dbfId, count]

export interface DeckDefinition {
cards: DeckList;
heroes: number[];
format: 1 | 2;
format: FormatType;
}

export function encode(deck: DeckDefinition): string;
Expand Down

0 comments on commit 3f14f75

Please sign in to comment.