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

100% Statements 18/18
80% Branches 4/5
100% Functions 1/1
100% Lines 18/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 191x 1x 1x 1x 1x 57x 57x 57x 57x 57x 57x 1x 1x 1x 1x 1x 1x 1x  
/** 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)
    );
}