<commit_before>var _ = require('underscore');
var traverse = require('traverse');

module.exports = function readOnlyDefaultValuesHandler(ctx) {
    var model = ctx.method.ctor;
    var traverseBody = traverse(ctx.req.body);
    var properties = model.definition.properties;

    return schemaWalk(properties, [], traverseBody);
};

function isNullValue(value) {
    return (value === null || value === undefined);
}

function schemaWalk(properties, propertyPath, traverseBody) {
    var arrayModel;
    var bodyNode;
    var nestedProperties;
    var path;
    var property;
    var propertyType;
    var propertyItems;

    for (var key in properties) {
        path = propertyPath.slice(0);
        path.push(key);
        property = properties[key];

        propertyType = property.type;
        if (propertyType == 'object') {
            nestedProperties = property.properties;

            if (nestedProperties) {
                schemaWalk(nestedProperties, path, traverseBody);
            }

            continue;
        } else if (propertyType == 'array') {
            bodyNode = traverseBody.get(path);

            if (!_.isArray(bodyNode))
                continue;


            propertyItems = property.items;
            if (_.isArray(propertyItems)) {
                for(var i = 0; i < propertyItems.length; i++) {
                    var arraySchema = propertyItems[i];

                    if (arraySchema.type == 'object') {
                        nestedProperties = arraySchema.properties;

                        if (nestedProperties) {
                            schemaWalk(nestedProperties, [], traverse(bodyNode[i]));
                        }
                    }
                }
            }
            else if (_.isObject(propertyItems)) {
                arrayModel = propertyItems;

                for(var j = 0; j < bodyNode.length; j++) {
                    schemaWalk(arrayModel.properties, [], traverse(bodyNode[j]));
                }
            }
        }

        if (property.readOnly) {
            removeReadOnlyProperty(traverseBody, path);
        }

        if (property.default) {
            applyDefaultPropertyValue(traverseBody, path, property.default);
        }
    }

    return traverseBody.value;
}

function removeReadOnlyProperty(traverseBody, path) {
    var node = traverseBody.value;
    var pathSize = path.length - 1;

    for(var i = 0; i < pathSize; i++) {
        node = node[path[i]];

        if (isNullValue(node)) {
            return;
        }<commit_msg>refactor array functions<commit_after>var _ = require('underscore');
var traverse = require('traverse');

module.exports = function readOnlyDefaultValuesHandler(ctx) {
    var model = ctx.method.ctor;
    var traverseBody = traverse(ctx.req.body);
    var properties = model.definition.properties;

    return schemaWalk(properties, [], traverseBody);
};

function isNullValue(value) {
    return (value === null || value === undefined);
}

function walkOnObjectProperties(schema, bodyItems) {
    for(var i = 0; i < bodyItems.length; i++) {
        schemaWalk(schema.properties, [], traverse(bodyItems[i]));
    }
}

function walkOnArrayProperties(schemas, bodyItems) {
    var arraySchema;
    var nestedProperties;

    for(var i = 0; i < schemas.length; i++) {
        arraySchema = schemas[i];

        if (arraySchema.type == 'object') {
            nestedProperties = arraySchema.properties;

            if (nestedProperties) {
                schemaWalk(nestedProperties, [], traverse(bodyItems[i]));
            }
        }
    }
}

function schemaWalk(properties, propertyPath, traverseBody) {
    var arrayModel;
    var bodyItems;
    var nestedProperties;
    var path;
    var property;
    var propertyType;
    var schema;

    for (var key in properties) {
        path = propertyPath.slice(0);
        path.push(key);
        property = properties[key];

        propertyType = property.type;
        if (propertyType === 'object') {
            nestedProperties = property.properties;

            if (nestedProperties) {
                schemaWalk(nestedProperties, path, traverseBody);
            }

            continue;
        } else if (propertyType === 'array') {
            bodyItems = traverseBody.get(path);

            if (!_.isArray(bodyItems)) {
                continue;
            }

            schema = property.items;
            if (_.isArray(schema)) {
                walkOnArrayProperties(schema, bodyItems);
            } else if (_.isObject(schema)) {
                walkOnObjectProperties(schema, bodyItems);
            }
        }

        if (property.readOnly) {
            removeReadOnlyProperty(traverseBody, path);
        }

        if (property.default) {
            applyDefaultPropertyValue(traverseBody, path, property.default);
        }
    }

    return traverseBody.value;
}

function removeReadOnlyProperty(traverseBody, path) {
    var node = traverseBody.value;
    var pathSize = path.length - 1;

    for(var i = 0; i < pathSize; i++) {
        node = node[path[i]];

        if (isNullValue(node)) {
            return;
        }
{'fraction_non_alphanumeric': 0.10964230171073094, 'fraction_numerical': 0.0019440124416796269, 'mean_word_length': 2.012295081967213, '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': '404881', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1321, 'n_words': 301}