Skip to content

Commit

Permalink
Merge pull request #2598 from KaelenProctor/feature/fix-collector-num…
Browse files Browse the repository at this point in the history
…ber-star-for-mana-pool

Fix collector numbers containing a star causing invalid character error (for Mana Pool links)
  • Loading branch information
dekkerglen authored Feb 8, 2025
2 parents fcfc2b7 + 6dde41e commit 346a9e3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/client/utils/Affiliate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ export const getBulkManaPoolLink = (cards: Card[]): string => {
const cardString = cards
.map((card) => `${cardName(card)} [${cardSet(card)}] ${cardCollectorNumber(card)}`)
.join('\n');
// base64 encode the card string
const encodedCardString = btoa(cardString);

/* base64 encode the card string
* Because the collector number may contain non-latin characters like ★ and btoa only works with latin1 character set,
* we use the solution in https://stackoverflow.com/a/70714541 to encode before btoa in order to have valid characters.
* Even though unescape is deprecated, it works in all browsers at this time
*/
const encodedCardString = btoa(unescape(encodeURIComponent(cardString)));
return `https://manapool.com/add-deck?ref=cubecobra&deck=${encodedCardString}`;
};

Expand Down
45 changes: 44 additions & 1 deletion tests/affiliate/links.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getCardMarketLink, getTCGLink, tcgplayerAffiliate } from 'utils/Affiliate';
import {
getBulkManaPoolLink,
getCardMarketLink,
getManaPoolLink,
getTCGLink,
tcgplayerAffiliate,
} from 'utils/Affiliate';

import { createCard, createCardDetails } from '../test-utils/data';

Expand Down Expand Up @@ -34,3 +40,40 @@ describe('CardMarket', () => {
);
});
});

describe('ManaPool', () => {
it('should generate a valid link for a card with a Mana Pool', () => {
const card = createCard({
details: createCardDetails({ name: 'Birds of Paradise', set: '6ED', collector_number: '217' }),
});

expect(getManaPoolLink(card)).toEqual('https://manapool.com/card/6ed/217/birds-of-paradise?ref=cubecobra');
});

it('should generate a valid link for a card with a Mana Pool, for a promo collector', () => {
const card = createCard({
details: createCardDetails({ name: 'Birds of Paradise', set: '7ed', collector_number: '231★' }),
});

expect(getManaPoolLink(card)).toEqual('https://manapool.com/card/7ed/231★/birds-of-paradise?ref=cubecobra');
});

it('should generate a valid link for a deck with a Mana Pool', () => {
const cards = [
createCard({
details: createCardDetails({ name: 'Birds of Paradise', set: '7ed', collector_number: '231★' }),
}),
createCard({
details: createCardDetails({ name: 'Oko, Thief of Crowns', set: 'ELD', collector_number: '197' }),
}),
];

let cardDetails = '';
cardDetails += `Birds of Paradise [7ed] 231★\n`;
cardDetails += `Oko, Thief of Crowns [ELD] 197`;

const encodedDeck = btoa(unescape(encodeURIComponent(cardDetails)));

expect(getBulkManaPoolLink(cards)).toEqual(`https://manapool.com/add-deck?ref=cubecobra&deck=${encodedDeck}`);
});
});

0 comments on commit 346a9e3

Please sign in to comment.