Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export parseSite function #2

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
12 changes: 9 additions & 3 deletions src/parseHand.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});
2 changes: 1 addition & 1 deletion src/parseHand.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions src/networks/all/parseSite.test.ts → src/parseSite.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/networks/all/parseSite.ts → src/parseSite.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down