Repository navigation
Refactor app.js to separate concerns by extracting store.js #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8d4ebf
refactor: Extract data layer to store.js from app.js to follow SRP
google-labs-jules[bot] 636439e
fix(store): normalize DB structure in setDB and load
fulltomo 3e1ba16
fix(store): filter out non-record items from DB arrays in normalizeDB
fulltomo 497ad72
fix: Add normalizeDB to store.js and related regression tests
google-labs-jules[bot] d6e7d64
test(store): add regression test for malformed field fallback in norm…
fulltomo f83f058
fix(store): remove unused DAY import and expand normalizeDB regressio…
fulltomo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:05] "GET / HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:05] "GET /app.css HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:05] "GET /core.js HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:05] "GET /app.js HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:05] "GET /store.js HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /sw.js HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /index.html HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /manifest.webmanifest HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /icons/icon.svg HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /icons/icon-192.png HTTP/1.1" 200 - | ||
| 127.0.0.1 - - [13/Aug/2026 15:20:06] "GET /icons/icon-512.png HTTP/1.1" 200 - |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { schedule } from './core.js'; | ||
|
|
||
| const uid = () => Math.random().toString(36).slice(2, 10); | ||
| const dayKey = t => { const d = new Date(t); return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`; }; | ||
|
|
||
| export const KEY = 'core-recall-v1'; | ||
| export let db = null; | ||
|
|
||
| export function normalizeDB(value) { | ||
| if (!value || typeof value !== 'object' || Array.isArray(value)) { | ||
| return blank(); | ||
| } | ||
| const b = blank(); | ||
| const isRecord = x => x && typeof x === 'object' && !Array.isArray(x); | ||
|
|
||
| const decks = Array.isArray(value.decks) ? value.decks.filter(isRecord) : b.decks; | ||
| const cards = Array.isArray(value.cards) ? value.cards.filter(isRecord) : b.cards; | ||
| const log = Array.isArray(value.log) ? value.log.filter(isRecord) : b.log; | ||
|
|
||
| return { | ||
| decks, | ||
| cards, | ||
| log, | ||
| lastBackup: typeof value.lastBackup === 'number' ? value.lastBackup : b.lastBackup, | ||
| settings: Object.assign(b.settings, isRecord(value.settings) ? value.settings : {}) | ||
| }; | ||
| } | ||
|
|
||
| export function setDB(newDB) { | ||
| db = normalizeDB(newDB); | ||
| } | ||
|
|
||
| export function blank() { | ||
| return { decks: [], cards: [], log: [], lastBackup: Date.now(), | ||
| settings: { new: 20, max: 200, ret: 90, theme: 'light' } }; | ||
| } | ||
|
|
||
| export function load() { | ||
| let loaded; | ||
| try { loaded = JSON.parse(localStorage.getItem(KEY)); } catch {} | ||
| db = normalizeDB(loaded || seed()); | ||
| save(); | ||
| } | ||
|
|
||
| export function save() { | ||
| localStorage.setItem(KEY, JSON.stringify(db)); | ||
| } | ||
|
|
||
| export function newCard(deck, front, back) { | ||
| return { id: uid(), deck, front, back, due: Date.now(), s: 0, d: 0, reps: 0, lapses: 0, last: 0, state: 0 }; | ||
| } | ||
|
|
||
| export function seed() { | ||
| const d = blank(); | ||
| const deck = { id: uid(), name: '英単語', created: Date.now() }; | ||
| d.decks.push(deck); | ||
| [['ubiquitous', '遍在する、どこにでもある'], ['ephemeral', 'つかの間の、短命な'], | ||
| ['meticulous', '几帳面な、細部にこだわる'], ['candid', '率直な、ありのままの'], | ||
| ['prudent', '慎重な、思慮深い'], ['resilient', '回復力のある、しなやかな'], | ||
| ['ambiguous', '曖昧な、多義的な'], ['tenacious', '粘り強い、執拗な']] | ||
| .forEach(([f, b]) => d.cards.push(newCard(deck.id, f, b))); | ||
| return d; | ||
| } | ||
|
|
||
| export const deckOf = id => db.decks.find(d => d.id === id); | ||
| export const cardsOf = id => db.cards.filter(c => c.deck === id); | ||
| export const todayLog = () => { const k = dayKey(Date.now()); return db.log.filter(l => dayKey(l.t) === k); }; | ||
|
|
||
| export function counts(deckId) { | ||
| const now = Date.now(), c = { n: 0, l: 0, d: 0 }; | ||
| for (const card of cardsOf(deckId)) { | ||
| if (!card.reps) c.n++; | ||
| else if (card.due <= now) (card.state === 2 ? c.l++ : c.d++); | ||
| } | ||
| return c; | ||
| } | ||
|
|
||
| export function buildQueue(deckId) { | ||
| const now = Date.now(), t = todayLog(); | ||
| const newLeft = Math.max(0, db.settings.new - t.filter(l => l.n).length); | ||
| const revLeft = Math.max(0, db.settings.max - t.filter(l => !l.n).length); | ||
| const all = cardsOf(deckId); | ||
| const due = all.filter(c => c.reps && c.due <= now).sort((a, b) => a.due - b.due).slice(0, revLeft); | ||
| const fresh = all.filter(c => !c.reps).slice(0, newLeft); | ||
| const q = due.slice(); | ||
| if (fresh.length) { | ||
| const step = Math.max(1, Math.floor((q.length + fresh.length) / fresh.length)); | ||
| fresh.forEach((c, i) => q.splice(Math.min(q.length, i * step + i), 0, c)); | ||
| } | ||
| if (deckOf(deckId)?.shuffle) for (let i = q.length - 1; i > 0; i--) { | ||
| const j = Math.floor(Math.random() * (i + 1)); | ||
| [q[i], q[j]] = [q[j], q[i]]; | ||
| } | ||
| return q; | ||
| } | ||
|
|
||
| export function plan(card, g, now) { | ||
| return schedule(card, g, now, db.settings.ret / 100); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.