From 3489b95ba8317352bd526cb218a166d986f12e5a Mon Sep 17 00:00:00 2001 From: Matt Huggins Date: Fri, 20 Oct 2023 17:01:47 -0500 Subject: [PATCH] feat: export parseSite function --- README.md | 18 ++++++++++++++++-- src/index.ts | 1 + src/parseHand.test.ts | 12 +++++++++--- src/parseHand.ts | 2 +- src/{networks/all => }/parseSite.test.ts | 8 ++++---- src/{networks/all => }/parseSite.ts | 12 ++++++------ 6 files changed, 37 insertions(+), 16 deletions(-) rename src/{networks/all => }/parseSite.test.ts (67%) rename src/{networks/all => }/parseSite.ts (65%) diff --git a/README.md b/README.md index 2120044..35d9aee 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,9 @@ Add `@poker-apprentice/hand-history-parser` as a dependency. ## Usage -This package exports a `parseHand` promise-based function that can be used for parsing hand histories from any [support poker site](#supported-poker-sites). To use it, simply pass the contents of an individual hand history. +### `parseHand` + +This promise-based function can be used to parse hand histories from any [support poker site](#supported-poker-sites). To use it, simply pass the contents of an individual hand history. ```ts // assumes `hand` is a string containing the hand history file contents @@ -38,7 +40,7 @@ try { } ``` -The value returned is represented as a [`HandHistory`](https://github.com/poker-apprentice/hand-history-parser/blob/main/src/types.ts#L276) type object. For example: +The value returned is represented as a [`HandHistory`](https://github.com/poker-apprentice/hand-history-parser/blob/main/src/types.ts#L263) type object. For example: ```js { @@ -253,6 +255,18 @@ The value returned is represented as a [`HandHistory`](https://github.com/poker- } ``` +### `parseSite` + +This function can be used to determine the [support poker site](#supported-poker-sites) on which a hand was played. To use it, simply pass the contents of an individual hand history. + +```ts +// assumes `hand` is a string containing the hand history file contents +const site = parseSite(hand); +console.log(site); +``` + +The value returned is represented as a [`Site`](https://github.com/poker-apprentice/hand-history-parser/blob/main/src/types.ts#6) string union type. + ## Supported Poker Sites/Networks This package currently supports the following poker sites & networks: diff --git a/src/index.ts b/src/index.ts index 02adc28..da2f1c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export { InvalidHandError } from './errors/InvalidHandError'; export { InvalidSiteError } from './errors/InvalidSiteError'; export { parseHand } from './parseHand'; +export { parseSite } from './parseSite'; export * from './types'; diff --git a/src/parseHand.test.ts b/src/parseHand.test.ts index cf60b04..22a0d94 100644 --- a/src/parseHand.test.ts +++ b/src/parseHand.test.ts @@ -1,4 +1,6 @@ -import { HAND_ALL_IN as HAND_BOVADA } from '~/__fixtures__/hands/bovada'; +import { HAND as HAND_BODOG } from './__fixtures__/hands/bodog'; +import { HAND_ALL_IN as HAND_BOVADA } from './__fixtures__/hands/bovada'; +import { HAND as HAND_IGNITION } from './__fixtures__/hands/ignition'; import { parseHand } from './parseHand'; import { Site } from './types'; @@ -8,7 +10,11 @@ describe('parseHand', () => { return info.site; }; - it('parses bovada hands', async () => { - expect(await parseSiteFromHand(HAND_BOVADA)).toEqual('bovada'); + it.each([ + ['bodog', HAND_BODOG], + ['bovada', HAND_BOVADA], + ['ignition', HAND_IGNITION], + ])('parses %s hands', async (site, handHistoryContent) => { + expect(await parseSiteFromHand(handHistoryContent)).toEqual(site); }); }); diff --git a/src/parseHand.ts b/src/parseHand.ts index 606399b..1a1c0b0 100644 --- a/src/parseHand.ts +++ b/src/parseHand.ts @@ -1,7 +1,7 @@ import assertNever from 'assert-never'; import { InvalidHandError } from './errors/InvalidHandError'; import { InvalidSiteError } from './errors/InvalidSiteError'; -import { parseSite } from './networks/all/parseSite'; +import { parseSite } from './parseSite'; import { HandHistory, Site } from './types'; const getParser = async (site: Site) => { diff --git a/src/networks/all/parseSite.test.ts b/src/parseSite.test.ts similarity index 67% rename from src/networks/all/parseSite.test.ts rename to src/parseSite.test.ts index 67cacad..bf12e3e 100644 --- a/src/networks/all/parseSite.test.ts +++ b/src/parseSite.test.ts @@ -1,7 +1,7 @@ -import { HAND as HAND_BODOG } from '~/__fixtures__/hands/bodog'; -import { HAND_ALL_IN as HAND_BOVADA } from '~/__fixtures__/hands/bovada'; -import { HAND as HAND_IGNITION } from '~/__fixtures__/hands/ignition'; -import { InvalidSiteError } from '~/errors/InvalidSiteError'; +import { HAND as HAND_BODOG } from './__fixtures__/hands/bodog'; +import { HAND_ALL_IN as HAND_BOVADA } from './__fixtures__/hands/bovada'; +import { HAND as HAND_IGNITION } from './__fixtures__/hands/ignition'; +import { InvalidSiteError } from './errors/InvalidSiteError'; import { parseSite } from './parseSite'; describe('parseSite', () => { diff --git a/src/networks/all/parseSite.ts b/src/parseSite.ts similarity index 65% rename from src/networks/all/parseSite.ts rename to src/parseSite.ts index 3f37fa2..c1029dd 100644 --- a/src/networks/all/parseSite.ts +++ b/src/parseSite.ts @@ -1,9 +1,9 @@ -import { InvalidSiteError } from '~/errors/InvalidSiteError'; -import { SiteLexer } from '~/grammar/SiteLexer'; -import { SiteParser } from '~/grammar/SiteParser'; -import { Site } from '~/types'; -import { getParser } from '~/utils/getParser'; -import { SiteSwitchVisitor } from './SiteSwitchVisitor'; +import { InvalidSiteError } from './errors/InvalidSiteError'; +import { SiteLexer } from './grammar/SiteLexer'; +import { SiteParser } from './grammar/SiteParser'; +import { SiteSwitchVisitor } from './networks/all/SiteSwitchVisitor'; +import { Site } from './types'; +import { getParser } from './utils/getParser'; const getFirstLine = (str: string) => { const [firstLine] = str.replace(/^\s+/g, '').split(/[\r\n]+/g, 2);