diff options
Diffstat (limited to 'desktop/modules/notes/Notes.js')
| -rw-r--r-- | desktop/modules/notes/Notes.js | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/desktop/modules/notes/Notes.js b/desktop/modules/notes/Notes.js index 76c8f95..6bc8c88 100644 --- a/desktop/modules/notes/Notes.js +++ b/desktop/modules/notes/Notes.js @@ -22,17 +22,28 @@ function notesOf(db) { return (db && Array.isArray(db.notes)) ? db.notes : []; } -// db.json, whole. Anything that is not an object carrying a notes array reads -// as an empty store, so a truncated or hand-edited file shows "no notes" -// rather than failing to load the page. -function parse(text) { +// The db object, or null when the text cannot be read as one. An empty or +// missing file is an empty store, not an error. Anything else that does not +// parse, or parses to something without a notes array, is null: a writer must +// refuse on null rather than replace a file it did not understand, which is +// what a conflicted db.json (merge markers from an autostash pop) is. +function valid(text) { + const t = String(text === undefined || text === null ? "" : text); + if (t.trim() === "") return { notes: [] }; let v = null; - try { v = JSON.parse(text); } catch (e) { v = null; } + try { v = JSON.parse(t); } catch (e) { v = null; } if (!v || typeof v !== "object" || Array.isArray(v) || !Array.isArray(v.notes)) - return { notes: [] }; + return null; return v; } +// db.json for display: the object, or an empty store when nothing readable is +// there, so a truncated or hand-edited file shows "no notes" rather than +// failing to load the page. +function parse(text) { + return valid(text) || { notes: [] }; +} + // Pretty, 2-space, trailing newline. notes.sh reads db.json through jq, which // does not care about the layout. function stringify(db) { |
