(options: {
store: Store,
table: SQLiteTable,
tableLabel: string,
entries: T[],
hiddenProperties?: (string)[],
propertyFormatters?: Record<string, (entry: any) => string>,
color?: Color,
// defaults to "queueId"
queueIdProperty?: string,
} & (
{ entryLabelProperty: string } | { entryLabel: string }
))
| 115 | const GLOBAL_HIDDEN_PROPERTIES = ["id", "guildId", "queueId", "isRole"]; |
| 116 | |
| 117 | export function describeTable<T extends object>(options: { |
| 118 | store: Store, |
| 119 | table: SQLiteTable, |
| 120 | tableLabel: string, |
| 121 | entries: T[], |
| 122 | hiddenProperties?: (string)[], |
| 123 | propertyFormatters?: Record<string, (entry: any) => string>, |
| 124 | color?: Color, |
| 125 | // defaults to "queueId" |
| 126 | queueIdProperty?: string, |
| 127 | } & ( |
| 128 | { entryLabelProperty: string } | { entryLabel: string } |
| 129 | )) { |
| 130 | const { store, table, tableLabel, color, entries } = options; |
| 131 | const hiddenProperties = options.hiddenProperties ?? []; |
| 132 | const propertyFormatters = options.propertyFormatters ?? {}; |
| 133 | const queueIdProperty = "queueIdProperty" in options ? options.queueIdProperty : "queueId"; |
| 134 | const entryLabelProperty = "entryLabelProperty" in options ? options.entryLabelProperty : null; |
| 135 | const entryLabel = "entryLabel" in options ? options.entryLabel : null; |
| 136 | |
| 137 | function formatPropertyLabel(property: string, isDefaultValue: boolean): string { |
| 138 | const label = convertCamelCaseToTitleCase(stripIdSuffix(property)); |
| 139 | return isDefaultValue ? label : bold(label); |
| 140 | } |
| 141 | |
| 142 | function formatPropertyValue(entry: T, property: string): string { |
| 143 | // handle mentionable properties |
| 144 | if ("isRole" in entry) { |
| 145 | const isRole = (entry as any).isRole; |
| 146 | const subjectId = (entry as any).subjectId; |
| 147 | return isRole ? roleMention(subjectId) : userMention(subjectId); |
| 148 | } |
| 149 | |
| 150 | const value = (entry as any)[property]; |
| 151 | const valueFormatter = propertyFormatters[property]; |
| 152 | return valueFormatter ? valueFormatter(value) : inlineCode(String(value)); |
| 153 | } |
| 154 | |
| 155 | function formatDescriptionProperty(entry: T, property: string): string { |
| 156 | const value = (entry as any)[property]; |
| 157 | const defaultValue = (table as any)[property]?.default; |
| 158 | const isDefaultValue = value == defaultValue; |
| 159 | |
| 160 | if (isNil(value) && isNil(defaultValue)) return ""; |
| 161 | |
| 162 | const formattedLabel = formatPropertyLabel(property, isDefaultValue); |
| 163 | const formattedValue = formatPropertyValue(entry, property) ?? ""; |
| 164 | const formattedOverriddenDefaultValue = |
| 165 | (property in table && !isDefaultValue) ? strikethrough(inlineCode(String(defaultValue))) : ""; |
| 166 | |
| 167 | return `- ${formattedLabel} = ${formattedValue} ${formattedOverriddenDefaultValue}`; |
| 168 | } |
| 169 | |
| 170 | function formatEntryDescription(entry: T): string { |
| 171 | const descriptionProperties = omit(entry, [...GLOBAL_HIDDEN_PROPERTIES, ...hiddenProperties, entryLabelProperty]); |
| 172 | const descriptionLines = Object.keys(descriptionProperties) |
| 173 | .map(property => formatDescriptionProperty(entry, property as string)) |
| 174 | .filter(Boolean); |
no test coverage detected