All files / datamodel/src/fields partial-field.js

89.47% Statements 17/19
100% Branches 4/4
81.82% Functions 9/11
88.89% Lines 16/18

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                                549x 549x 549x 549x 549x 549x                 1957x 549x                   965x                         513x                                           4x 4x     4x             42x             10x             10x          
import { extend2 } from '../utils';
 
 /**
  * The base class for every field type.
  * It provides some common functionalities.
  */
class PartialField {
 
    /**
     * Sets basic setups to each Field instance.
     *
     * @param {string} name - The name or identifier of the field.
     * @param {Array} data - The data array.
     * @param {Object} schema - The schema of the data type.
     */
    constructor(name, data, schema) {
        this.name = name;
        this.data = data || [];
        this.schema = schema;
        this.fieldDescription = schema.description;
        this.fieldType = schema.type;
        this.sanitize();
    }
 
    /**
     * Sanitizes the field data.
     *
     * @return {PartialField} - Returns the instance of the current context for chaining.
     */
    sanitize () {
        this.data = this.data.map(d => this.parsed(this.parse(d)));
        return this;
    }
 
    /**
     * The post parsing hook for field instance.
     *
     * @param {*} val - The value to be parsed.
     * @return {*} Returns the parsed value.
     */
    parsed (val) {
        return val;
    }
 
    /**
     * Generates and returns the domain for the field.
     *
     * @abstract
     */
    domain() {
        throw new Error('Not yet implemented!');
    }
 
    subType() {
        return null;
    }
 
 
    /**
     * Parse the input value before using.
     *
     * @abstract
     */
    parse () {
        throw new Error('Not yet implemented!');
    }
 
    /**
     * Creates brand new copy of current field instance. To avoid optimization issue
     * pass the required data otherwise current data would be copied which might
     * be expensive.
     *
     * @param {Array} data - The input data, if provided current data will not be cloned.
     * @return {PartialField} Returns the cloned field instance.
     */
    clone(data) {
        data = data || extend2([], this.data);
        const schema = extend2({}, this.schema);
        // Here call the constructor to create an instance of
        // the current field class type e.g. Measure, Dimension etc.
        return new this.constructor(this.name, data, schema);
    }
 
    /**
     * @return {string} Name of the field
     */
    fieldName() {
        return this.name;
    }
 
     /**
     * @return {string} Type of the field
     */
    type() {
        return this.fieldType;
    }
 
    /**
     * @return {description} Name of the field
     */
    description() {
        return this.fieldDescription;
    }
}
 
export default PartialField;