All files / mframejs/binding setValue.ts

90.32% Statements 28/31
50% Branches 1/2
100% Functions 4/4
90.32% Lines 28/31

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 9829x 29x             29x   54x 54x     54x         33x     30x     33x   28x           21x               7x 7x   7x             7x       7x 7x   5x 5x                 7x 7x       1x         6x   7x 7x     1x   5x 5x                    
import { getCorrectContext } from './contextOfObject';
import { Cache } from '../utils/exported';
import { IBindingContext } from '../interface/exported';
 
/**
  * simple function for setting value with token key
  *
  */
export function setValue(_class: IBindingContext, key: string, value: any) {
 
    _class = getCorrectContext(key, _class);
    _class = _class.__bindingContext ? _class.$context : _class;
 
    // types
    const keys = Cache.keyMaps.getCreate(key);
    try {
        let ref: any;
        // more then 1, then its array
        if (keys.length > 1) {
            keys.forEach((prop: any, i: number) => {
                if (prop) {
                    if (i === keys.length - 1) {
                        ref[prop] = value;
                    } else {
                        if (i === 0) {
                            ref = _class[prop];
                        } else {
                            ref = ref[prop];
                        }
                    }
                }
            });
        } else {
            _class[key] = value;
        }
    } catch (e) {
 
        // if key is not found, we will try and build it
 
        // first we make a object to what we want to build
        try {
            console.warn('key not found, rebuilding', key);
            let newObject = {};
            let ref: any;
            keys.forEach((prop: any, i: number) => {
                if (prop) {
                    if (i === keys.length - 1) {
                        if (i === 0) {
                            newObject[prop] = {};
                            ref = newObject[prop];
                        } else {
                            ref[prop] = value;
                        }
                    } else {
                        if (i === 0) {
                            newObject[prop] = {};
                            ref = newObject[prop];
                        } else {
                            ref[prop] = {};
                            ref = ref[prop];
                        }
                    }
                }
            });
 
            // not we add it to class
 
            // let refx = newObject;
            let classRef = _class;
            keys.forEach((prop: any, i: number) => {
                if (prop) {
                    if (i === keys.length - 1) {
                        if (!classRef[prop]) {
                            classRef[prop] = newObject[prop];
                        }
                    } else {
                        if (i === 0) {
                            if (!classRef[prop]) {
                                classRef[prop] = newObject[prop];
                            }
                            newObject = newObject[prop];
                            classRef = classRef[prop];
                        } else {
                            if (!classRef[prop]) {
                                classRef[prop] = newObject[prop];
                            }
                            newObject = newObject[prop];
                            classRef = classRef[prop];
                        }
                    }
                }
            });
        } catch (e) {
            console.error('key not found', key);
        }
    }
}