All files / blong-browser/src/lib label.ts

0% Statements 0/18
0% Branches 0/1
0% Functions 0/1
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                                     
/** Simple utility to convert camelCase object names to title case (Camel Case) for default card titles.
 *  It also strips common prefixes and suffixes, e.g. fieldId -> Field, isActive → Active, itemType → Type, entityName → Name.
 *  Stripping happens after splitting camelCase, so e.g. userId becomes User Id → User and solid becomes Solid (not Sol).
 */
export default function label<T>(title: T, name: T): T | string {
    return (
        title ??
        (typeof name === 'string'
            ? name
                  ?.split('.')
                  .pop()!
                  .replace(/([a-z])([A-Z])/g, '$1 $2') // Split camelCase
                  .replace(/^(is|has|can|should|field)?(.*?)(Id)?$/i, '$2')
                  .trim()
                  .replace(/\b\w/g, (c: string) => c.toUpperCase()) // Capitalise first letter of each word
            : name)
    );
}