All files / src/utils/data/record DataField.ts

81.48% Statements 22/27
88.24% Branches 15/17
66.67% Functions 6/9
81.48% Lines 22/27

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 108 109 110 111 112 113  4x         339x 76x 76x 111x   76x             4x                                     277x       277x   277x   8x     277x                               278x       3x     278x   278x         61x     61x             61x   61x                   480x         8x              
import { DataFieldDescriptor, IDataField } from "./interfaces";
import Observer from "../../observer/Observer";
import Subscriber from "../../observer/Subscriber";
 
function toTypeOf(typeFunction: Function) {
 
    switch (typeFunction) {
        case String: return 'string';
        case Boolean: return 'boolean';
        case Number: return 'number';
        case BigInt: return 'bigint';
        default: return 'object';
    }
}
 
/**
 * The field that is stored in a record of a store
 */
export default class DataField implements IDataField {
 
    /**
     * The descriptor of the field
     */
    private _fieldDescriptor: DataFieldDescriptor;
 
    /** The current value */
    private _value?: any;
 
    /** 
     * The initial value of the field 
     * The initial value is the one set on the initialize function and
     * corresponds to the value of the field of an empty record or a loaded one.
     * A field is considered "modified" if its current value is different from the "initial" one
     */
    private _initialValue?: any;
 
    /** The observer to notify when the value of the field changed */
    private _observer: Observer = new Observer('onValueSet');
 
    constructor(fieldDescriptor: DataFieldDescriptor, subscriber: Subscriber) {
 
        this._fieldDescriptor = fieldDescriptor;
 
        if (fieldDescriptor.value !== undefined) {
 
            this.initialize(fieldDescriptor.value);
        }
 
        this._observer.subscribe(subscriber);
    }
 
    get name() {
 
        return this._fieldDescriptor.name;
    }
 
    get isId() {
 
        return this._fieldDescriptor.isId;
    }
 
    initialize(value: any) {
 
        // Convert the value if its type is different from the expected type of the field descriptor
        if (value !== undefined &&
            value != null &&
            typeof value !== toTypeOf(this._fieldDescriptor.type)) {
 
            value = this._fieldDescriptor.converter!.fromString(value, this._fieldDescriptor.type);
        }
 
        this._value = value;
 
        this._initialValue = value;
    }
 
    set value(value: any) {
 
        const oldValue = this._value;
 
        // Convert the value if its type is different from the expected type of the field descriptor
        Iif (value !== undefined &&
            value != null &&
            typeof value !== toTypeOf(this._fieldDescriptor.type)) {
 
            value = this._fieldDescriptor.converter!.fromString(value, this._fieldDescriptor.type);
        }
 
        this._value = value;
 
        this._observer.notify(
            this._fieldDescriptor,
            this._value,
            oldValue,
            this._initialValue
        );
    }
 
    get value(): any {
 
        return this._value;
    }
 
    reset(): void {
 
        this.value = this._initialValue;
    }
 
    hasSameInitialValue(value: any): boolean {
 
        return this._initialValue === value
    }
}