All files / helpers valueConverter.ts

39.13% Statements 9/23
23.81% Branches 5/21
50% Functions 1/2
39.13% Lines 9/23

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 833x 3x   3x       6x         6x       1x                                                       2x                             3x                                             3x
import getGlobalFunction from "./getGlobalFunction";
import { OneOf } from "./oneOf";
 
const valueConverter = {
 
    toProperty: (value: string, type: Function) => {
 
        Iif (type instanceof OneOf) {
 
            return type.toProperty(value);
        }
 
        switch (type) {
 
            case Boolean:
 
                return value !== null && value !== 'false';
 
            case Number:
 
                return value === null ? null : Number(value);
 
            case Array:
                {
                    // All the properties that are not declared as Function accept a function as alternative by design
                    // The probing is as follows: 
                    // Test whether it is really an array
                    try {
 
                        return JSON.parse(value);
                    }
                    catch (error) {// Value is a string but not a JSON one, assume a function
 
                        return getGlobalFunction(value);
                    }
                }
 
            // case ElementNode: {
 
            //     return createVirtualNode(value);
            // }
 
            case Function: { // Extract the string and return the global function
 
                return getGlobalFunction(value);
            }
 
            case Object: // It can also be a string
 
                try {
 
                    value = JSON.parse(value);
                }
                catch (error) {
 
                    return value;
                }
        }
 
        return value;
    },
 
    toAttribute: (value: any) => {
 
        const type = typeof value;
 
        if (type === 'boolean') {
 
            return value ? '' : 'false';
        }
        else if (type === 'object' || Array.isArray(value)) {
 
            return value == null ? value : JSON.stringify(value);
        }
        else {
 
            return value;
        }
    }
 
};
 
export default valueConverter;