Files
nocodb/packages/nc-gui/composables/useMultiSelect/convertCellData.ts
Pranav C 5c36a3e9d6 chore(gui): lint
Signed-off-by: Pranav C <pranavxc@gmail.com>
2022-12-01 11:08:08 +05:30

39 lines
1.1 KiB
TypeScript

import { UITypes } from 'nocodb-sdk'
export default function convertCellData(args: { from: UITypes; to: UITypes; value: any }) {
const { from, to, value } = args
if (from === to && from !== UITypes.Attachment) {
return value
}
switch (to) {
case UITypes.Number:
return Number(value)
case UITypes.Checkbox:
return Boolean(value)
case UITypes.Date:
return new Date(value)
case UITypes.Attachment: {
let parsedVal
try {
parsedVal = typeof value === 'string' ? JSON.parse(value) : value
parsedVal = Array.isArray(parsedVal) ? parsedVal : [parsedVal]
} catch (e) {
throw new Error('Invalid attachment data')
}
if (parsedVal.some((v: any) => v && !(v.url || v.data))) {
throw new Error('Invalid attachment data')
}
return JSON.stringify(parsedVal)
}
case UITypes.LinkToAnotherRecord:
case UITypes.Lookup:
case UITypes.Rollup:
case UITypes.Formula:
case UITypes.QrCode:
throw new Error(`Unsupported conversion from ${from} to ${to}`)
default:
return value
}
}