All files / src SSVGPropertyEnum.js

88.88% Statements 24/27
66.66% Branches 8/12
100% Functions 4/4
88% Lines 22/25

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                                  5x 5x 5x     5x 5x       5x 5x 5x 1x     4x 11x       5x 5x 5x 4x     5x 4x                 52x   52x             16x 16x           85x      
// @ts-check
 
import SSVGProperty from './SSVGProperty';
import { isNil } from './utils';
import { parseInitial, parseString } from './parser/values';
import { parseList } from './parser/utils';
 
/**
 * @typedef {import('./SSVGState').default} SSVGState
 */
 
export default class SSVGPropertyEnum extends SSVGProperty {
  /**
   * @param {SSVGState} ssvg
   * @param {Element} element
   */
  constructor(ssvg /*: SSVGState */, element /*: Element */) {
    super(ssvg, element);
    const values = element.getAttribute('values');
    Iif (!values) {
      throw new SyntaxError('values missing on enum property');
    }
    this.values = parseList({ index: 0, value: values }, parseString);
    Iif (this.values.length === 0) {
      throw new SyntaxError('invalid values property on enum: ' + values);
    }
    /** @type {{ [key: string]: number }} */
    this.range = {};
    const count = this.values.length;
    if (count === 1) {
      this.range[this.values[0]] = 0;
    }
    else {
      for (let i = 0; i < count; ++i) {
        this.range[this.values[i]] = i / (count - 1);
      }
    }
 
    this._value = this.values[0];
    const initial = element.getAttribute('initial');
    if (!isNil(initial)) {
      this._value = parseInitial(initial);
    }
 
    for (let i = 0; i < this.relations.length; ++i) {
      this.relations[i].setInitial(this);
    }
  }
 
  /**
   * @param {string?=} value
   * @return {number}
   */
  normalize(value) {
    Eif (isNil(value)) {
      // @ts-ignore
      return this.range[this._value];
    }
    return this.range[value];
  }
 
  /** @param {string|any} value */
  set value(value) {
    Eif (!isNil(this.range[value])) {
      super.value = value;
    }
  }
 
  /** @return {string|any} */
  get value() {
    return super.value;
  }
}