<commit_before>import { serializeDate } from './date'
import { isObservableArray, wrapObservableObjectOrArray } from '../utils/knockout'
import { isPrimitive, keys } from '../utils/objects'
import { getObjectTypeInfo } from '../metadata/typeMap';
import { notifySymbol, unmapKnockoutObservables } from '../state-manager';

export function deserialize(viewModel: any, target?: any, deserializeAll: boolean = false): any {
    if (ko.isObservable(viewModel)) {
        throw new Error("Parameter viewModel should not be an observable. Maybe you forget to invoke the observable you are passing as a viewModel parameter.");
    }

    if (ko.isObservable(target) && "setState" in target) {
        const targetObservable = target as DotvvmObservable<any>;
        targetObservable.setState(unmapKnockoutObservables(viewModel));
        (target as any)[notifySymbol as any](targetObservable.state)
        return target
    }

    if (isPrimitive(viewModel)) {
        return deserializePrimitive(viewModel, target);
    }

    if (viewModel instanceof Date) {
        return deserializeDate(viewModel, target);
    }

    if (viewModel instanceof Array) {
        return deserializeArray(viewModel, target, deserializeAll)
    }

    return deserializeObject(viewModel, target, deserializeAll)
}

export function deserializePrimitive(viewModel: any, target?: any): any {
    if (ko.isObservable(target)) {
        target(viewModel);
        return target;
    }
    return viewModel;
}

export function deserializeDate(viewModel: any, target?: any): any {
    serializeDate(viewModel);
    if (ko.isObservable(target)) {
        target(viewModel);
        return target;
    }
    return viewModel;
}

export function deserializeArray(viewModel: any, target?: any, deserializeAll: boolean = false): any {
    if (isObservableArray(target) && target() != null && target().length === viewModel.length) {
        updateArrayItems(viewModel, target, deserializeAll);
    } else {
        target = rebuildArrayFromScratch(viewModel, target, deserializeAll);
    }
    return target;
}

function rebuildArrayFromScratch(viewModel: any, target: any, deserializeAll: boolean) {
    const array: Array<KnockoutObservable<any>> = [];
    for (let i = 0; i < viewModel.length; i++) {
        array.push(wrapObservableObjectOrArray(deserialize(ko.unwrap(viewModel[i]), {}, deserializeAll)));
    }
    if (ko.isObservable(target)) {
        target = extendToObservableArrayIfRequired(target);
        target(array);
    } else {
        target = array;
    }
    return target;<commit_msg>increase code coverage by deleting code<commit_after>import { serializeDate } from './date'
import { isObservableArray, wrapObservableObjectOrArray } from '../utils/knockout'
import { isPrimitive, keys } from '../utils/objects'
import { getObjectTypeInfo } from '../metadata/typeMap';
import { notifySymbol, unmapKnockoutObservables } from '../state-manager';

export function deserialize(viewModel: any, target?: any, deserializeAll: boolean = false): any {
    if (ko.isObservable(viewModel)) {
        throw new Error("Parameter viewModel should not be an observable. Maybe you forget to invoke the observable you are passing as a viewModel parameter.");
    }

    if (ko.isObservable(target) && "setState" in target) {
        const targetObservable = target as DotvvmObservable<any>;
        targetObservable.setState(unmapKnockoutObservables(viewModel));
        (target as any)[notifySymbol as any](targetObservable.state)
        return target
    }

    if (isPrimitive(viewModel)) {
        return deserializePrimitive(viewModel, target);
    }

    if (viewModel instanceof Date) {
        return deserializeDate(viewModel, target);
    }

    if (viewModel instanceof Array) {
        return deserializeArray(viewModel, target, deserializeAll)
    }

    return deserializeObject(viewModel, target, deserializeAll)
}

export function deserializePrimitive(viewModel: any, target?: any): any {
    if (ko.isObservable(target)) {
        target(viewModel);
        return target;
    }
    return viewModel;
}

export function deserializeDate(viewModel: any, target?: any): any {
    return deserializePrimitive(target, serializeDate(viewModel));
}

export function deserializeArray(viewModel: any, target?: any, deserializeAll: boolean = false): any {
    if (isObservableArray(target) && target() != null && target().length === viewModel.length) {
        updateArrayItems(viewModel, target, deserializeAll);
    } else {
        target = rebuildArrayFromScratch(viewModel, target, deserializeAll);
    }
    return target;
}

function rebuildArrayFromScratch(viewModel: any, target: any, deserializeAll: boolean) {
    const array: Array<KnockoutObservable<any>> = [];
    for (let i = 0; i < viewModel.length; i++) {
        array.push(wrapObservableObjectOrArray(deserialize(ko.unwrap(viewModel[i]), {}, deserializeAll)));
    }
    if (ko.isObservable(target)) {
        target = extendToObservableArrayIfRequired(target);
        target(array);
    } else {
        target = array;
    }
    return target;
{'fraction_non_alphanumeric': 0.11047880690737834, 'fraction_numerical': 0.0003924646781789639, 'mean_word_length': 3.81758034026465, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7488813', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1416, 'n_words': 367}