From 3f14f7519b82511f4075ec1d06e3ca1690ebfb78 Mon Sep 17 00:00:00 2001 From: Benedict Etzel Date: Fri, 13 Jul 2018 11:26:05 +0200 Subject: [PATCH] Export FormatType Closes #3. --- README.md | 4 ++-- src/constants.ts | 8 ++++++++ src/index.ts | 10 ++++++---- test/DeckstringsTest.js | 6 +++--- types.d.ts | 4 +++- 5 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 src/constants.ts diff --git a/README.md b/README.md index 3b3b4be..5151b17 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..cd7cb78 --- /dev/null +++ b/src/constants.ts @@ -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, +}; diff --git a/src/index.ts b/src/index.ts index 3fac2f7..fa4470d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -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) ) { @@ -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`); } @@ -130,3 +130,5 @@ export function decode(deckstring: string): DeckDefinition { format, }; } + +export { FormatType }; diff --git a/test/DeckstringsTest.js b/test/DeckstringsTest.js index 8dda490..da78b4b 100644 --- a/test/DeckstringsTest.js +++ b/test/DeckstringsTest.js @@ -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 @@ -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, { diff --git a/types.d.ts b/types.d.ts index 439aa36..1c3d171 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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;