<commit_before>(function(global, undefined) {
    var jsonapi = global.jsonapi || (global.jsonapi = {});

    // Parse a HTTP Response using the JSONAPI format: http://jsonapi.org/format/
    jsonapi.parse = function(response) {
        var json, parsed;

        // IF: Response is a string, try to parse as JSON string
        // ELSE IF: Response is a object, reassign to local variable
        // ELSE: Return whatever the input was
        if (isString(response)) {
            try {
                json = global.JSON.parse(response);
            }
            catch (error) {
                // IF: Not JSON format, return it
                // ELSE: Throw the error
                if (error.name === 'SyntaxError') {
                    return response;
                }
                throw error;
            }
        }
        else if (isObject(response)) {
            json = response;
        }
        else {
            return response;
        }

        // IF: No required top-level JSON API members, return input
        if (isUndefined(json.data) && isUndefined(json.errors) && isUndefined(json.meta)) {
            return json;
        }

        // IF: Already parsed, return it
        if (json.jsonapi && json.jsonapi.parsed) {
            return json;
        }

        parsed = deserialize(json);
        parsed.jsonapi.parsed = true;

        return parsed;
    };

    // Deserialize the JSONAPI formatted object
    function deserialize(json) {
        var data, deserialized;

        var includedMap = {};
        each(json.included, function(value) {
            var key = value.type + '-' + value.id;
            includedMap[key] = value;
        });

        if (isArray(json.data)) {
            data = map(
                json.data,
                function(record) {
                    populateRelatedFields(record, includedMap);
                    return flatten(record);
                }
            );
        }
        else if (isObject(json.data)) {
            populateRelatedFields(json.data, includedMap);
            data = flatten(json.data);
        }

        deserialized = {
            data: data,
            jsonapi: json.jsonapi || {}
        };

        if (json.meta) {
            deserialized.meta = json.meta;
        }

        if (json.errors) {
            deserialized.errors = json.errors;
        }

        if (json.links) {
            deserialized.links = json.links;
        }

        return deserialized;
    }

    // Populate relations of the provided record from the included objects
    function populateRelatedFields(record, includedMap, parents, fromRelationship) {

        // IF: Object has relationships, update so this record is listed as a parent
        if (record.relationships) {
            parents = parents ? parents.concat([record]) : [record] ;
        }

        // IF: relationship defined meta, merge with record provided meta
        if (fromRelationship && isObject(fromRelationship.meta)) {
            record.meta = extend({}, record.meta, relationship.meta);
        }

        each(
            record.relationships,
            function(relationship, property) {
                // IF: relationship describes non-record specific meta;
                // append relationship meta to the record
                if (relationship && isObject(relationship.meta)) {
                    record.meta = record.meta || {};
                    record.meta[property] = relationship.meta;
                }

                // IF: No relationship data, don't add anything
                if (!relationship.data) {
                    return;
                }

                // IF: Relationship has multiple matches, create an array for matched records
                // ELSE: Assign relationship directly to the property
                if (isArray(relationship.data)) {
                    record.attributes[property] = map(
                        relationship.data,
                        function(data) {<commit_msg>fix a 'ReferenceError: relationship is not defined' bug<commit_after>(function(global, undefined) {
    var jsonapi = global.jsonapi || (global.jsonapi = {});

    // Parse a HTTP Response using the JSONAPI format: http://jsonapi.org/format/
    jsonapi.parse = function(response) {
        var json, parsed;

        // IF: Response is a string, try to parse as JSON string
        // ELSE IF: Response is a object, reassign to local variable
        // ELSE: Return whatever the input was
        if (isString(response)) {
            try {
                json = global.JSON.parse(response);
            }
            catch (error) {
                // IF: Not JSON format, return it
                // ELSE: Throw the error
                if (error.name === 'SyntaxError') {
                    return response;
                }
                throw error;
            }
        }
        else if (isObject(response)) {
            json = response;
        }
        else {
            return response;
        }

        // IF: No required top-level JSON API members, return input
        if (isUndefined(json.data) && isUndefined(json.errors) && isUndefined(json.meta)) {
            return json;
        }

        // IF: Already parsed, return it
        if (json.jsonapi && json.jsonapi.parsed) {
            return json;
        }

        parsed = deserialize(json);
        parsed.jsonapi.parsed = true;

        return parsed;
    };

    // Deserialize the JSONAPI formatted object
    function deserialize(json) {
        var data, deserialized;

        var includedMap = {};
        each(json.included, function(value) {
            var key = value.type + '-' + value.id;
            includedMap[key] = value;
        });

        if (isArray(json.data)) {
            data = map(
                json.data,
                function(record) {
                    populateRelatedFields(record, includedMap);
                    return flatten(record);
                }
            );
        }
        else if (isObject(json.data)) {
            populateRelatedFields(json.data, includedMap);
            data = flatten(json.data);
        }

        deserialized = {
            data: data,
            jsonapi: json.jsonapi || {}
        };

        if (json.meta) {
            deserialized.meta = json.meta;
        }

        if (json.errors) {
            deserialized.errors = json.errors;
        }

        if (json.links) {
            deserialized.links = json.links;
        }

        return deserialized;
    }

    // Populate relations of the provided record from the included objects
    function populateRelatedFields(record, includedMap, parents, fromRelationship) {

        // IF: Object has relationships, update so this record is listed as a parent
        if (record.relationships) {
            parents = parents ? parents.concat([record]) : [record] ;
        }

        // IF: relationship defined meta, merge with record provided meta
        if (fromRelationship && isObject(fromRelationship.meta)) {
            record.meta = extend({}, record.meta, fromRelationship.meta);
        }

        each(
            record.relationships,
            function(relationship, property) {
                // IF: relationship describes non-record specific meta;
                // append relationship meta to the record
                if (relationship && isObject(relationship.meta)) {
                    record.meta = record.meta || {};
                    record.meta[property] = relationship.meta;
                }

                // IF: No relationship data, don't add anything
                if (!relationship.data) {
                    return;
                }

                // IF: Relationship has multiple matches, create an array for matched records
                // ELSE: Assign relationship directly to the property
                if (isArray(relationship.data)) {
                    record.attributes[property] = map(
                        relationship.data,
                        function(data) {
{'fraction_non_alphanumeric': 0.09458456973293769, 'fraction_numerical': 0.0, 'mean_word_length': 1.4646556977452774, 'pattern_counts': {'":': 0, '<': 3, '<?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': '56124', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1921, 'n_words': 553}