<commit_before>/*
 * - key combo Meta+S should fire on keyup
 * - key combo Meta+I+I should fire on keyup
 *
 **/

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define(function() {

  describe('tests suite', function() {
    it('should execute', function() {
      expect(true).toBe(true);
    });
  });

  describe('simple keyboard shortcut', function() {
    it('should call according callback', function() {
      var callback = jasmine.createSpy('callback');
      var shortcut = ['Meta', 'S'];
      mapShortcut(shortcut, callback);
      pressKeysAndKeyUp(shortcut);
      expect(callback).toHaveBeenCalled();
    });
  });
  
});

function mapShortcut(shortcut, callback) {
  var pressedKeys = [];

  function keyDown(key) {
    pressedKeys.push(key);
  }

  function keyUp() {
    if (shortcut.join('+') == pressedKeys.join('+')) {
      callback();
    }
  }
  keyboardUtil.addKeyDownListener(keyDown);
  keyboardUtil.addKeyUpListener(keyUp);
}

var keyboardUtil = {
  addKeyDownListener: function() {},
  addKeyUpListener: function() {}
};

// test utils

var keyDownListeners = [];
var keyUpListeners = [];
spyOn(keyboardUtil, 'addKeyDownListener').andCallFake(function(fn) {
  keyDownListeners.push(fn);
});
spyOn(keyboardUtil, 'addKeyUpListener').andCallFake(function(fn) {
  keyUpListeners.push(fn);
});
function pressKeysAndKeyUp(keys) {
  keys.forEach(function(key) {
    keyDownListeners[0](key);<commit_msg>Handle multiple keys shortcut.<commit_after>/*
 * - key combo Meta+S should fire on keyup
 * - key combo Meta+I+I should fire on keyup
 *
 **/

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define(function() {

  describe('tests suite', function() {
    it('should execute', function() {
      expect(true).toBe(true);
    });
  });

  describe('simple keyboard shortcut', function() {
    it('with one key should call according callback', function() {
      var callback = jasmine.createSpy('callback');
      var shortcut = ['Meta', 'S'];
      mapShortcuts([[shortcut, callback]]);
      pressKeysAndKeyUp(shortcut);
      expect(callback).toHaveBeenCalled();
    });
    it('with two keys should call according callback', function() {
      var callback = jasmine.createSpy('callback');
      var shortcut = ['Meta', 'I', 'I'];
      mapShortcuts([[shortcut, callback]]);
      pressKeysAndKeyUp(shortcut);
      expect(callback).toHaveBeenCalled();
    });
  });
  
});

function ShortcutManager() {
  keyboardUtil.addKeyDownListener(this._keyDown.bind(this));
  keyboardUtil.addKeyUpListener(this._keyUp.bind(this));
}
ShortcutManager.prototype = {
  _pressedKeys: [],
  _registeredShortcuts: [],
  registerShortcut: function(shortcut, callback) {
    this._registeredShortcuts.push([shortcut, callback]);
  },

  _keyDown: function(key) {
    this._pressedKeys.push(key);
  },

  _keyUp: function() {
    var pressedKeys = this._pressedKeys;
    var found = this._registeredShortcuts.filter(function(shortcut) {
      return shortcut[0].join('+') == pressedKeys.join('+');
    });
    if (found.length) {
      // Use the first shortcut map found.
      var firstShortcut = found[0];
      firstShortcut[1]();
    }
    this._pressedKeys = [];
  }
};

function mapShortcuts(shortcuts) {
  var manager = new ShortcutManager();
  shortcuts.forEach(function(shortcut) {
    manager.registerShortcut(shortcut[0], shortcut[1]);
  });
}

var keyboardUtil = {
  addKeyDownListener: function() {},
  addKeyUpListener: function() {}
};

// test utils

var keyDownListeners = [];
var keyUpListeners = [];
spyOn(keyboardUtil, 'addKeyDownListener').andCallFake(function(fn) {
  keyDownListeners.push(fn);
});
spyOn(keyboardUtil, 'addKeyUpListener').andCallFake(function(fn) {
  keyUpListeners.push(fn);
});
function pressKeysAndKeyUp(keys) {
  keys.forEach(function(key) {
    keyDownListeners[0](key);
{'fraction_non_alphanumeric': 0.15784061696658097, 'fraction_numerical': 0.0017994858611825194, 'mean_word_length': 4.181091877496671, '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': '1054367', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1218, 'n_words': 233}