All files / src/services control-name-resolver-service.ts

100% Statements 19/19
100% Branches 16/16
100% Functions 4/4
100% Lines 19/19
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 40 41 42 43 44 45          1x                       1x 74x   36x 36x 4x   32x 12x   20x 4x 1x 4x     20x 19x 2x 1x 2x       20x 20x    
/**
 * @module Services
 * 
 */ /** */
 
 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 ControlNameResolverService {
     private nameCache: string[] = [];
     public getNameForControl(control: {new(...args: any[])} | {name: string}, eachModule: (callback: (key: string, value: Object) => boolean) => void = PLATFORM.eachModule){
         let found: string | null = null;
         if (!control || !control.name || !control.name.length){
             return null;
         }
         if (this.nameCache[control.name] !== undefined){
             return this.nameCache[control.name];
         }
         eachModule((m, v) => {
             if (v[control.name] === control)
                 found = m;
             return true;
         });
 
         if (!found){
            eachModule((m, v) => {
                if (v[control.name] && v[control.name].name === control.name)
                    found = m;
                return true;
            }); 
         }
 
         this.nameCache[control.name] = found;
         return found;
     }
 }