All files / src/string css-unit.ts

100% Statements 28/28
90% Branches 9/10
100% Functions 2/2
100% Lines 28/28

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                5x       5x         5x         5x         5x         5x                                   54x 54x   54x               54x 8x 8x 8x 46x 8x 8x 8x 38x 8x 8x 8x   30x 30x 30x 30x     54x   54x                                                                                     5x      
import * as is from '../is/is';
import type {ColorRGBA} from '../mathf/color';
import * as color from '../mathf/color';
 
/**
 * The different types of structures that can be passed.
 * @see https://css-tricks.com/converting-color-spaces-in-javascript/
 */
export enum CssUnitObjectTypes {
  /**
   * A number.
   */
  number = 'num',
 
  /**
   * A standard numerical value + unit such as '1px' or '10%'
   */
  unit = 'unit',
 
  /**
   * A css hex unit such as '#FFFFFF'
   */
  cssHex = 'cssHex',
 
  /**
   * A css rgba unit such as 'rgba(255, 255, 255, 0.3)'
   */
  rgba = 'rgba',
 
  /**
   * A css rgb unit such as 'rgba(255, 255, 255)'
   */
  rgb = 'rgb',
}
 
export interface CssUnitObject {
  value: number | ColorRGBA | null;
  valueType: CssUnitObjectTypes | string | null;
  originalValue: string | null;
  unit: string | null;
  type: CssUnitObjectTypes | string | null;
}
 
/**
 * Takes a css unit value like '10px' or '20vw' and parses it into
 * a CssUnitObject.
 * @param css
 */
export function parse(css: string): CssUnitObject {
  // const value = css.match(/-?\d+/g);
  const value = css.replace(/[a-zA-Z%]+/g, '');
  const unit = css.match(/[a-zA-Z%]+/g);
 
  const result: CssUnitObject = {
    value: null,
    unit: null,
    type: null,
    valueType: null,
    originalValue: css,
  };
 
  if (is.cssRgba(css)) {
    result.type = CssUnitObjectTypes.rgba;
    result.value = color.cssToRgba(css);
    result.valueType = CssUnitObjectTypes.rgba;
  } else if (is.cssRgb(css)) {
    result.type = CssUnitObjectTypes.rgb;
    result.value = color.cssToRgba(css);
    result.valueType = CssUnitObjectTypes.rgba;
  } else if (is.cssHex(css)) {
    result.value = color.cssToRgba(css);
    result.type = CssUnitObjectTypes.cssHex;
    result.valueType = CssUnitObjectTypes.rgba;
  } else {
    result.value = value ? +value : null;
    result.unit = unit ? unit[0] : null;
    result.type = CssUnitObjectTypes.unit;
    result.valueType = CssUnitObjectTypes.number;
  }
 
  result.originalValue = css;
 
  return result;
}
 
/**
 * A class that helps parsing of css units.
 *
 *
 * ```ts
 *
 * cssUnit.parse('10%').unit // %
 * cssUnit.parse('10%').type // unit
 * cssUnit.parse('10%').valueType // number
 * cssUnit.parse('10%').value // 10
 *
 *
 *
 * let rgba = 'rgba(255, 255, 255, 0.3)'
 * cssUnit.parse(rgba).unit // null
 * cssUnit.parse(rgba).type // rgba
 * cssUnit.parse(rgba).value // Return ColorRgba
 * cssUnit.parse(rgba).value.r // 255
 * cssUnit.parse(rgba).value.g // 255
 * cssUnit.parse(rgba).value.b // 255
 * cssUnit.parse(rgba).value.a // a
 *
 *
 * let hex = '#FFFFFF'
 * cssUnit.parse(rgba).unit // null
 * cssUnit.parse(rgba).type // cssHex
 * cssUnit.parse(rgba).originalValue // '#FFFFFF'
 * cssUnit.parse(rgba).value // Return ColorRgba (we use rgba as the centeral unit)
 * cssUnit.parse(rgba).value.r // 255
 * cssUnit.parse(rgba).value.g // 255
 * cssUnit.parse(rgba).value.b // 255
 * cssUnit.parse(rgba).value.a // a
 *
 * let rgba = 'rgb(255, 255, 255)'
 * cssUnit.parse(rgba).unit // null
 * cssUnit.parse(rgba).type // rgba
 * cssUnit.parse(rgba).value // Return ColorRgba
 *
 * ```
 */
export const cssUnit = {
  parse,
};