53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
function canonical(value: unknown): unknown {
|
|
if (Array.isArray(value)) return value.map(canonical);
|
|
if (value !== null && typeof value === 'object') {
|
|
return Object.fromEntries(
|
|
Object.entries(value)
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([key, item]) => [key, canonical(item)]),
|
|
);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function preparation(data: Record<string, unknown>): unknown {
|
|
const { operation_key: _operationKey, ...fields } = data;
|
|
return canonical({
|
|
...fields,
|
|
items: Array.isArray(fields.items)
|
|
? fields.items.map((item) => {
|
|
if (
|
|
item !== null &&
|
|
typeof item === 'object' &&
|
|
!Array.isArray(item)
|
|
) {
|
|
const {
|
|
key: _rowKey,
|
|
procurement_category,
|
|
category_reason,
|
|
...values
|
|
} = item;
|
|
return {
|
|
...values,
|
|
...(procurement_category
|
|
? { procurement_category }
|
|
: {}),
|
|
...(category_reason ? { category_reason } : {}),
|
|
};
|
|
}
|
|
return item;
|
|
})
|
|
: fields.items,
|
|
});
|
|
}
|
|
|
|
export function hasPreparationChanges(
|
|
initial: Record<string, unknown>,
|
|
current: Record<string, unknown>,
|
|
): boolean {
|
|
return (
|
|
JSON.stringify(preparation(initial)) !==
|
|
JSON.stringify(preparation(current))
|
|
);
|
|
}
|