<commit_before>var RSVP = require('rsvp'),
    Promise = RSVP.Promise,
    _ = require('lodash'),
    inflect = require('i')(),
    $http = require('http-as-promised');

module.exports = function (adapter, schemas) {

    function linked(body, inclusions) {

        var primaryResourceName = extractPrimaryResource(body);
        // a bit dirty, but necessary to avoid total refactor of sendResponse
        function extractPrimaryResource(body) {
            return _.first(_.filter(_.keys(body), function (key) {
                return !(key === 'meta' || key === 'links');
            }));
        }

        var modelName = inflect.singularize(primaryResourceName);
        var primarySchema = schemas[modelName];

        function isRemote(branch, key) {
            return null != branch && null != branch.refs && null != branch.refs[key] && null != branch.refs[key].def && null != branch.refs[key].def.baseUri;
        }

        function toIncludes(map) {
            var includes = [];

            function buildRecursively(map, prefix) {
                prefix = prefix || '';
                return _.forEach(map, function (value, key) {
                    if (_.isEmpty(value)) {
                        includes.push(prefix + key);
                    } else {
                        var newPrefix = prefix + key + '.';
                        buildRecursively(value, newPrefix)
                    }
                });
            }

            buildRecursively(map);
            return includes.join(',');
        }

        function mapToInclusionTree(typeMap, schema, path) {
            path = path || [];
            var branch = _.map(typeMap, function (value, key) {
                path.push(key);
                var subBranch = buildInclusionBranch([key], schema);
                if (!_.isEmpty(value)) {
                    if (isRemote(subBranch, key)) {
                        subBranch.refs[key].def.remoteIncludes = toIncludes(value);
                    } else {
                        var schemaName = schema[key];
                        var subMap = mapToInclusionTree(value, schemas[schemaName], path);
                        _.merge(subBranch.refs[key], subMap);
                    }
                }
                path.pop();
                return subBranch;
            });
            return _.reduce(branch, function (acc, item) {
                return _.merge(acc, item);
            });
        }

        var inclusionMap = _.map(inclusions,function (inclusionString) {
            var result = {};
            var node = result;
            inclusionString.split('.').forEach(function (token) {
                node = node[token] = {};
            });
            return result;
        }).reduce(function (acc, node) {
                return _.merge(acc, node);
            });

        var inclusionTree = mapToInclusionTree(inclusionMap, primarySchema);

        // builds a tree representation out of a series of inclusion tokens
        function buildInclusionBranch(inclusionTokens, schema) {

            var inclusionToken = _.first(inclusionTokens);
            var type = _.isArray(schema[inclusionToken]) ? schema[inclusionToken][0] : schema[inclusionToken];
            var normalisedType = _.isPlainObject(type) ? type.ref : type;

            var linkDescriptor = {
                def: {type: normalisedType}
            };

            var baseUri = _.isPlainObject(type) ? type.baseUri : null;
            if (baseUri) {
                var remoteIncludes = _.drop(inclusionTokens, _.indexOf(inclusionTokens, inclusionToken) + 1).join('.');
                var remoteDescriptor = _.merge(linkDescriptor, {
                        def: {baseUri: baseUri, remoteIncludes: remoteIncludes}
                    }
                );
                return setRefs(remoteDescriptor);
            } else {
                var tokensRemaining = _.drop(inclusionTokens);
                if (tokensRemaining.length == 0) {
                    return setRefs(linkDescriptor);
                } else {<commit_msg>added a check around empty reduce<commit_after>var RSVP = require('rsvp'),
    Promise = RSVP.Promise,
    _ = require('lodash'),
    inflect = require('i')(),
    $http = require('http-as-promised');

module.exports = function (adapter, schemas) {

    function linked(body, inclusions) {

        var primaryResourceName = extractPrimaryResource(body);
        // a bit dirty, but necessary to avoid total refactor of sendResponse
        function extractPrimaryResource(body) {
            return _.first(_.filter(_.keys(body), function (key) {
                return !(key === 'meta' || key === 'links');
            }));
        }

        var modelName = inflect.singularize(primaryResourceName);
        var primarySchema = schemas[modelName];

        function isRemote(branch, key) {
            return null != branch && null != branch.refs && null != branch.refs[key] && null != branch.refs[key].def && null != branch.refs[key].def.baseUri;
        }

        function toIncludes(map) {
            var includes = [];

            function buildRecursively(map, prefix) {
                prefix = prefix || '';
                return _.forEach(map, function (value, key) {
                    if (_.isEmpty(value)) {
                        includes.push(prefix + key);
                    } else {
                        var newPrefix = prefix + key + '.';
                        buildRecursively(value, newPrefix)
                    }
                });
            }

            buildRecursively(map);
            return includes.join(',');
        }

        function mapToInclusionTree(typeMap, schema, path) {
            path = path || [];
            var branch = _.map(typeMap, function (value, key) {
                path.push(key);
                var subBranch = buildInclusionBranch([key], schema);
                if (!_.isEmpty(value)) {
                    if (isRemote(subBranch, key)) {
                        subBranch.refs[key].def.remoteIncludes = toIncludes(value);
                    } else {
                        var schemaName = schema[key];
                        var subMap = mapToInclusionTree(value, schemas[schemaName], path);
                        _.merge(subBranch.refs[key], subMap);
                    }
                }
                path.pop();
                return subBranch;
            });
            return _.reduce(branch, function (acc, item) {
                return _.merge(acc, item);
            });
        }

        var inclusionMap = _.map(inclusions,function (inclusionString) {
            var result = {};
            var node = result;
            inclusionString.split('.').forEach(function (token) {
                node = node[token] = {};
            });
            return result;
        });

        if (inclusionMap.length > 0) {
            inclusionMap = inclusionMap.reduce(function (acc, node) {
                return _.merge(acc, node);
            });
        }

        var inclusionTree = mapToInclusionTree(inclusionMap, primarySchema);

        // builds a tree representation out of a series of inclusion tokens
        function buildInclusionBranch(inclusionTokens, schema) {

            var inclusionToken = _.first(inclusionTokens);
            var type = _.isArray(schema[inclusionToken]) ? schema[inclusionToken][0] : schema[inclusionToken];
            var normalisedType = _.isPlainObject(type) ? type.ref : type;

            var linkDescriptor = {
                def: {type: normalisedType}
            };

            var baseUri = _.isPlainObject(type) ? type.baseUri : null;
            if (baseUri) {
                var remoteIncludes = _.drop(inclusionTokens, _.indexOf(inclusionTokens, inclusionToken) + 1).join('.');
                var remoteDescriptor = _.merge(linkDescriptor, {
                        def: {baseUri: baseUri, remoteIncludes: remoteIncludes}
                    }
                );
                return setRefs(remoteDescriptor);
            } else {
                var tokensRemaining = _.drop(inclusionTokens);
                if (tokensRemaining.length == 0) {
                    return setRefs(linkDescriptor);
                } else {
{'fraction_non_alphanumeric': 0.11158174996974464, 'fraction_numerical': 0.0008471499455403607, 'mean_word_length': 1.5865414710485133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2705969', 'n_tokens_mistral': 2118, 'n_tokens_neox': 2014, 'n_words': 461}