-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathutils.ts
More file actions
89 lines (79 loc) · 1.77 KB
/
Copy pathutils.ts
File metadata and controls
89 lines (79 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
export interface Column {
id: string
type: string
label?: string
enum?: string
isArray?: boolean
isEditable?: boolean
isNullable?: boolean
maxLength?: number | null
precision?: number | null
scale?: number | null
unique?: string
primaryKey?: string
defaultValue?: string | null
foreign?: {
name: string
schema: string
table: string
column: string
onDelete?: string
onUpdate?: string
}
references?: {
name: string
schema: string
table: string
column: string
isUnique?: boolean
}[]
}
export const DEFAULT_ROW_HEIGHT = 32
export const DEFAULT_COLUMN_WIDTH = 240
function prepareValue(value: unknown) {
if (value instanceof Date)
return value.toISOString()
return value
}
export function getEditableValue({
value,
oneLine,
column,
}: {
value: unknown
oneLine: boolean
column: Column
}) {
const _value = prepareValue(value)
if (typeof _value === 'object' && _value !== null) {
return oneLine
? JSON.stringify(_value).replaceAll('\n', ' ')
: JSON.stringify(_value, null, 2)
}
if (column.type === 'boolean' && !column.isArray && _value === null)
return 'false'
return oneLine
? String(_value ?? '').replaceAll('\n', ' ')
: String(_value ?? '')
}
export function getDisplayValue({
value,
size,
column,
}: {
value: unknown
size: number
column: Column
}) {
if (value === null)
return 'null'
if (value === '')
return 'empty'
/*
If value has a lot of symbols that don't fit in the cell,
we truncate it to avoid performance issues.
Used 6 as a multiplier because 1 symbol takes ~6px width
+ 5 to make sure there are extra symbols for ellipsis
*/
return getEditableValue({ value, oneLine: true, column }).slice(0, (size / 6) + 5)
}