All files / src/helpers ControlNameResolver.ts

42.86% Statements 6/14
33.33% Branches 3/9
50% Functions 1/2
42.86% Lines 6/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                1x                       1x 1x   3x 3x 3x                          
/**
 * @module Helpers
 * 
 * @preferred
 * @description This module contains helper utilities
 * 
 */ /** */
 
import { PLATFORM } from 'aurelia-framework';
 
/**
 * Helper class that resolves content names to be able to use in Composition, based on their Control classes
 * Usage:
 * 
 * ``` ts
 * import { ContentTypes } from 'sn-client-js';
 * const resolvedControlName = ControlNameResolver.getNameForControl(ContentTypes.Task);
 * 
 * ```
 */
export class ControlNameResolver {
    private static nameCache: string[] = [];
    public static getNameForControl(control: {new(...args: any[])}){
        let found: string | null = null;
        Eif (!control || !control.name || !control.name.length){
            return null;
        }
        if (this.nameCache[control.name] !== undefined){
            return this.nameCache[control.name];
        }
        PLATFORM.eachModule((m, v) => {
            if (v[control.name] === control)
                found = m;
            return true;
        });
        this.nameCache[control.name] = found;
        return found;
    }
}