Skip to content

Commit

Permalink
fix: ensure undefined values are removed in cleanup
Browse files Browse the repository at this point in the history
This fixes a crasher.
  • Loading branch information
aalemayhu committed Oct 23, 2024
1 parent 7374194 commit bc701b6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/lib/parser/Deck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Deck from './Deck';

describe('Deck', () => {
test('undefined cards becomes empty array', () => {
const deck = new Deck('deck', undefined, undefined, '', 0, null);

Check failure on line 5 in src/lib/parser/Deck.test.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Argument of type 'undefined' is not assignable to parameter of type '(Note | undefined)[]'.
expect(deck.cards).toStrictEqual([]);
});

test('handles cleaning undefined notes', () => {
const deck = new Deck('deck', [undefined], undefined, '', 0, null);

Check failure on line 10 in src/lib/parser/Deck.test.ts

View workflow job for this annotation

GitHub Actions / build (20.18.0)

Argument of type 'null' is not assignable to parameter of type 'Settings'.
expect(Deck.CleanCards(deck.cards)).toStrictEqual([]);
});
});
16 changes: 8 additions & 8 deletions src/lib/parser/Deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Settings from './Settings';
export default class Deck {
name: string;

cards: Note[];
cards: (Note | undefined)[];

image: string | undefined;

Expand All @@ -20,27 +20,27 @@ export default class Deck {

constructor(
name: string,
cards: Note[],
cards: (Note | undefined)[],
image: string | undefined,
style: string | null,
id: number,
settings: Settings
) {
this.settings = settings;
this.name = name;
this.cards = cards;
this.cards = cards ?? [];
this.image = image;
this.style = style;
this.id = id;
console.log(`New Deck with ${this.cards.length} cards`);
}

static CleanCards(cards: Note[]) {
static CleanCards(cards: (Note | undefined)[]) {
return cards.filter(
(note) =>
note.isValidClozeNote() ||
note.isValidInputNote() ||
note.isValidBasicNote()
note &&
(note.isValidClozeNote() ||
note.isValidInputNote() ||
note.isValidBasicNote())
);
}

Expand Down

0 comments on commit bc701b6

Please sign in to comment.