All files / datamodel/src/fields measure.js

100% Statements 14/14
100% Branches 6/6
100% Functions 8/8
100% Lines 13/13

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                                      260x 260x 260x 260x 260x                   11x                       911x 911x                 2x                 2x                 1x 1x                 34x          
import PartialField from './partial-field';
import { generateMeasureDomain, formatNumber } from '../utils';
import { defaultReducerName } from '../operator/group-by-function';
 
/**
 * Represents measure field type.
 *
 * @extends PartialField
 */
class Measure extends PartialField {
 
    /**
     * Creates new Measure field instance.
     *
     * @param {string} name - The name of the field.
     * @param {Array} data - An array containing the field data.
     * @param {Object} schema - The schema for the field.
     */
    constructor(name, data, schema) {
        super(name, data, schema);
        this.fieldUnit = schema.unit;
        this.fieldScale = schema.scale;
        this.fieldDefAggFn = schema.defAggFn || defaultReducerName;
        this.fieldNumberformat = schema.numberFormat instanceof Function ? schema.numberFormat : formatNumber;
    }
 
    /**
     * Returns the domain for the measure field.
     *
     * @override
     * @return {Array} Returns min and max values from measure values.
     */
    domain() {
        return generateMeasureDomain(this.data);
    }
 
    /**
     * A hook which is called for every entry(cell) of the column.
     *
     * @todo Fix the null data e.g. NaN value.
     *
     * @param {*} val - The current entry present in the column while iteration.
     * @return {number | null} Returns the parsed number value of content of cell or null.
     */
    parse (val) {
        val = parseFloat(val, 10);
        return Number.isNaN(val) ? null : val;
    }
 
    /**
     * Getter for unit value of the field.
     *
     * @return {string} Returns unit of the field.
     */
    unit() {
        return this.fieldUnit;
    }
 
    /**
     * Getter for scale value of the field.
     *
     * @return {string} Returns scale of the field.
     */
    scale() {
        return this.fieldScale;
    }
 
    /**
     * Getter for number format value of the field.
     *
     * @return {string} Returns number format of the field.
     */
    numberFormat() {
        const formatter = this.fieldNumberformat;
        return val => formatter(val);
    }
 
    /**
     * Getter for aggregation function of the field.
     *
     * @return {Function} Returns aggregation function of the field.
     */
    defAggFn() {
        return this.fieldDefAggFn;
    }
}
 
export default Measure;