2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
31 lines
632 B
JavaScript
31 lines
632 B
JavaScript
function parseStoredJson(json, isValid, fallback) {
|
|
if (typeof json !== 'string') return fallback;
|
|
try {
|
|
const value = JSON.parse(json);
|
|
return isValid(value) ? value : fallback;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function parseStoredArray(json) {
|
|
return parseStoredJson(json, Array.isArray, []);
|
|
}
|
|
|
|
export function parseStoredObject(json) {
|
|
return parseStoredJson(
|
|
json,
|
|
(value) =>
|
|
value !== null && typeof value === 'object' && !Array.isArray(value),
|
|
{},
|
|
);
|
|
}
|
|
|
|
export function readStorageValue(key) {
|
|
try {
|
|
return localStorage.getItem(key);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|