All files / src/app/core/models/attribute attribute.helper.ts

100% Statements 7/7
100% Branches 3/3
100% Functions 3/3
100% Lines 6/6

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 20 21 22 23 24 25 26 27 28 29 30 31    89x                 289x 12x   673x                       277x 277x      
import { Attribute } from './attribute.model';
 
export class AttributeHelper {
  /**
   * Gets a specific attribute by attribute name.
   *
   * @param attributes      An array of attributes belonging to an object (e.g. product, basket etc.).
   * @param attributeName   The attribute name of the attribute to get.
   * @returns               The matching attribute.
   */
  static getAttributeByAttributeName(attributes: Attribute[], attributeName: string): Attribute {
    if (!attributes) {
      return;
    }
    return attributes.find(attribute => attribute.name === attributeName);
  }
 
  /**
   * Gets an attribute value of an attribute within an attribute array.
   * Checks if the attribute is available and returns the value, otherwise undefined.
   *
   * @param attributes      An array of attributes belonging to an object (e.g. product, basket etc.).
   * @param attributeName   The attribute name of the appropriate attribute.
   * @returns               The matching attribute value.
   */
  static getAttributeValueByAttributeName<T>(attributes: Attribute[], attributeName: string) {
    const attribute = AttributeHelper.getAttributeByAttributeName(attributes, attributeName);
    return attribute ? (attribute.value as T) : undefined;
  }
}