From 4fbd183af0c774daddbcaed29317f61f797c07fc Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Sat, 23 Jul 2022 15:06:08 +0800 Subject: [PATCH] feat(gui-v2): parserHelpers --- .../nc-gui-v2/utils/parsers/parserHelpers.ts | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 packages/nc-gui-v2/utils/parsers/parserHelpers.ts diff --git a/packages/nc-gui-v2/utils/parsers/parserHelpers.ts b/packages/nc-gui-v2/utils/parsers/parserHelpers.ts new file mode 100644 index 0000000000..e8fef2ebdf --- /dev/null +++ b/packages/nc-gui-v2/utils/parsers/parserHelpers.ts @@ -0,0 +1,95 @@ +import { UITypes, ColumnType } from 'nocodb-sdk' +import { isValidURL } from '~/utils/urlUtils' +import { isEmail } from '~/utils/validation' + +const booleanOptions = [ + { checked: true, unchecked: false }, + { 'x': true, '': false }, + { yes: true, no: false }, + { y: true, n: false }, + { 1: true, 0: false }, + { '[x]': true, '[]': false, '[ ]': false }, + { '☑': true, '': false }, + { '✅': true, '': false }, + { '✓': true, '': false }, + { '✔': true, '': false }, + { enabled: true, disabled: false }, + { on: true, off: false }, + { 'done': true, '': false }, + { true: true, false: false }, +] +const aggBooleanOptions: any = booleanOptions.reduce((obj, o) => ({ ...obj, ...o }), {}) + +const getColVal = (row: any, col = null) => { + return row && col ? row[col] : row +} + +export const isCheckboxType: any = (values: [], col = null) => { + let options = booleanOptions + for (let i = 0; i < values.length; i++) { + const val = getColVal(values[i], col) + + if (val === null || val === undefined || val.toString().trim() === '') { + continue + } + + options = options.filter((v) => val in v) + if (!options.length) { + return false + } + } + return options +} +export const getCheckboxValue = (value: number) => { + return value && aggBooleanOptions[value] +} + +export const isMultiLineTextType = (values: [], col = null) => { + return values.some( + (r) => (getColVal(r, col) || '').toString().match(/[\r\n]/) || (getColVal(r, col) || '').toString().length > 255, + ) +} + +export const extractMultiOrSingleSelectProps = (colData: []) => { + const colProps: any = {} + if (colData.some((v: any) => v && (v || '').toString().includes(','))) { + let flattenedVals = colData.flatMap((v: any) => + v + ? v + .toString() + .trim() + .split(/\s*,\s*/) + : [], + ) + const uniqueVals = (flattenedVals = flattenedVals.filter( + (v, i, arr) => i === arr.findIndex((v1) => v.toLowerCase() === v1.toLowerCase()), + )) + if (flattenedVals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(flattenedVals.length / 2)) { + colProps.uidt = UITypes.MultiSelect + colProps.dtxp = `'${uniqueVals.join("','")}'` + } + } else { + const uniqueVals = colData + .map((v: any) => (v || '').toString().trim()) + .filter((v, i, arr) => i === arr.findIndex((v1) => v.toLowerCase() === v1.toLowerCase())) + if (colData.length > uniqueVals.length && uniqueVals.length <= Math.ceil(colData.length / 2)) { + colProps.uidt = UITypes.SingleSelect + colProps.dtxp = `'${uniqueVals.join("','")}'` + } + } + return colProps +} + +export const isDecimalType = (colData: []) => + colData.some((v: any) => { + return v && parseInt(v) !== +v + }) + +export const isEmailType = (colData: []) => + !colData.some((v: any) => { + return v && !isEmail(v) + }) +export const isUrlType = (colData: []) => + !colData.some((v: any) => { + return v && !isValidURL(v) + })