<commit_before>'use strict';

var assert = require('assert');

var JsigAST = require('../../ast/');
var cloneJSIG = require('./clone-ast.js');

module.exports = updateObject;

function updateObject(targetType, keyPath, newValue) {
    if (targetType.type === 'object') {
        return _updateObject(targetType, keyPath, newValue);
    }

    assert(targetType.type === 'intersectionType');

    var clone = cloneJSIG(targetType);
    clone.intersections = clone.intersections.slice();
    var intersections = clone.intersections;

    assert(keyPath.length === 1, 'can only have shallow keys');
    var key = keyPath[0];
    var foundIndex = -1;

    for (var i = 0; i < intersections.length; i++) {
        var possibleObject = intersections[i];
        if (possibleObject.type !== 'object') {
            continue;
        }

        for (var j = 0; j < possibleObject.keyValues.length; j++) {
            var pair = possibleObject.keyValues[j];
            if (pair.key === key) {
                foundIndex = i;
                break;
            }
        }
    }

    assert(foundIndex !== -1, 'cannot updateObject() weird intersection');

    var obj = updateObject(intersections[foundIndex], keyPath, newValue);
    intersections[foundIndex] = obj;

    return clone;
}

function _updateObject(targetType, keyPath, newValue) {
    assert(targetType.type === 'object', 'targetType must be object');
    assert(newValue, 'newValue cannot be null');

    var pairs = [];
    var addedField = false;
    for (var i = 0; i < targetType.keyValues.length; i++) {
        var pair = targetType.keyValues[i];
        var fieldName = keyPath[0];

        if (pair.key !== fieldName) {
            pairs.push(JsigAST.keyValue(pair.key, pair.value));
            continue;
        }

        var fieldValue;
        if (keyPath.length === 1) {
            fieldValue = newValue;
        } else {
            fieldValue = updateObject(pair.value, keyPath.slice(1), newValue);
        }

        addedField = true;
        pairs.push(JsigAST.keyValue(pair.key, fieldValue));
    }

    if (!addedField) {
        assert(keyPath.length === 1, 'keyPath must be shallow');
        pairs.push(JsigAST.keyValue(keyPath[0], newValue));
    }

    return JsigAST.object(pairs);
}
<commit_msg>updateObject(): support nested queries<commit_after>'use strict';

var assert = require('assert');

var JsigAST = require('../../ast/');
var cloneJSIG = require('./clone-ast.js');

module.exports = updateObject;

function updateObject(targetType, keyPath, newValue) {
    if (targetType.type === 'object') {
        return _updateObject(targetType, keyPath, newValue);
    }

    assert(targetType.type === 'intersectionType');

    var clone = cloneJSIG(targetType);
    clone.intersections = clone.intersections.slice();
    var intersections = clone.intersections;

    var primaryKey = keyPath[0];
    var foundIndex = -1;

    for (var i = 0; i < intersections.length; i++) {
        var possibleObject = intersections[i];
        if (possibleObject.type !== 'object') {
            continue;
        }

        for (var j = 0; j < possibleObject.keyValues.length; j++) {
            var pair = possibleObject.keyValues[j];
            if (pair.key === primaryKey) {
                foundIndex = i;
                break;
            }
        }
    }

    assert(foundIndex !== -1, 'cannot updateObject() weird intersection');

    var obj = updateObject(intersections[foundIndex], keyPath, newValue);
    intersections[foundIndex] = obj;

    return clone;
}

function _updateObject(targetType, keyPath, newValue) {
    assert(targetType.type === 'object', 'targetType must be object');
    assert(newValue, 'newValue cannot be null');

    var pairs = [];
    var addedField = false;
    for (var i = 0; i < targetType.keyValues.length; i++) {
        var pair = targetType.keyValues[i];
        var fieldName = keyPath[0];

        if (pair.key !== fieldName) {
            pairs.push(JsigAST.keyValue(pair.key, pair.value));
            continue;
        }

        var fieldValue;
        if (keyPath.length === 1) {
            fieldValue = newValue;
        } else {
            fieldValue = updateObject(pair.value, keyPath.slice(1), newValue);
        }

        addedField = true;
        pairs.push(JsigAST.keyValue(pair.key, fieldValue));
    }

    if (!addedField) {
        assert(keyPath.length === 1, 'keyPath must be shallow');
        pairs.push(JsigAST.keyValue(keyPath[0], newValue));
    }

    return JsigAST.object(pairs);
}

{'fraction_non_alphanumeric': 0.13561190738699008, 'fraction_numerical': 0.005071664829106946, 'mean_word_length': 2.491916859122402, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2956562', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1319, 'n_words': 318}