All files / utils/data/record DataRecordDescriptor.ts

68.97% Statements 40/58
58.7% Branches 27/46
54.55% Functions 6/11
69.09% Lines 38/55

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195  3x                                                               3x         9x           61x                                             9x   43x             43x   43x                     9x           43x         43x                                       91x   415x   91x   91x   91x   49x   51x       51x   51x   47x   47x   2x   2x     45x       4x   4x   4x       49x   6x   27x   27x   27x               42x   168x   168x   168x         91x            
import { DataFieldModel, DataFieldDescriptor, IdentifierInfo } from "./interfaces";
import defaultValueConverter from "../converter/defaultValueConverter";
//import RecordValidator from "../validation/validators/record/RecordValidator";
 
class RecordValidator {
    
}
 
function getType(value: any): Function {
 
    const type = typeof value;
 
    switch (type) {
        case 'boolean': return Boolean;
        case 'number': return Number;
        case 'bigint': return BigInt;
        case 'string': return String;
        case 'object': {
 
            if (value instanceof Date) {
 
                return Date;
            }
            else {
 
                throw Error(`Not implemented for type: '${type}'`);
            }
        }
        default: throw Error(`Not implemented for type: '${type}'`);
        // "symbol" | "undefined" | "function"
    }
}
 
export default class DataRecordDescriptor {
 
    /**
     * The descriptors of the field of the record
     */
    private _fieldDescriptors: DataFieldDescriptor[] = [];
 
    private _recordValidators?: RecordValidator[];
 
    get fieldDescriptors() {
 
        return this._fieldDescriptors;
    }
 
    get recordValidators() {
 
        return this._recordValidators;
    }
 
    getFieldDescriptor(name: string): DataFieldDescriptor | undefined {
 
        const descriptors = this._fieldDescriptors.filter(d => d.name === name);
 
        return descriptors.length > 0 ?
            descriptors[0] :
            undefined;
    }
 
    /**
     * Creates the record descriptor from a model
     * @param model 
     */
    fromModel(model: Record<string, DataFieldModel>, ...recordValidators: RecordValidator[]): void {
 
        for (const key in model) {
 
            Eif (model.hasOwnProperty(key)) {
 
                const {
                    isId,
                    type,
                    value,
                    converter
                } = model[key];
 
                this.addFieldDescriptor({
                    name: key,
                    isId: isId ?? false,
                    type: type !== undefined ? type : value !== undefined ?
                        getType(value) : String,
                    value,
                    converter: converter || defaultValueConverter
                });
            }
        }
 
        this._recordValidators = recordValidators;
    }
 
    addFieldDescriptor(fd: DataFieldDescriptor): void {
 
        // Set the default converter if none are provided
        Iif (fd.converter === undefined) {
 
            fd.converter = defaultValueConverter;
        }
 
        this._fieldDescriptors.push(fd);
    }
 
    removeFieldDescriptor(fd: DataFieldDescriptor): void {
 
        const index = this._fieldDescriptors.indexOf(fd);
 
        if (index > -1) {
 
            this._fieldDescriptors.splice(index, 1);
        }  
    }
 
    /**
     * Creates an id object from the data using the field descriptors id property
     * @param data 
     * @param fcn A function to process the data further if necessary
     */
    getId(data: any, fcn?: Function): IdentifierInfo {
 
        const id: Record<string, any> = {};
 
        const idDescriptors = this._fieldDescriptors.filter(f => f.isId === true);
 
        const { length } = idDescriptors;
 
        let hasUndefinedIdentifiers = false;
 
        if (length > 0) { // There are some field(s) that compose an identifier
 
            for (let i = 0; i < length; ++i) {
 
                const descriptor = idDescriptors[i];
 
                const {
                    name
                } = descriptor;
 
                if (data.hasOwnProperty(name)) {
 
                    const value = fcn !== undefined ? fcn(data[name]) : data[name];
 
                    if (value === undefined) {
 
                        hasUndefinedIdentifiers = true;
 
                        break;
                    }
 
                    id[name] = value;
                }
                else { // No data property for that id descriptor
 
                    id[name] = undefined;
 
                    hasUndefinedIdentifiers = true;
 
                    break;
                }
            }
 
            if (hasUndefinedIdentifiers) { // No field serves a an identifier, create one using all the fields for (hopefully) uniqueness
 
                for (const key in data) {
 
                    Eif (data.hasOwnProperty(key)) {
 
                        const value = fcn !== undefined ? fcn(data[key]) : data[key];
 
                        id[key] = value;
                    }
                }
            }
 
        }
        else { // No field serves a an identifier, create one using all the fields for (hopefully) uniqueness
 
            for (const key in data) {
 
                Eif (data.hasOwnProperty(key)) {
 
                    const value = fcn !== undefined ? fcn(data[key]) : data[key];
 
                    id[key] = value;
                }
            }
        }
 
        return {
            value: id,
            noDescriptorsId: length === 0,
            hasUndefinedIdentifiers: length > 0 && hasUndefinedIdentifiers === true
        };
    }
}