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

100% Statements 2/2
100% Branches 4/4
100% Functions 2/2
100% Lines 2/2

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          788x                 74x        
/** 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)
    );
}