MCPcopy Create free account
hub / github.com/ArrowM/Queue-Bot / describeTable

Function describeTable

src/utils/string.utils.ts:117–202  ·  view source on GitHub ↗
(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 }
	))

Source from the content-addressed store, hash-verified

115const GLOBAL_HIDDEN_PROPERTIES = ["id", "guildId", "queueId", "isRole"];
116
117export 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);

Callers 8

admins_getMethod · 0.90
prioritize_getMethod · 0.90
queues_getMethod · 0.90
voice_getMethod · 0.90
blacklist_getMethod · 0.90
whitelist_getMethod · 0.90
displays_getMethod · 0.90
schedule_getMethod · 0.90

Calls 4

BigIntSafeFunction · 0.90
queueMentionFunction · 0.85
formatEntryFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected