all files / common/core/graphics/ unit-value.ts

100% Statements 17/17
100% Branches 13/13
100% Functions 6/6
100% Lines 16/16
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                            108× 105× 105× 105×                 107×      
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
export class UnitValue {
    public value: number;
    public unit: string;
 
    public static equals(s1: UnitValue, s2: UnitValue) {
        return s1.value === s2.value && s1.unit === s2.unit;
    }
 
    constructor(value?: number | string, unit?: string) {
        if (typeof value === 'string') {
            const match = value.match(/([0-9\.]+)(.*)/);
            this.value = match && match.length >= 2 && parseInt(match[1], 10);
            this.unit = match && match.length >= 3 &&  match[2];
        } else {
            this.value = value;
            this.unit = unit;
        }
    }
 
    public clone() {
        return new UnitValue(this.value, this.unit);
    }
 
    public toString() {
        return String(this.value) + this.unit;
    }
 
    public isInvalid() {
        return this.value === undefined || this.value === null || isNaN(this.value);
    }
}