Unify records model and add notification center
This commit is contained in:
@@ -1,47 +1,3 @@
|
||||
export function prettyLabel(value) {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const withSpaces = String(value)
|
||||
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
||||
.replace(/[_-]+/g, ' ')
|
||||
.trim();
|
||||
|
||||
return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
|
||||
}
|
||||
|
||||
export function inferColumnType(value) {
|
||||
if (typeof value === 'boolean') {
|
||||
return 'boolean';
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return 'number';
|
||||
}
|
||||
if (typeof value === 'string' && /^-?\d+(\.\d+)?$/.test(value)) {
|
||||
return 'number';
|
||||
}
|
||||
return 'text';
|
||||
}
|
||||
|
||||
export function normalizeColumnDefinition(field, columnDefinition = {}, sampleValue) {
|
||||
return {
|
||||
field,
|
||||
id: field,
|
||||
label: columnDefinition.label || columnDefinition.display_name || prettyLabel(field),
|
||||
sortable: columnDefinition.sortable ?? true,
|
||||
filterable: columnDefinition.filterable ?? true,
|
||||
align: columnDefinition.align || (inferColumnType(sampleValue) === 'number' ? 'right' : 'left'),
|
||||
width: columnDefinition.width ?? null,
|
||||
type: columnDefinition.type || inferColumnType(sampleValue),
|
||||
format: columnDefinition.format || null,
|
||||
renderer: columnDefinition.renderer || columnDefinition.render || null,
|
||||
currency: columnDefinition.currency || 'USD',
|
||||
priority: columnDefinition.priority || null,
|
||||
alwaysVisible: columnDefinition.alwaysVisible ?? false
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeColumnDefinitionsInput(input = {}) {
|
||||
if (Array.isArray(input)) {
|
||||
return Object.fromEntries(
|
||||
@@ -114,35 +70,6 @@ export function normalizeColumnsArray(input = []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
export function compareValues(left, right, direction = 'asc') {
|
||||
if (left === right) {
|
||||
return 0;
|
||||
}
|
||||
if (left === null || left === undefined || left === '') {
|
||||
return 1;
|
||||
}
|
||||
if (right === null || right === undefined || right === '') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const leftNumber = Number(left);
|
||||
const rightNumber = Number(right);
|
||||
const bothNumeric = !Number.isNaN(leftNumber) && !Number.isNaN(rightNumber);
|
||||
const result = bothNumeric
|
||||
? leftNumber - rightNumber
|
||||
: String(left).localeCompare(String(right), undefined, { sensitivity: 'base' });
|
||||
|
||||
return direction === 'desc' ? -result : result;
|
||||
}
|
||||
|
||||
export function getColumnKeysFromRows(rows = []) {
|
||||
const fields = new Set();
|
||||
for (const row of rows) {
|
||||
Object.keys(row || {}).forEach((field) => fields.add(field));
|
||||
}
|
||||
return Array.from(fields);
|
||||
}
|
||||
|
||||
export function resolveCellValue(row, column) {
|
||||
return row?.[column.field];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user