webtools/site/queued-pastes/prefs.mjs

28 lines
743 B
JavaScript
Raw Normal View History

2026-09-16 21:52:03 +10:00
// Serialization for the Queued Pastes preferences stored in localStorage
// under the key 'queued-paste-prefs'. Pure functions so they can be unit
// tested under Node (see prefs.test.mjs).
const DEFAULTS = { newLines: false, advanceFocus: false };
export function serializePrefs(prefs) {
return JSON.stringify({
'new-lines': Boolean(prefs.newLines),
'advance-focus': Boolean(prefs.advanceFocus),
});
}
export function parsePrefs(json) {
try {
const obj = JSON.parse(json);
if (obj === null || typeof obj !== 'object') {
return { ...DEFAULTS };
}
return {
newLines: obj['new-lines'] === true,
advanceFocus: obj['advance-focus'] === true,
};
} catch {
return { ...DEFAULTS };
}
}