mobx-persist-store
Version:
Mobx Persist Store
34 lines (33 loc) • 1.1 kB
JavaScript
import { mpsConfig } from './configurePersistable';
import { consoleDebug, isObject } from './utils';
const isSerializableProperty = (obj) => {
const keys = ['key', 'serialize', 'deserialize'];
if (!isObject(obj)) {
consoleDebug(!!mpsConfig.debugMode, 'passed value is not an object', { obj });
return false;
}
return keys.every((key) => {
if (obj.hasOwnProperty(key) && typeof key !== 'undefined') {
return true;
}
consoleDebug(!!mpsConfig.debugMode, `${String(key)} not found in SerializableProperty`, { key, obj });
return false;
});
};
export const makeSerializableProperties = (properties) => {
return properties.reduce((acc, curr) => {
if (typeof curr === 'string') {
acc.push({
key: curr,
serialize: (value) => value,
deserialize: (value) => value,
});
return acc;
}
if (isSerializableProperty(curr)) {
acc.push(curr);
return acc;
}
return acc;
}, []);
};