Skip to content

Commit

Permalink
small refactoring + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Dec 9, 2024
1 parent 609c7a9 commit 85aec29
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
[![Build and test](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&07c28c31dc260cd38636ca8bd296cfc4)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&26ea018b3abc2f72f4987018534fef0c)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
![NPM Downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![jsDelivr Downloads](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded&n=1)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![Paid Support](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)
Expand Down
8 changes: 8 additions & 0 deletions __tests__/terminal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,14 @@ describe('Terminal utils', function() {
emitter.emit('test');
expect(handler.mock.calls.length).toEqual(0);
});
it('should not remove wrong event', () => {
var handler = jest.fn();
emitter.on('test', handler);
emitter.off('test_2');
emitter.emit('test');
emitter.emit('test');
expect(handler.mock.calls.length).toEqual(2);
});
it('should add mutiple event handlers', () => {
var foo = jest.fn();
var bar = jest.fn();
Expand Down
8 changes: 6 additions & 2 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
};
}
// -----------------------------------------------------------------------
// :: EventEmitter class
// :: EventEmitter class, created by ChatGPT (with small refactoring)
// -----------------------------------------------------------------------
function EventEmitter() {
this._events = {};
Expand All @@ -283,9 +283,13 @@
};
// -----------------------------------------------------------------------
EventEmitter.prototype.off = function(event, listener) {
if (!this._events[event]) {
return;
}

if (!listener) {
delete this._events[event];
} else if (this._events[event]) {
} else {
this._events[event] = this._events[event].filter(function(l) {
return l !== listener;
});
Expand Down
18 changes: 11 additions & 7 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Mon, 09 Dec 2024 15:38:00 +0000
* Date: Mon, 09 Dec 2024 20:30:04 +0000
*/
/* global define, Map, BigInt */
/* eslint-disable */
Expand Down Expand Up @@ -260,7 +260,7 @@
};
}
// -----------------------------------------------------------------------
// :: EventEmitter class
// :: EventEmitter class, created by ChatGPT (with small refactoring)
// -----------------------------------------------------------------------
function EventEmitter() {
this._events = {};
Expand All @@ -275,25 +275,29 @@
// -----------------------------------------------------------------------
EventEmitter.prototype.emit = function(event) {
if (this._events[event]) {
const args = Array.prototype.slice.call(arguments, 1);
var args = Array.prototype.slice.call(arguments, 1);
this._events[event].forEach(function(listener) {
listener.apply(null, args);
});
}
};
// -----------------------------------------------------------------------
EventEmitter.prototype.off = function(event, listener) {
if (!this._events[event]) {
return;
}

if (!listener) {
delete this._events[event];
} else if (this._events[event]) {
} else {
this._events[event] = this._events[event].filter(function(l) {
return l !== listener;
});
}
};
// -----------------------------------------------------------------------
EventEmitter.prototype.once = function(event, listener) {
const self = this;
var self = this;

function wrapper() {
self.off(event, wrapper);
Expand All @@ -304,7 +308,7 @@
};
// -----------------------------------------------------------------------
EventEmitter.prototype.wait_for = function(event) {
const deferred = new $.Deferred();
var deferred = new $.Deferred();

this.once(event, function() {
deferred.resolve();
Expand Down Expand Up @@ -5398,7 +5402,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Mon, 09 Dec 2024 15:38:00 +0000',
date: 'Mon, 09 Dec 2024 20:30:04 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/jquery.terminal.min.js.map

Large diffs are not rendered by default.

0 comments on commit 85aec29

Please sign in to comment.