File

src/pipes/theme-icon-mapper/random-color-map.pipe.ts

Metadata

Methods

transform
transform(key: string, index?: number)
Parameters :
Name Type Optional
key string No
index number Yes
Returns : any

Properties

Private Static cache
Type : literal type
Default value : {}
Private colors
Default value : ColorMapping.map(({primary, secondary}) => ({ iconBgColor: primary, pillBgColor: secondary }))
import {Pipe, PipeTransform} from '@angular/core';
import {ColorMapping} from '@app/app/app.constant';

@Pipe({
    name: 'randomColorMapPipe',
    pure: true
})
export class RandomColorMapPipe implements PipeTransform {
    private static cache: {[key: string]: any} = {};

    private colors = ColorMapping.map(({primary, secondary}) => ({
        iconBgColor: primary,
        pillBgColor: secondary
    }));

    transform(key: string, index?: number): any {
        if (!RandomColorMapPipe.cache[key]) {
            if (!index && index !== 0) {
                RandomColorMapPipe.cache[key] = this.colors[Math.floor(Math.random() * this.colors.length)];
            } else {
                RandomColorMapPipe.cache[key] = this.colors[(index % this.colors.length)];
            }
        }

        return RandomColorMapPipe.cache[key];
    }
}

results matching ""

    No results matching ""