All files / datamodel/src/fields dimension.js

100% Statements 9/9
100% Branches 8/8
100% Functions 3/3
100% Lines 9/9

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                                  1x                       976x 976x                   992x 992x 992x 250x   742x   992x          
import PartialField from './partial-field';
import { uniqueValues } from '../utils';
 
/**
 * Represents dimension field type.
 *
 * @extends PartialField
 */
class Dimension extends PartialField {
 
    /**
     * Returns the domain for the dimension field.
     *
     * @override
     * @return {Array} Returns the unique values from dimension values.
     */
    domain() {
        return uniqueValues(this.data);
    }
 
    /**
     * A hook which is called for every entry(cell) of the column.
     *
     * @todo Fix the null data e.g. undefined or null etc.
     *
     * @param {*} val - The current entry present in the column while iteration.
     * @return {string} Returns the string representation of the value.
     */
    parse (val) {
        val = (val === undefined || val === null) ? '' : val.toString();
        return val.trim();
    }
 
    /**
     * Saves the cardinality of the dimensional values after parsing the data.
     *
     * @param {string} val - The parsed value.
     * @return {string} Returns the input val.
     */
    parsed (val) {
        this._unique = this._unique || {};
        const unique = this._unique;
        if (val in unique) {
            unique[val]++;
        } else {
            unique[val] = 1;
        }
        return val;
    }
}
 
export default Dimension;