28 lines
743 B
JavaScript
28 lines
743 B
JavaScript
|
|
// 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 };
|
||
|
|
}
|
||
|
|
}
|