Simplify initialization of editable attributes for clarity

Instead of cramming all the steps for correct deep cloning, I split the implementation into 4 distinct steps:
- define they keys to exclude
- filtering the keys
- reduce filtered keys into an object
- returning a writable deep clone
This commit is contained in:
Safwan Parkar
2023-08-10 15:53:16 +04:00
parent 399ec90108
commit 5ad034973e
@@ -22,25 +22,29 @@
const documentId = $page.params.document;
const editing = true;
const work = writable(
deepClone(
Object.keys($doc)
.filter((key) => {
return ![
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
].includes(key);
})
.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {}) as Models.Document
)
);
function initWork() {
const prohibitedKeys = [
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
];
const filteredKeys = Object.keys($doc).filter((key) => {
return !prohibitedKeys.includes(key);
});
const result = filteredKeys.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {});
return writable(deepClone(result as Models.Document));
}
const work = initWork();
async function updateData() {
try {