MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / defaultTableGenerator

Function defaultTableGenerator

src/lib/table-generator.ts:191–286  ·  view source on GitHub ↗
(tableOptions: Pick<TableOptions, 'groupRows'>)

Source from the content-addressed store, hash-verified

189}
190
191export const defaultTableGenerator = (tableOptions: Pick<TableOptions, 'groupRows'>): TableGenerator => {
192 const logger = log4js.getLogger('table-manager')
193
194 const convertToLabel = (propertyName: string): string => {
195 // We only use the last field for the name if it's a nested property.
196 const propertyNames = propertyName.split('.')
197 return propertyNames[propertyNames.length - 1]
198 .replace(/([a-z])([A-Z])/g, '$1 $2')
199 .replace(/^([a-z])/, text => text.toUpperCase())
200 .replace(/\bUri\b/, 'URI')
201 .replace(/\bUrl\b/, 'URL')
202 .replace(/\bArn\b/, 'ARN')
203 .replace(/\bO ?[Aa]uth\b/, 'OAuth')
204 .replace(/^Is /, '')
205 }
206
207 const getLabelFor = <T extends object>(definition: TableFieldDefinition<T>): string => {
208 if (typeof definition !== 'object') {
209 return convertToLabel(definition.toString())
210 }
211
212 if (definition.label) {
213 return definition.label
214 }
215
216 if ('value' in definition) {
217 return definition.label
218 }
219
220 if ('prop' in definition) {
221 return convertToLabel(definition.prop.toString())
222 }
223
224 return convertToLabel(definition.path)
225 }
226
227 const getDisplayValueFor = <T extends object>(item: T, definition: TableFieldDefinition<T>): string | undefined => {
228 if (typeof definition === 'object' && 'value' in definition) {
229 return definition.value(item)
230 }
231
232 if (typeof definition !== 'object' || 'prop' in definition) {
233 const propertyName = typeof definition !== 'object' ? definition : definition.prop
234 return stringFromUnknown(item[propertyName])
235 }
236
237 // The DefinitelyTyped type for the second parameter doesn't match what lodash actually
238 // takes so we cast it to the DefinitelyTyped type.
239 const matches = at(item, definition.path as unknown as keyof T)
240 if (matches.length === 0) {
241 logger.debug(`did not find match for ${definition.path} in ${JSON.stringify(item)}`)
242 return ''
243 }
244 if (matches.length > 1) {
245 logger.warn(`found more than one match for ${definition.path} in ${JSON.stringify(item)}`)
246 }
247 return matches.map(value => stringFromUnknown(value)).join(', ')
248 }

Calls

no outgoing calls

Tested by

no test coverage detected