<commit_before>define(['logManager'], function (logManager) {
  
  "use strict";

  var utils = function (core, META) {
    this.core = core;
    this.META = META;
    this.logger = logManager.create('utils');
  };

  utils.prototype.exists = function (initial_node, component_path, next) {
    var self = this;
    self.logger.info('exists()');

    var dirs = self.get_dirs(component_path);
    var curr_node = initial_node;
    check_component(0);

    function check_component (index) {
      if (index >= dirs.length) {
        next(true);
      } else {
        self.core.loadChildren(curr_node, function (err, children) {
          if (err) {
            self.logger.error(err);
            next(false);
          } else {
            var dir_node = null;
            for (var j = children.length - 1; j >= 0; j--) {
              var name = self.core.getAttribute(children[j], 'name');
              if (name === dirs[index]) {
                dir_node = children[j];
              }
            }
            if (dir_node == null) {
              return next(false);
            }
            curr_node = dir_node;
            check_component(index+1);
          }
        });
      }
    }

  };

  utils.prototype.md = function (initial_node, component_path, next) {
    var self = this;
    self.logger.info('md()');

    var dirs = self.get_dirs(component_path);
    var curr_node = initial_node;
    create_directory(0);

    function create_directory (index) {
      if (index >= dirs.length) {
        next(curr_node);
      } else {
        self.core.loadChildren(curr_node, function (err, children) {
          var dir_node = null;
          for (var j = children.length - 1; j >= 0; j--) {
            var name = self.core.getAttribute(children[j], 'name');
            if (name === dirs[index]) {
              dir_node = children[j];
            }
          }
          // TODO: Once we found a non-existent directory, we can create the rest
          if (dir_node == null) {
            dir_node = self.core.createNode({
              base: self.META.Folder,
              parent: curr_node
            });
            self.core.setAttribute(dir_node, 'name', dirs[index]);
          }
          curr_node = dir_node;
          create_directory(index+1);
        });
      }
    }

  };

  utils.prototype.get_dirs = function (component_path) {
    var path = require('path');
    var ext = path.extname(component_path);
    var dirs = '';
    if (ext === '') dirs = component_path;
    if (ext === '.nc') dirs = path.dirname(component_path);
    return dirs.split('/');
  };

  return utils;
});
<commit_msg>Add for_each_then_call_next<commit_after>define(['logManager'], function (logManager) {
  
  "use strict";

  var utils = function (core, META) {
    this.core = core;
    this.META = META;
    this.logger = logManager.create('utils');
  };

  utils.prototype.for_each_then_call_next = function (object, apply, next) {
    var self = this;
    self.logger.info('for_each_then_call_next()');
    var counter = 0, objects_length = Object.keys(object).length;
    for (var key in object) {
      apply(object[key], function () {
        counter++;
        if (counter >= objects_length) {
          next();
        }
      });
    }
  };

  utils.prototype.exists = function (initial_node, component_path, next) {
    var self = this;
    self.logger.info('exists()');

    var dirs = self.get_dirs(component_path);
    var curr_node = initial_node;
    check_component(0);

    function check_component (index) {
      if (index >= dirs.length) {
        next(true);
      } else {
        self.core.loadChildren(curr_node, function (err, children) {
          if (err) {
            self.logger.error(err);
            next(false);
          } else {
            var dir_node = null;
            for (var j = children.length - 1; j >= 0; j--) {
              var name = self.core.getAttribute(children[j], 'name');
              if (name === dirs[index]) {
                dir_node = children[j];
              }
            }
            if (dir_node == null) {
              return next(false);
            }
            curr_node = dir_node;
            check_component(index+1);
          }
        });
      }
    }

  };

  utils.prototype.md = function (initial_node, component_path, next) {
    var self = this;
    self.logger.info('md()');

    var dirs = self.get_dirs(component_path);
    var curr_node = initial_node;
    create_directory(0);

    function create_directory (index) {
      if (index >= dirs.length) {
        next(curr_node);
      } else {
        self.core.loadChildren(curr_node, function (err, children) {
          var dir_node = null;
          for (var j = children.length - 1; j >= 0; j--) {
            var name = self.core.getAttribute(children[j], 'name');
            if (name === dirs[index]) {
              dir_node = children[j];
            }
          }
          // TODO: Once we found a non-existent directory, we can create the rest
          if (dir_node == null) {
            dir_node = self.core.createNode({
              base: self.META.Folder,
              parent: curr_node
            });
            self.core.setAttribute(dir_node, 'name', dirs[index]);
          }
          curr_node = dir_node;
          create_directory(index+1);
        });
      }
    }

  };

  utils.prototype.get_dirs = function (component_path) {
    var path = require('path');
    var ext = path.extname(component_path);
    var dirs = '';
    if (ext === '') dirs = component_path;
    if (ext === '.nc') dirs = path.dirname(component_path);
    return dirs.split('/');
  };

  return utils;
});

{'fraction_non_alphanumeric': 0.13527336860670194, 'fraction_numerical': 0.002998236331569665, 'mean_word_length': 1.8743030917384693, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '603558', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1725, 'n_words': 379}