From 11c0a7a0c5fc4735a7a3cb0e7d80b823ec1f5788 Mon Sep 17 00:00:00 2001 From: ronzulu <75528127+ronzulu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:45:52 +1100 Subject: [PATCH] fix issues reported by pnpm lint --- src/Card.ts | 3 +- src/CardSchedule.ts | 52 +++++++++++++++--------------- src/Deck.ts | 39 ++++++++++++----------- src/DeckTreeIterator.ts | 10 +++--- src/DeckTreeStatsCalculator.ts | 12 +++---- src/FlashcardReviewSequencer.ts | 18 +++++------ src/NoteFileLoader.ts | 8 ++--- src/NoteParser.ts | 6 ++-- src/NoteQuestionParser.ts | 36 ++++++++++----------- src/Question.ts | 27 ++++++++-------- src/QuestionType.ts | 27 ++++++++-------- src/SRFile.ts | 4 +-- src/TopicPath.ts | 21 ++++++------- src/gui/flashcard-modal.tsx | 39 +++++++++-------------- src/gui/flashcards-edit-modal.ts | 1 - src/main.ts | 54 ++++++++++++-------------------- src/note.ts | 4 +-- src/scheduling.ts | 2 -- src/stats.ts | 1 - src/util/DateProvider.ts | 2 +- src/util/MultiLineTextFinder.ts | 16 +++++----- src/util/NumberCountDict.ts | 2 +- src/util/RandomNumberProvider.ts | 24 ++------------ src/util/utils.ts | 7 +---- 24 files changed, 176 insertions(+), 239 deletions(-) diff --git a/src/Card.ts b/src/Card.ts index e15e0a3b..b95ce6c6 100644 --- a/src/Card.ts +++ b/src/Card.ts @@ -1,7 +1,6 @@ import { Question } from "./Question"; import { CardScheduleInfo } from "./CardSchedule"; import { CardListType } from "./Deck"; -import { SRSettings } from "./settings"; export class Card { question: Question; @@ -34,7 +33,7 @@ export class Card { } formatSchedule(): string { - var result: string; + let result: string = ""; if (this.hasSchedule) result = this.scheduleInfo.formatSchedule(); else result = "New"; return result; diff --git a/src/CardSchedule.ts b/src/CardSchedule.ts index a86084b2..12403ab4 100644 --- a/src/CardSchedule.ts +++ b/src/CardSchedule.ts @@ -1,9 +1,7 @@ -import moment, { Moment } from "moment"; +import { Moment } from "moment"; import { - ALLOWED_DATE_FORMATS, LEGACY_SCHEDULING_EXTRACTOR, MULTI_SCHEDULING_EXTRACTOR, - PREFERRED_DATE_FORMAT, TICKS_PER_DAY, } from "./constants"; import { INoteEaseList } from "./NoteEaseList"; @@ -48,7 +46,7 @@ export class CardScheduleInfo { ease: number, delayBeforeReviewTicks: number, ) { - let dueDateTicks: Moment = DateUtil.dateStrToMoment(dueDateStr); + const dueDateTicks: Moment = DateUtil.dateStrToMoment(dueDateStr); return new CardScheduleInfo(dueDateTicks, interval, ease, delayBeforeReviewTicks); } @@ -91,18 +89,18 @@ export class CardScheduleCalculator { } getResetCardSchedule(): CardScheduleInfo { - let interval = CardScheduleInfo.initialInterval; - let ease = this.settings.baseEase; - let dueDate = globalDateProvider.today.add(interval, "d"); - let delayBeforeReview = 0; + const interval = CardScheduleInfo.initialInterval; + const ease = this.settings.baseEase; + const dueDate = globalDateProvider.today.add(interval, "d"); + const delayBeforeReview = 0; return CardScheduleInfo.fromDueDateMoment(dueDate, interval, ease, delayBeforeReview); } getNewCardSchedule(response: ReviewResponse, notePath: string): CardScheduleInfo { - let initial_ease: number = this.noteEaseList.getEaseByPath(notePath); - let delayBeforeReview = 0; + const initial_ease: number = this.noteEaseList.getEaseByPath(notePath); + const delayBeforeReview = 0; - let schedObj: Record = schedule( + const schedObj: Record = schedule( response, CardScheduleInfo.initialInterval, initial_ease, @@ -111,9 +109,9 @@ export class CardScheduleCalculator { this.dueDatesFlashcards, ); - let interval = schedObj.interval; - let ease = schedObj.ease; - let dueDate = globalDateProvider.today.add(interval, "d"); + const interval = schedObj.interval; + const ease = schedObj.ease; + const dueDate = globalDateProvider.today.add(interval, "d"); return CardScheduleInfo.fromDueDateMoment(dueDate, interval, ease, delayBeforeReview); } @@ -121,7 +119,7 @@ export class CardScheduleCalculator { response: ReviewResponse, cardSchedule: CardScheduleInfo, ): CardScheduleInfo { - let schedObj: Record = schedule( + const schedObj: Record = schedule( response, cardSchedule.interval, cardSchedule.ease, @@ -129,10 +127,10 @@ export class CardScheduleCalculator { this.settings, this.dueDatesFlashcards, ); - let interval = schedObj.interval; - let ease = schedObj.ease; - let dueDate = globalDateProvider.today.add(interval, "d"); - let delayBeforeReview: 0; + const interval = schedObj.interval; + const ease = schedObj.ease; + const dueDate = globalDateProvider.today.add(interval, "d"); + const delayBeforeReview = 0; return CardScheduleInfo.fromDueDateMoment(dueDate, interval, ease, delayBeforeReview); } } @@ -143,17 +141,17 @@ export class NoteCardScheduleParser { if (scheduling.length === 0) scheduling = [...questionText.matchAll(LEGACY_SCHEDULING_EXTRACTOR)]; - let result: CardScheduleInfo[] = []; + const result: CardScheduleInfo[] = []; for (let i = 0; i < scheduling.length; i++) { - let match: RegExpMatchArray = scheduling[i]; - let dueDateStr = match[1]; - let interval = parseInt(match[2]); - let ease = parseInt(match[3]); - let dueDate: Moment = DateUtil.dateStrToMoment(dueDateStr); - let delayBeforeReviewTicks: number = + const match: RegExpMatchArray = scheduling[i]; + const dueDateStr = match[1]; + const interval = parseInt(match[2]); + const ease = parseInt(match[3]); + const dueDate: Moment = DateUtil.dateStrToMoment(dueDateStr); + const delayBeforeReviewTicks: number = dueDate.valueOf() - globalDateProvider.today.valueOf(); - let info: CardScheduleInfo = new CardScheduleInfo( + const info: CardScheduleInfo = new CardScheduleInfo( dueDate, interval, ease, diff --git a/src/Deck.ts b/src/Deck.ts index af4e47b6..fceecb56 100644 --- a/src/Deck.ts +++ b/src/Deck.ts @@ -60,7 +60,7 @@ export class Deck { if (!topicPath.hasPath) { return this; } - let t: TopicPath = topicPath.clone(); + const t: TopicPath = topicPath.clone(); const deckName: string = t.shift(); for (const subdeck of this.subdecks) { if (deckName === subdeck.deckName) { @@ -70,8 +70,7 @@ export class Deck { let result: Deck = null; if (createAllowed) { - let parent: Deck = this; - const subdeck: Deck = new Deck(deckName, parent); + const subdeck: Deck = new Deck(deckName, this /* parent */); this.subdecks.push(subdeck); result = subdeck._getOrCreateDeck(t, createAllowed); } @@ -79,7 +78,8 @@ export class Deck { } getTopicPath(): TopicPath { - let list: string[] = []; + const list: string[] = []; + // eslint-disable-next-line @typescript-eslint/no-this-alias let deck: Deck = this; while (!deck.isRootDeck) { list.push(deck.deckName); @@ -89,6 +89,7 @@ export class Deck { } getRootDeck(): Deck { + // eslint-disable-next-line @typescript-eslint/no-this-alias let deck: Deck = this; while (!deck.isRootDeck) { deck = deck.parent; @@ -97,7 +98,7 @@ export class Deck { } getCard(index: number, cardListType: CardListType): Card { - let cardList: Card[] = this.getCardListForCardType(cardListType); + const cardList: Card[] = this.getCardListForCardType(cardListType); return cardList[index]; } @@ -106,20 +107,20 @@ export class Deck { } appendCard(topicPath: TopicPath, cardObj: Card): void { - let deck: Deck = this.getOrCreateDeck(topicPath); - let cardList: Card[] = deck.getCardListForCardType(cardObj.cardListType); + const deck: Deck = this.getOrCreateDeck(topicPath); + const cardList: Card[] = deck.getCardListForCardType(cardObj.cardListType); cardList.push(cardObj); } deleteCard(card: Card): void { - let cardList: Card[] = this.getCardListForCardType(card.cardListType); - let idx = cardList.indexOf(card); + const cardList: Card[] = this.getCardListForCardType(card.cardListType); + const idx = cardList.indexOf(card); if (idx != -1) cardList.splice(idx, 1); } deleteCardAtIndex(index: number, cardListType: CardListType): void { - let cardList: Card[] = this.getCardListForCardType(cardListType); + const cardList: Card[] = this.getCardListForCardType(cardListType); cardList.splice(index, 1); } @@ -130,7 +131,7 @@ export class Deck { } toDeckArray(): Deck[] { - let result: Deck[] = []; + const result: Deck[] = []; result.push(this); for (const subdeck of this.subdecks) { result.push(...subdeck.toDeckArray()); @@ -165,12 +166,12 @@ export class Deck { result += `${indentStr}${this.deckName}\r\n`; indentStr += " "; for (let i = 0; i < this.newFlashcards.length; i++) { - let card = this.newFlashcards[i]; + const card = this.newFlashcards[i]; result += `${indentStr}New: ${i}: ${card.front}::${card.back}\r\n`; } for (let i = 0; i < this.dueFlashcards.length; i++) { - let card = this.dueFlashcards[i]; - let s = card.isDue ? "Due" : "Not due"; + const card = this.dueFlashcards[i]; + const s = card.isDue ? "Due" : "Not due"; result += `${indentStr}${s}: ${i}: ${card.front}::${card.back}\r\n`; } @@ -181,24 +182,24 @@ export class Deck { } clone(): Deck { - return this.copyWithCardFilter((card) => true); + return this.copyWithCardFilter(() => true); } copyWithCardFilter(predicate: (value: Card) => boolean, parent: Deck = null): Deck { - let result: Deck = new Deck(this.deckName, parent); + const result: Deck = new Deck(this.deckName, parent); result.newFlashcards = [...this.newFlashcards.filter((card) => predicate(card))]; result.dueFlashcards = [...this.dueFlashcards.filter((card) => predicate(card))]; for (const s of this.subdecks) { - let newParent = result; - let newDeck = s.copyWithCardFilter(predicate, newParent); + const newParent = result; + const newDeck = s.copyWithCardFilter(predicate, newParent); result.subdecks.push(newDeck); } return result; } static otherListType(cardListType: CardListType): CardListType { - var result: CardListType; + let result: CardListType; if (cardListType == CardListType.NewCard) result = CardListType.DueCard; else if (cardListType == CardListType.DueCard) result = CardListType.NewCard; else throw "Invalid cardListType"; diff --git a/src/DeckTreeIterator.ts b/src/DeckTreeIterator.ts index e89455a6..8f5b39d7 100644 --- a/src/DeckTreeIterator.ts +++ b/src/DeckTreeIterator.ts @@ -96,7 +96,7 @@ class SingleDeckIterator { private nextCardWithinList(): boolean { let result: boolean = false; - let cardList: Card[] = this.deck.getCardListForCardType(this.cardListType); + const cardList: Card[] = this.deck.getCardListForCardType(this.cardListType); // Delete the current card so we don't return it again if (this.hasCurrentCard) { @@ -119,8 +119,8 @@ class SingleDeckIterator { deleteCurrentQuestion(): void { this.ensureCurrentCard(); - let q: Question = this.currentCard.question; - let cards: Card[] = this.deck.getCardListForCardType(this.cardListType); + const q: Question = this.currentCard.question; + const cards: Card[] = this.deck.getCardListForCardType(this.cardListType); do { this.deck.deleteCardAtIndex(this.cardIdx, this.cardListType); } while (this.cardIdx < cards.length && Object.is(q, cards[this.cardIdx].question)); @@ -135,9 +135,9 @@ class SingleDeckIterator { moveCurrentCardToEndOfList(): void { this.ensureCurrentCard(); - let cardList: Card[] = this.deck.getCardListForCardType(this.cardListType); + const cardList: Card[] = this.deck.getCardListForCardType(this.cardListType); if (cardList.length <= 1) return; - let card = this.currentCard; + const card = this.currentCard; this.deck.deleteCardAtIndex(this.cardIdx, this.cardListType); this.deck.appendCard(TopicPath.emptyPath, card); this.setNoCurrentCard(); diff --git a/src/DeckTreeStatsCalculator.ts b/src/DeckTreeStatsCalculator.ts index f83eb552..e810b094 100644 --- a/src/DeckTreeStatsCalculator.ts +++ b/src/DeckTreeStatsCalculator.ts @@ -1,4 +1,4 @@ -import { CardListType, Deck } from "./Deck"; +import { Deck } from "./Deck"; import { CardListOrder, DeckTreeIterator, @@ -16,21 +16,21 @@ export class DeckTreeStatsCalculator { calculate(deckTree: Deck): Stats { // Order doesn't matter as long as we iterate over everything - let iteratorOrder: IIteratorOrder = { + const iteratorOrder: IIteratorOrder = { deckOrder: OrderMethod.Sequential, cardListOrder: CardListOrder.DueFirst, cardOrder: OrderMethod.Sequential, }; - let iterator: IDeckTreeIterator = new DeckTreeIterator( + const iterator: IDeckTreeIterator = new DeckTreeIterator( iteratorOrder, IteratorDeckSource.CloneBeforeUse, ); - let result = new Stats(); + const result = new Stats(); iterator.setDeck(deckTree); while (iterator.nextCard()) { - let card: Card = iterator.currentCard; + const card: Card = iterator.currentCard; if (card.hasSchedule) { - let schedule: CardScheduleInfo = card.scheduleInfo; + const schedule: CardScheduleInfo = card.scheduleInfo; result.update(schedule.delayBeforeReviewDaysInt, schedule.interval, schedule.ease); } else { result.incrementNew(); diff --git a/src/FlashcardReviewSequencer.ts b/src/FlashcardReviewSequencer.ts index baf9d048..af522bb1 100644 --- a/src/FlashcardReviewSequencer.ts +++ b/src/FlashcardReviewSequencer.ts @@ -1,7 +1,7 @@ import { Card } from "./Card"; import { CardListType, Deck } from "./Deck"; import { Question, QuestionText } from "./Question"; -import { ReviewResponse, schedule } from "./scheduling"; +import { ReviewResponse } from "./scheduling"; import { SRSettings } from "./settings"; import { TopicPath } from "./TopicPath"; import { CardScheduleInfo, ICardScheduleCalculator } from "./CardSchedule"; @@ -93,7 +93,7 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer { } setCurrentDeck(topicPath: TopicPath): void { - let deck: Deck = this.remainingDeckTree.getDeck(topicPath); + const deck: Deck = this.remainingDeckTree.getDeck(topicPath); this.cardSequencer.setDeck(deck); this.cardSequencer.nextCard(); } @@ -103,12 +103,12 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer { } getDeckStats(topicPath: TopicPath): DeckStats { - let totalCount: number = this._originalDeckTree + const totalCount: number = this._originalDeckTree .getDeck(topicPath) .getCardCount(CardListType.All, true); - let remainingDeck: Deck = this.remainingDeckTree.getDeck(topicPath); - let newCount: number = remainingDeck.getCardCount(CardListType.NewCard, true); - let dueCount: number = remainingDeck.getCardCount(CardListType.DueCard, true); + const remainingDeck: Deck = this.remainingDeckTree.getDeck(topicPath); + const newCount: number = remainingDeck.getCardCount(CardListType.NewCard, true); + const dueCount: number = remainingDeck.getCardCount(CardListType.DueCard, true); return new DeckStats(dueCount, newCount, totalCount); } @@ -155,7 +155,7 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer { } determineCardSchedule(response: ReviewResponse, card: Card): CardScheduleInfo { - var result: CardScheduleInfo; + let result: CardScheduleInfo; if (response == ReviewResponse.Reset) { // Resetting the card schedule @@ -168,7 +168,7 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer { card.scheduleInfo, ); } else { - let currentNote: Note = card.question.note; + const currentNote: Note = card.question.note; result = this.cardScheduleCalculator.getNewCardSchedule( response, currentNote.filePath, @@ -179,7 +179,7 @@ export class FlashcardReviewSequencer implements IFlashcardReviewSequencer { } async updateCurrentQuestionText(text: string): Promise { - let q: QuestionText = this.currentQuestion.questionText; + const q: QuestionText = this.currentQuestion.questionText; q.actualQuestion = text; diff --git a/src/NoteFileLoader.ts b/src/NoteFileLoader.ts index bff51791..83ed69e9 100644 --- a/src/NoteFileLoader.ts +++ b/src/NoteFileLoader.ts @@ -1,8 +1,6 @@ import { ISRFile } from "./SRFile"; -import { CardScheduleInfo } from "./CardSchedule"; import { Note } from "./Note"; import { Question } from "./Question"; -import { CardFrontBack } from "./QuestionType"; import { TopicPath } from "./TopicPath"; import { NoteQuestionParser } from "./NoteQuestionParser"; import { SRSettings } from "./settings"; @@ -21,14 +19,14 @@ export class NoteFileLoader { async load(noteFile: ISRFile, noteTopicPath: TopicPath): Promise { this.noteFile = noteFile; - let questionParser: NoteQuestionParser = new NoteQuestionParser(this.settings); + const questionParser: NoteQuestionParser = new NoteQuestionParser(this.settings); - let questionList: Question[] = await questionParser.createQuestionList( + const questionList: Question[] = await questionParser.createQuestionList( noteFile, noteTopicPath, ); - let result: Note = new Note(noteFile, questionList); + const result: Note = new Note(noteFile, questionList); return result; } } diff --git a/src/NoteParser.ts b/src/NoteParser.ts index 9aa93dcf..01a00ab6 100644 --- a/src/NoteParser.ts +++ b/src/NoteParser.ts @@ -13,10 +13,10 @@ export class NoteParser { } async parse(noteFile: ISRFile, folderTopicPath: TopicPath): Promise { - let questionParser: NoteQuestionParser = new NoteQuestionParser(this.settings); - let questions = await questionParser.createQuestionList(noteFile, folderTopicPath); + const questionParser: NoteQuestionParser = new NoteQuestionParser(this.settings); + const questions = await questionParser.createQuestionList(noteFile, folderTopicPath); - let result: Note = new Note(noteFile, questions); + const result: Note = new Note(noteFile, questions); return result; } } diff --git a/src/NoteQuestionParser.ts b/src/NoteQuestionParser.ts index 8e2ff025..1f8c01b6 100644 --- a/src/NoteQuestionParser.ts +++ b/src/NoteQuestionParser.ts @@ -4,7 +4,7 @@ import { parse } from "./parser"; import { CardType, Question } from "./Question"; import { CardFrontBack, CardFrontBackUtil } from "./QuestionType"; import { SRSettings } from "./settings"; -import { ISRFile, UnitTestSRFile } from "./SRFile"; +import { ISRFile } from "./SRFile"; import { TopicPath } from "./TopicPath"; export class ParsedQuestionInfo { @@ -31,15 +31,15 @@ export class NoteQuestionParser { async createQuestionList(noteFile: ISRFile, folderTopicPath: TopicPath): Promise { this.noteFile = noteFile; - let noteText: string = await noteFile.read(); - var noteTopicPath: TopicPath; + const noteText: string = await noteFile.read(); + let noteTopicPath: TopicPath; if (this.settings.convertFoldersToDecks) { noteTopicPath = folderTopicPath; } else { - let tagList: string[] = noteFile.getAllTags(); + const tagList: string[] = noteFile.getAllTags(); noteTopicPath = this.determineTopicPathFromTags(tagList); } - var result: Question[] = this.doCreateQuestionList(noteText, noteTopicPath); + const result: Question[] = this.doCreateQuestionList(noteText, noteTopicPath); return result; } @@ -47,14 +47,14 @@ export class NoteQuestionParser { this.noteText = noteText; this.noteTopicPath = noteTopicPath; - let result: Question[] = []; - let parsedQuestionInfoList: [CardType, string, number][] = this.parseQuestions(); + const result: Question[] = []; + const parsedQuestionInfoList: [CardType, string, number][] = this.parseQuestions(); for (const t of parsedQuestionInfoList) { - let parsedQuestionInfo: ParsedQuestionInfo = new ParsedQuestionInfo(t[0], t[1], t[2]); - let question: Question = this.createQuestionObject(parsedQuestionInfo); + const parsedQuestionInfo: ParsedQuestionInfo = new ParsedQuestionInfo(t[0], t[1], t[2]); + const question: Question = this.createQuestionObject(parsedQuestionInfo); // Each rawCardText can turn into multiple CardFrontBack's (e.g. CardType.Cloze, CardType.SingleLineReversed) - let cardFrontBackList: CardFrontBack[] = CardFrontBackUtil.expand( + const cardFrontBackList: CardFrontBack[] = CardFrontBackUtil.expand( question.questionType, question.questionText.actualQuestion, this.settings, @@ -65,14 +65,14 @@ export class NoteQuestionParser { NoteCardScheduleParser.createCardScheduleInfoList(question.questionText.original); // we have some extra scheduling dates to delete - let correctLength = cardFrontBackList.length; + const correctLength = cardFrontBackList.length; if (cardScheduleInfoList.length > correctLength) { question.hasChanged = true; cardScheduleInfoList = cardScheduleInfoList.slice(0, correctLength); } // Create the list of card objects, and attach to the question - let cardList: Card[] = this.createCardList(cardFrontBackList, cardScheduleInfoList); + const cardList: Card[] = this.createCardList(cardFrontBackList, cardScheduleInfoList); question.setCardList(cardList); result.push(question); } @@ -80,8 +80,7 @@ export class NoteQuestionParser { } private parseQuestions(): [CardType, string, number][] { - let settings: SRSettings = this.settings; - const lines: string[] = this.noteText.replaceAll("\r\n", "\n").split("\n"); + const settings: SRSettings = this.settings; const result: [CardType, string, number][] = parse( this.noteText, settings.singleLineCardSeparator, @@ -96,11 +95,10 @@ export class NoteQuestionParser { } private createQuestionObject(parsedQuestionInfo: ParsedQuestionInfo): Question { - var { cardType, cardText, lineNo } = parsedQuestionInfo; + const { cardType, cardText, lineNo } = parsedQuestionInfo; - let originalQuestionText: string = cardText; const questionContext: string[] = this.noteFile.getQuestionContext(lineNo); - let result = Question.Create( + const result = Question.Create( this.settings, cardType, this.noteTopicPath, @@ -119,9 +117,9 @@ export class NoteQuestionParser { // One card for each CardFrontBack, regardless if there is scheduled info for it for (let i = 0; i < cardFrontBackList.length; i++) { - let { front, back } = cardFrontBackList[i]; + const { front, back } = cardFrontBackList[i]; - let hasScheduleInfo: boolean = i < cardScheduleInfoList.length; + const hasScheduleInfo: boolean = i < cardScheduleInfoList.length; const cardObj: Card = new Card({ front, back, diff --git a/src/Question.ts b/src/Question.ts index 62eb0463..82948c10 100644 --- a/src/Question.ts +++ b/src/Question.ts @@ -3,10 +3,9 @@ import { CardScheduleInfo, NoteCardScheduleParser } from "./CardSchedule"; import { SR_HTML_COMMENT_BEGIN, SR_HTML_COMMENT_END } from "./constants"; import { Note } from "./Note"; import { SRSettings } from "./settings"; -import { ISRFile } from "./SRFile"; import { TopicPath } from "./TopicPath"; import { MultiLineTextFinder } from "./util/MultiLineTextFinder"; -import { cyrb53, escapeRegexString, splitTextIntoLineArray } from "./util/utils"; +import { cyrb53 } from "./util/utils"; export enum CardType { SingleLineBasic, @@ -69,7 +68,7 @@ export class QuestionText { } static create(original: string, settings: SRSettings): QuestionText { - let [topicPath, postTopicPathWhiteSpace, actualQuestion] = this.splitText( + const [topicPath, postTopicPathWhiteSpace, actualQuestion] = this.splitText( original, settings, ); @@ -78,7 +77,7 @@ export class QuestionText { } static splitText(original: string, settings: SRSettings): [TopicPath, string, string] { - let strippedSR = NoteCardScheduleParser.removeCardScheduleInfo(original).trim(); + const strippedSR = NoteCardScheduleParser.removeCardScheduleInfo(original).trim(); let actualQuestion: string = strippedSR; let whiteSpace: string = ""; @@ -141,8 +140,8 @@ export class Question { // We always want the correct schedule format, so we use this if there is no schedule for a card for (let i = 0; i < this.cards.length; i++) { - let card: Card = this.cards[i]; - let schedule: CardScheduleInfo = card.hasSchedule + const card: Card = this.cards[i]; + const schedule: CardScheduleInfo = card.hasSchedule ? card.scheduleInfo : CardScheduleInfo.getDummySchedule(settings); result += schedule.formatSchedule(); @@ -161,15 +160,15 @@ export class Question { } updateQuestionText(noteText: string, settings: SRSettings): string { - let originalText: string = this.questionText.original; + const originalText: string = this.questionText.original; // Get the entire text for the question including: // 1. the topic path (if present), // 2. the question text // 3. the schedule HTML comment (if present) - let replacementText = this.formatForNote(settings); + const replacementText = this.formatForNote(settings); - var newText = MultiLineTextFinder.findAndReplace(noteText, originalText, replacementText); + let newText = MultiLineTextFinder.findAndReplace(noteText, originalText, replacementText); if (newText) { this.questionText = QuestionText.create(replacementText, settings); } else { @@ -185,9 +184,9 @@ export class Question { } async writeQuestion(settings: SRSettings): Promise { - let fileText: string = await this.note.file.read(); + const fileText: string = await this.note.file.read(); - let newText: string = this.updateQuestionText(fileText, settings); + const newText: string = this.updateQuestionText(fileText, settings); await this.note.file.write(newText); this.hasChanged = false; } @@ -200,15 +199,15 @@ export class Question { lineNo: number, context: string[], ): Question { - let hasEditLaterTag = originalText.includes(settings.editLaterTag); - let questionText: QuestionText = QuestionText.create(originalText, settings); + const hasEditLaterTag = originalText.includes(settings.editLaterTag); + const questionText: QuestionText = QuestionText.create(originalText, settings); let topicPath: TopicPath = noteTopicPath; if (questionText.topicPath.hasPath) { topicPath = questionText.topicPath; } - let result: Question = new Question({ + const result: Question = new Question({ questionType, topicPath, questionText, diff --git a/src/QuestionType.ts b/src/QuestionType.ts index a483149a..2592d07b 100644 --- a/src/QuestionType.ts +++ b/src/QuestionType.ts @@ -1,4 +1,3 @@ -import { getDefaultHighWaterMark } from "stream"; import { CardType } from "./Question"; import { SRSettings } from "./settings"; @@ -18,7 +17,7 @@ export class CardFrontBackUtil { questionText: string, settings: SRSettings, ): CardFrontBack[] { - let handler: IQuestionTypeHandler = QuestionTypeFactory.create(questionType); + const handler: IQuestionTypeHandler = QuestionTypeFactory.create(questionType); return handler.expand(questionText, settings); } } @@ -29,24 +28,24 @@ export interface IQuestionTypeHandler { class QuestionType_SingleLineBasic implements IQuestionTypeHandler { expand(questionText: string, settings: SRSettings): CardFrontBack[] { - let idx: number = questionText.indexOf(settings.singleLineCardSeparator); - let item: CardFrontBack = new CardFrontBack( + const idx: number = questionText.indexOf(settings.singleLineCardSeparator); + const item: CardFrontBack = new CardFrontBack( questionText.substring(0, idx), questionText.substring(idx + settings.singleLineCardSeparator.length), ); - let result: CardFrontBack[] = [item]; + const result: CardFrontBack[] = [item]; return result; } } class QuestionType_SingleLineReversed implements IQuestionTypeHandler { expand(questionText: string, settings: SRSettings): CardFrontBack[] { - let idx: number = questionText.indexOf(settings.singleLineReversedCardSeparator); + const idx: number = questionText.indexOf(settings.singleLineReversedCardSeparator); const side1: string = questionText.substring(0, idx), side2: string = questionText.substring( idx + settings.singleLineReversedCardSeparator.length, ); - let result: CardFrontBack[] = [ + const result: CardFrontBack[] = [ new CardFrontBack(side1, side2), new CardFrontBack(side2, side1), ]; @@ -56,25 +55,25 @@ class QuestionType_SingleLineReversed implements IQuestionTypeHandler { class QuestionType_MultiLineBasic implements IQuestionTypeHandler { expand(questionText: string, settings: SRSettings): CardFrontBack[] { - let idx = questionText.indexOf("\n" + settings.multilineCardSeparator + "\n"); - let item: CardFrontBack = new CardFrontBack( + const idx = questionText.indexOf("\n" + settings.multilineCardSeparator + "\n"); + const item: CardFrontBack = new CardFrontBack( questionText.substring(0, idx), questionText.substring(idx + 2 + settings.multilineCardSeparator.length), ); - let result: CardFrontBack[] = [item]; + const result: CardFrontBack[] = [item]; return result; } } class QuestionType_MultiLineReversed implements IQuestionTypeHandler { expand(questionText: string, settings: SRSettings): CardFrontBack[] { - let idx = questionText.indexOf("\n" + settings.multilineReversedCardSeparator + "\n"); + const idx = questionText.indexOf("\n" + settings.multilineReversedCardSeparator + "\n"); const side1: string = questionText.substring(0, idx), side2: string = questionText.substring( idx + 2 + settings.multilineReversedCardSeparator.length, ); - let result: CardFrontBack[] = [ + const result: CardFrontBack[] = [ new CardFrontBack(side1, side2), new CardFrontBack(side2, side1), ]; @@ -106,7 +105,7 @@ class QuestionType_Cloze implements IQuestionTypeHandler { }); let front: string, back: string; - let result: CardFrontBack[] = []; + const result: CardFrontBack[] = []; for (const m of siblings) { const deletionStart: number = m.index, deletionEnd: number = deletionStart + m[0].length; @@ -149,7 +148,7 @@ export class QuestionType_ClozeUtil { export class QuestionTypeFactory { static create(questionType: CardType): IQuestionTypeHandler { - var handler: IQuestionTypeHandler; + let handler: IQuestionTypeHandler; switch (questionType) { case CardType.SingleLineBasic: handler = new QuestionType_SingleLineBasic(); diff --git a/src/SRFile.ts b/src/SRFile.ts index 79ef273c..46378070 100644 --- a/src/SRFile.ts +++ b/src/SRFile.ts @@ -5,7 +5,6 @@ import { getAllTags as ObsidianGetAllTags, HeadingCache, } from "obsidian"; -import { OBSIDIAN_TAG_AT_STARTOFLINE_REGEX } from "./constants"; import { getAllTagsFromText } from "./util/utils"; export interface ISRFile { @@ -57,7 +56,7 @@ export class SrTFile implements ISRFile { stack.push(heading); } - let result = []; + const result = []; for (const headingObj of stack) { headingObj.heading = headingObj.heading.replace(/\[\^\d+\]/gm, "").trim(); result.push(headingObj.heading); @@ -95,6 +94,7 @@ export class UnitTestSRFile implements ISRFile { return getAllTagsFromText(this.content); } + // eslint-disable-next-line @typescript-eslint/no-unused-vars getQuestionContext(cardLine: number): string[] { return []; } diff --git a/src/TopicPath.ts b/src/TopicPath.ts index 05eac710..23914066 100644 --- a/src/TopicPath.ts +++ b/src/TopicPath.ts @@ -1,4 +1,3 @@ -import { MetadataCache, TFile, getAllTags } from "obsidian"; import { SRSettings } from "src/settings"; import { OBSIDIAN_TAG_AT_STARTOFLINE_REGEX } from "./constants"; import { ISRFile } from "./SRFile"; @@ -35,13 +34,13 @@ export class TopicPath { formatAsTag(): string { if (this.isEmptyPath) throw "Empty path"; - let result = "#" + this.path.join("/"); + const result = "#" + this.path.join("/"); return result; } static getTopicPathOfFile(noteFile: ISRFile, settings: SRSettings): TopicPath { - var deckPath: string[] = []; - var result: TopicPath = TopicPath.emptyPath; + let deckPath: string[] = []; + let result: TopicPath = TopicPath.emptyPath; if (settings.convertFoldersToDecks) { deckPath = noteFile.path.split("/"); @@ -50,7 +49,7 @@ export class TopicPath { result = new TopicPath(deckPath); } } else { - let tagList: TopicPath[] = this.getTopicPathsFromTagList(noteFile.getAllTags()); + const tagList: TopicPath[] = this.getTopicPathsFromTagList(noteFile.getAllTags()); outer: for (const tagToReview of this.getTopicPathsFromTagList( settings.flashcardTags, @@ -82,17 +81,17 @@ export class TopicPath { } static removeTopicPathFromStartOfCardText(cardText: string): [string, string] { - let cardText1: string = cardText + const cardText1: string = cardText .trimStart() .replaceAll(OBSIDIAN_TAG_AT_STARTOFLINE_REGEX, ""); - let cardText2: string = cardText1.trimStart(); - let whiteSpaceLength: number = cardText1.length - cardText2.length; - let whiteSpace: string = cardText1.substring(0, whiteSpaceLength); + const cardText2: string = cardText1.trimStart(); + const whiteSpaceLength: number = cardText1.length - cardText2.length; + const whiteSpace: string = cardText1.substring(0, whiteSpaceLength); return [cardText2, whiteSpace]; } static getTopicPathsFromTagList(tagList: string[]): TopicPath[] { - let result: TopicPath[] = []; + const result: TopicPath[] = []; for (const tag of tagList) { if (this.isValidTag(tag)) result.push(TopicPath.getTopicPathFromTag(tag)); } @@ -112,7 +111,7 @@ export class TopicPath { if (tag[0] != "#") throw "Tag must start with #"; if (tag.length == 1) throw "Invalid tag"; - let path: string[] = tag + const path: string[] = tag .replace("#", "") .split("/") .filter((str) => str); diff --git a/src/gui/flashcard-modal.tsx b/src/gui/flashcard-modal.tsx index c32f2e80..a574de4d 100644 --- a/src/gui/flashcard-modal.tsx +++ b/src/gui/flashcard-modal.tsx @@ -1,23 +1,12 @@ -import { - ButtonComponent, - Modal, - App, - MarkdownRenderer, - Notice, - Platform, - TFile, - setIcon, -} from "obsidian"; +import { Modal, App, Notice, Platform, setIcon } from "obsidian"; // eslint-disable-next-line @typescript-eslint/no-unused-vars import h from "vhtml"; import type SRPlugin from "src/main"; import { SRSettings } from "src/settings"; -import { schedule, textInterval, ReviewResponse } from "src/scheduling"; -import { COLLAPSE_ICON, IMAGE_FORMATS, AUDIO_FORMATS, VIDEO_FORMATS } from "src/constants"; -import { escapeRegexString, cyrb53 } from "src/util/utils"; +import { textInterval, ReviewResponse } from "src/scheduling"; +import { COLLAPSE_ICON } from "src/constants"; import { t } from "src/lang/helpers"; -import { unwatchFile } from "fs"; import { Card } from "../Card"; import { CardListType, Deck } from "../Deck"; import { CardType, Question } from "../Question"; @@ -29,7 +18,7 @@ import { import { FlashcardEditModal } from "./flashcards-edit-modal"; import { Note } from "src/Note"; import { RenderMarkdownWrapper } from "src/util/RenderMarkdownWrapper"; -import { CardScheduleCalculator, CardScheduleInfo } from "src/CardSchedule"; +import { CardScheduleInfo } from "src/CardSchedule"; import { TopicPath } from "src/TopicPath"; export enum FlashcardModalMode { @@ -146,7 +135,7 @@ export class FlashcardModal extends Modal { renderDecksList(): void { this.mode = FlashcardModalMode.DecksList; - let stats: DeckStats = this.reviewSequencer.getDeckStats(TopicPath.emptyPath); + const stats: DeckStats = this.reviewSequencer.getDeckStats(TopicPath.emptyPath); this.titleEl.setText(t("DECKS")); this.titleEl.innerHTML += (

@@ -202,7 +191,7 @@ export class FlashcardModal extends Modal { const deckViewInnerText: HTMLElement = deckViewInner.createDiv("tag-pane-tag-text"); deckViewInnerText.innerHTML += {deck.deckName}; const deckViewOuter: HTMLElement = deckViewSelf.createDiv("tree-item-flair-outer"); - let deckStats = this.reviewSequencer.getDeckStats(deck.getTopicPath()); + const deckStats = this.reviewSequencer.getDeckStats(deck.getTopicPath()); deckViewOuter.innerHTML += ( { - let currentQ: Question = this.reviewSequencer.currentQuestion; + const currentQ: Question = this.reviewSequencer.currentQuestion; // Just the question/answer text; without any preceding topic tag - let textPrompt = currentQ.questionText.actualQuestion; + const textPrompt = currentQ.questionText.actualQuestion; const editModal = FlashcardEditModal.Prompt(this.app, textPrompt); editModal @@ -430,7 +419,7 @@ export class FlashcardModal extends Modal { this.flashcardView.empty(); } - let wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper( + const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper( this.app, this.plugin, this.currentNote.filePath, @@ -456,7 +445,7 @@ export class FlashcardModal extends Modal { } private async showCurrentCard(): Promise { - let deck: Deck = this.reviewSequencer.currentDeck; + const deck: Deck = this.reviewSequencer.currentDeck; this.responseDiv.style.display = "none"; this.resetButton.disabled = true; @@ -466,7 +455,7 @@ export class FlashcardModal extends Modal { this.flashcardView.empty(); this.mode = FlashcardModalMode.Front; - let wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper( + const wrapper: RenderMarkdownWrapper = new RenderMarkdownWrapper( this.app, this.plugin, this.currentNote.filePath, @@ -502,7 +491,7 @@ export class FlashcardModal extends Modal { } private formatQuestionContextText(questionContext: string[]): string { - let result = `${this.currentNote.file.basename} > ${questionContext.join(" > ")}`; + const result = `${this.currentNote.file.basename} > ${questionContext.join(" > ")}`; return result; } @@ -511,7 +500,7 @@ export class FlashcardModal extends Modal { buttonName: string, reviewResponse: ReviewResponse, ) { - var schedule: CardScheduleInfo = this.reviewSequencer.determineCardSchedule( + const schedule: CardScheduleInfo = this.reviewSequencer.determineCardSchedule( reviewResponse, this.currentCard, ); diff --git a/src/gui/flashcards-edit-modal.ts b/src/gui/flashcards-edit-modal.ts index efc2743a..1330c811 100644 --- a/src/gui/flashcards-edit-modal.ts +++ b/src/gui/flashcards-edit-modal.ts @@ -1,6 +1,5 @@ import { App, ButtonComponent, Modal, TextAreaComponent } from "obsidian"; import { t } from "src/lang/helpers"; -import SRPlugin from "src/main"; // from https://github.com/chhoumann/quickadd/blob/bce0b4cdac44b867854d6233796e3406dfd163c6/src/gui/GenericInputPrompt/GenericInputPrompt.ts#L5 export class FlashcardEditModal extends Modal { diff --git a/src/main.ts b/src/main.ts index de9143c0..80c4c6d8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,4 @@ -import { - Notice, - Plugin, - TAbstractFile, - TFile, - HeadingCache, - getAllTags, - FrontMatterCache, -} from "obsidian"; +import { Notice, Plugin, TAbstractFile, TFile, getAllTags, FrontMatterCache } from "obsidian"; import * as graph from "pagerank.js"; import { SRSettingTab, SRSettings, DEFAULT_SETTINGS } from "src/settings"; @@ -14,13 +6,7 @@ import { FlashcardModal } from "src/gui/flashcard-modal"; import { StatsModal } from "src/gui/stats-modal"; import { ReviewQueueListView, REVIEW_QUEUE_VIEW_TYPE } from "src/gui/sidebar"; import { ReviewResponse, schedule } from "src/scheduling"; -import { - YAML_FRONT_MATTER_REGEX, - SCHEDULING_INFO_REGEX, - LEGACY_SCHEDULING_EXTRACTOR, - MULTI_SCHEDULING_EXTRACTOR, -} from "src/constants"; -import { escapeRegexString, cyrb53 } from "src/util/utils"; +import { YAML_FRONT_MATTER_REGEX, SCHEDULING_INFO_REGEX } from "src/constants"; import { ReviewDeck, ReviewDeckSelectionModal } from "src/ReviewDeck"; import { t } from "src/lang/helpers"; import { appIcon } from "src/icons/appicon"; @@ -46,7 +32,7 @@ import { NoteFileLoader } from "./NoteFileLoader"; import { ISRFile, SrTFile as SrTFile } from "./SRFile"; import { NoteEaseCalculator } from "./NoteEaseCalculator"; import { DeckTreeStatsCalculator } from "./DeckTreeStatsCalculator"; -import { INoteEaseList, NoteEaseList } from "./NoteEaseList"; +import { NoteEaseList } from "./NoteEaseList"; import { QuestionPostponementList } from "./QuestionPostponementList"; interface PluginData { @@ -224,7 +210,7 @@ export default class SRPlugin extends Plugin { id: "srs-cram-flashcards", name: t("CRAM_ALL_CARDS"), callback: async () => { - await this.sync(true); + await this.sync(); this.openFlashcardModal(this.deckTree, this.deckTree, FlashcardReviewMode.Cram); }, }); @@ -283,11 +269,11 @@ export default class SRPlugin extends Plugin { reviewMode: FlashcardReviewMode, ): Promise { const topicPath: TopicPath = this.findTopicPath(this.createSrTFile(noteFile)); - let note: Note = await this.loadNote(noteFile, topicPath); + const note: Note = await this.loadNote(noteFile, topicPath); - let deckTree = new Deck("root", null); + const deckTree = new Deck("root", null); note.appendCardsToDeck(deckTree); - let remainingDeckTree = DeckTreeFilter.filterForRemainingCards( + const remainingDeckTree = DeckTreeFilter.filterForRemainingCards( this.questionPostponementList, deckTree, reviewMode, @@ -300,12 +286,12 @@ export default class SRPlugin extends Plugin { remainingDeckTree: Deck, reviewMode: FlashcardReviewMode, ): void { - let deckIterator = SRPlugin.createDeckTreeIterator(this.data.settings); - let cardScheduleCalculator = new CardScheduleCalculator( + const deckIterator = SRPlugin.createDeckTreeIterator(this.data.settings); + const cardScheduleCalculator = new CardScheduleCalculator( this.data.settings, this.easeByPath, ); - let reviewSequencer: IFlashcardReviewSequencer = new FlashcardReviewSequencer( + const reviewSequencer: IFlashcardReviewSequencer = new FlashcardReviewSequencer( reviewMode, deckIterator, this.data.settings, @@ -318,7 +304,7 @@ export default class SRPlugin extends Plugin { } private static createDeckTreeIterator(settings: SRSettings): IDeckTreeIterator { - let iteratorOrder: IIteratorOrder = { + const iteratorOrder: IIteratorOrder = { deckOrder: OrderMethod.Sequential, cardListOrder: CardListOrder.DueFirst, cardOrder: settings.randomizeCardOrder ? OrderMethod.Random : OrderMethod.Sequential, @@ -326,7 +312,7 @@ export default class SRPlugin extends Plugin { return new DeckTreeIterator(iteratorOrder, IteratorDeckSource.UpdatedByIterator); } - async sync(ignoreStats = false): Promise { + async sync(): Promise { if (this.syncLock) { return; } @@ -342,7 +328,7 @@ export default class SRPlugin extends Plugin { this.reviewDecks = {}; // reset flashcards stuff - let fullDeckTree = new Deck("root", null); + const fullDeckTree = new Deck("root", null); const now = window.moment(Date.now()); const todayDate: string = now.format("YYYY-MM-DD"); @@ -384,8 +370,8 @@ export default class SRPlugin extends Plugin { const topicPath: TopicPath = this.findTopicPath(this.createSrTFile(noteFile)); if (topicPath.hasPath) { - let note: Note = await this.loadNote(noteFile, topicPath); - let flashcardsInNoteAvgEase: number = NoteEaseCalculator.Calculate( + const note: Note = await this.loadNote(noteFile, topicPath); + const flashcardsInNoteAvgEase: number = NoteEaseCalculator.Calculate( note, this.data.settings, ); @@ -444,7 +430,7 @@ export default class SRPlugin extends Plugin { } } - var ease; + let ease: number; if (this.easeByPath.hasEaseForPath(noteFile.path)) { ease = (this.easeByPath.getEaseByPath(noteFile.path) + frontmatter["sr-ease"]) / 2; } else { @@ -477,7 +463,7 @@ export default class SRPlugin extends Plugin { this.deckTree, FlashcardReviewMode.Review, ); - let calc: DeckTreeStatsCalculator = new DeckTreeStatsCalculator(); + const calc: DeckTreeStatsCalculator = new DeckTreeStatsCalculator(); this.cardStats = calc.calculate(this.deckTree); if (this.data.settings.showDebugMessages) { @@ -510,8 +496,8 @@ export default class SRPlugin extends Plugin { } async loadNote(noteFile: TFile, topicPath: TopicPath): Promise { - let loader: NoteFileLoader = new NoteFileLoader(this.data.settings); - let note: Note = await loader.load(this.createSrTFile(noteFile), topicPath); + const loader: NoteFileLoader = new NoteFileLoader(this.data.settings); + const note: Note = await loader.load(this.createSrTFile(noteFile), topicPath); if (note.hasChanged) note.writeNoteFile(this.data.settings); return note; } @@ -643,7 +629,7 @@ export default class SRPlugin extends Plugin { if (this.data.settings.burySiblingCards) { const topicPath: TopicPath = this.findTopicPath(this.createSrTFile(note)); - let noteX: Note = await this.loadNote(note, topicPath); + const noteX: Note = await this.loadNote(note, topicPath); for (const question of noteX.questionList) { this.data.buryList.push(question.questionText.textHash); } diff --git a/src/note.ts b/src/note.ts index f092dd60..46ae5cb8 100644 --- a/src/note.ts +++ b/src/note.ts @@ -1,5 +1,3 @@ -import { HeadingCache } from "obsidian"; -import { TopicPath } from "./TopicPath"; import { SRSettings } from "./settings"; import { Deck } from "./Deck"; import { Question } from "./Question"; @@ -34,7 +32,7 @@ export class Note { debugLogToConsole(desc: string = "") { let str: string = `Note: ${desc}: ${this.questionList.length} questions\r\n`; for (let i = 0; i < this.questionList.length; i++) { - let q: Question = this.questionList[i]; + const q: Question = this.questionList[i]; str += `[${i}]: ${q.questionType}: ${q.lineNo}: ${q.topicPath?.path}: ${q.questionText.original}\r\n`; } console.debug(str); diff --git a/src/scheduling.ts b/src/scheduling.ts index fb4059e8..677f107d 100644 --- a/src/scheduling.ts +++ b/src/scheduling.ts @@ -1,5 +1,3 @@ -import { TFile } from "obsidian"; - import { SRSettings } from "src/settings"; import { t } from "src/lang/helpers"; diff --git a/src/stats.ts b/src/stats.ts index 37e5228e..edadf19d 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -1,5 +1,4 @@ import { ValueCountDict } from "./util/NumberCountDict"; -import { getKeysPreserveType, getTypedObjectEntries } from "./util/utils"; export class Stats { eases: ValueCountDict = new ValueCountDict(); diff --git a/src/util/DateProvider.ts b/src/util/DateProvider.ts index 8c1479ab..1674c60b 100644 --- a/src/util/DateProvider.ts +++ b/src/util/DateProvider.ts @@ -34,7 +34,7 @@ export class DateUtil { } } -export var globalDateProvider: IDateProvider = new LiveDateProvider(); +export let globalDateProvider: IDateProvider = new LiveDateProvider(); export function setupStaticDateProvider_20230906() { globalDateProvider = StaticDateProvider.fromDateStr("2023-09-06"); diff --git a/src/util/MultiLineTextFinder.ts b/src/util/MultiLineTextFinder.ts index 55b29a9b..bd5a43a9 100644 --- a/src/util/MultiLineTextFinder.ts +++ b/src/util/MultiLineTextFinder.ts @@ -10,12 +10,12 @@ export class MultiLineTextFinder { if (sourceText.includes(searchText)) { result = sourceText.replace(searchText, replacementText); } else { - let sourceTextArray = splitTextIntoLineArray(sourceText); - let searchTextArray = splitTextIntoLineArray(searchText); - let lineNo: number = MultiLineTextFinder.find(sourceTextArray, searchTextArray); + const sourceTextArray = splitTextIntoLineArray(sourceText); + const searchTextArray = splitTextIntoLineArray(searchText); + const lineNo: number = MultiLineTextFinder.find(sourceTextArray, searchTextArray); if (lineNo) { - let replacementTextArray = splitTextIntoLineArray(replacementText); - let linesToRemove: number = searchTextArray.length; + const replacementTextArray = splitTextIntoLineArray(replacementText); + const linesToRemove: number = searchTextArray.length; sourceTextArray.splice(lineNo, linesToRemove, ...replacementTextArray); result = sourceTextArray.join("\n"); } @@ -26,10 +26,10 @@ export class MultiLineTextFinder { static find(sourceText: string[], searchText: string[]): number | null { let result: number = null; let searchIdx: number = 0; - let maxSearchIdx: number = searchText.length - 1; + const maxSearchIdx: number = searchText.length - 1; for (let sourceIdx = 0; sourceIdx < sourceText.length; sourceIdx++) { - let sourceLine: string = sourceText[sourceIdx].trim(); - let searchLine: string = searchText[searchIdx].trim(); + const sourceLine: string = sourceText[sourceIdx].trim(); + const searchLine: string = searchText[searchIdx].trim(); if (searchLine == sourceLine) { if (searchIdx == maxSearchIdx) { result = sourceIdx - searchIdx; diff --git a/src/util/NumberCountDict.ts b/src/util/NumberCountDict.ts index 82b1ad4a..5d6d0cc7 100644 --- a/src/util/NumberCountDict.ts +++ b/src/util/NumberCountDict.ts @@ -21,7 +21,7 @@ export class ValueCountDict { } getTotalOfValueMultiplyCount(): number { - let v: number = + const v: number = getTypedObjectEntries(this.dict) .map(([value, count]) => value * count) .reduce((a, b) => a + b, 0) || 0; diff --git a/src/util/RandomNumberProvider.ts b/src/util/RandomNumberProvider.ts index 9db33d7e..aa14f86d 100644 --- a/src/util/RandomNumberProvider.ts +++ b/src/util/RandomNumberProvider.ts @@ -4,29 +4,11 @@ export interface IRandomNumberProvider { export class RandomNumberProvider implements IRandomNumberProvider { getInteger(lowerBound: number, upperBound: number): number { - let range = upperBound - lowerBound + 1; + const range = upperBound - lowerBound + 1; return Math.floor(Math.random() * range) + lowerBound; } } -export class WeightedRandomNumber { - private provider: IRandomNumberProvider; - - constructor(provider: IRandomNumberProvider) { - this.provider = provider; - } - - // - // weights is a dictionary: - // first number - a value that can be returned - // second number - the weight that influences the probability of the - // first number being returned - // - getRandomValues(weights: Record): number { - throw ""; - } -} - export class StaticRandomNumberProvider implements IRandomNumberProvider { expectedLowerBound: number; expectedUpperBound: number; @@ -39,8 +21,8 @@ export class StaticRandomNumberProvider implements IRandomNumberProvider { } } -export var globalRandomNumberProvider: IRandomNumberProvider = new RandomNumberProvider(); -export var staticRandomNumberProvider: StaticRandomNumberProvider = +export let globalRandomNumberProvider: IRandomNumberProvider = new RandomNumberProvider(); +export const staticRandomNumberProvider: StaticRandomNumberProvider = new StaticRandomNumberProvider(); export interface IStaticRandom { diff --git a/src/util/utils.ts b/src/util/utils.ts index 35d7509d..1debe429 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -59,11 +59,6 @@ export function cyrb53(str: string, seed = 0): string { return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16); } -// ✅ Defining a reusable function -function padTo2Digits(num: number) { - return num.toString().padStart(2, "0"); -} - export function ticksFromDate(year: number, month: number, day: number): number { return moment({ year, month, day }).utc().valueOf(); } @@ -76,7 +71,7 @@ export function formatDate_YYYY_MM_DD(ticks: Moment): string { export function getAllTagsFromText(text: string): string[] { const tagRegex = /#[^\s#]+/gi; - let result: RegExpMatchArray = text.match(tagRegex); + const result: RegExpMatchArray = text.match(tagRegex); if (!result) return []; return result; }