-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decklists): add main and side counts
- Loading branch information
1 parent
7ec2671
commit 223cb0b
Showing
8 changed files
with
70 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import { DECK_RE } from '@/tools/decklists/constants'; | ||
import type { Card } from '@/tools/decklists/types'; | ||
|
||
type ParseCards = (text: string) => { cards: Card[][]; count: number }; | ||
|
||
/** | ||
* Parse a body of lines representing cards in a decklist. | ||
* Empty lines found within the body will serve as delimiters for groups in | ||
* order to accomodate for multi-column rendering. | ||
*/ | ||
export const parseCards = (text: string): Card[][] => | ||
text | ||
export const parseCards: ParseCards = (text) => { | ||
let count = 0; | ||
const cards: Card[][] = text | ||
.split(DECK_RE.groupDelimiter) | ||
.map((group) => group.match(DECK_RE.line)) | ||
.filter((group) => group) | ||
.map((group) => | ||
(group as string[]).map((card) => { | ||
const [, quantity, name] = card.match(DECK_RE.card) || []; | ||
return [parseInt(quantity, 10), name]; | ||
const quantityValue = parseInt(quantity, 10); | ||
count += quantityValue; | ||
return [quantityValue, name]; | ||
}) | ||
); | ||
return { cards, count }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters