Skip to content

Commit bc737b8

Browse files
committed
small refactoring + update tests
1 parent 609c7a9 commit bc737b8

6 files changed

+25
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
99
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
1010
[![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)
11-
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&07c28c31dc260cd38636ca8bd296cfc4)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
11+
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&30173c7672fc43af7598ae2f8ea22778)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
1212
![NPM Downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
1313
[![jsDelivr Downloads](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded&n=1)](https://www.jsdelivr.com/package/npm/jquery.terminal)
1414
[![Paid Support](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)

__tests__/terminal.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,14 @@ describe('Terminal utils', function() {
16541654
emitter.emit('test');
16551655
expect(handler.mock.calls.length).toEqual(0);
16561656
});
1657+
it('should not remove wrong event', () => {
1658+
var handler = jest.fn();
1659+
emitter.on('test', handler);
1660+
emitter.off('test_2');
1661+
emitter.emit('test');
1662+
emitter.emit('test');
1663+
expect(handler.mock.calls.length).toEqual(2);
1664+
});
16571665
it('should add mutiple event handlers', () => {
16581666
var foo = jest.fn();
16591667
var bar = jest.fn();

js/jquery.terminal-src.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@
260260
};
261261
}
262262
// -----------------------------------------------------------------------
263-
// :: EventEmitter class
263+
// :: EventEmitter class, created by ChatGPT (with small refactoring)
264264
// -----------------------------------------------------------------------
265265
function EventEmitter() {
266266
this._events = {};
@@ -283,9 +283,11 @@
283283
};
284284
// -----------------------------------------------------------------------
285285
EventEmitter.prototype.off = function(event, listener) {
286+
if (!this._events[event]) return;
287+
286288
if (!listener) {
287289
delete this._events[event];
288-
} else if (this._events[event]) {
290+
} else {
289291
this._events[event] = this._events[event].filter(function(l) {
290292
return l !== listener;
291293
});

js/jquery.terminal.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* broken image by Sophia Bai from the Noun Project (CC-BY)
4343
*
44-
* Date: Mon, 09 Dec 2024 15:38:00 +0000
44+
* Date: Mon, 09 Dec 2024 20:12:39 +0000
4545
*/
4646
/* global define, Map, BigInt */
4747
/* eslint-disable */
@@ -260,7 +260,7 @@
260260
};
261261
}
262262
// -----------------------------------------------------------------------
263-
// :: EventEmitter class
263+
// :: EventEmitter class, created by ChatGPT (with small refactoring)
264264
// -----------------------------------------------------------------------
265265
function EventEmitter() {
266266
this._events = {};
@@ -275,25 +275,27 @@
275275
// -----------------------------------------------------------------------
276276
EventEmitter.prototype.emit = function(event) {
277277
if (this._events[event]) {
278-
const args = Array.prototype.slice.call(arguments, 1);
278+
var args = Array.prototype.slice.call(arguments, 1);
279279
this._events[event].forEach(function(listener) {
280280
listener.apply(null, args);
281281
});
282282
}
283283
};
284284
// -----------------------------------------------------------------------
285285
EventEmitter.prototype.off = function(event, listener) {
286+
if (!this._events[event]) return;
287+
286288
if (!listener) {
287289
delete this._events[event];
288-
} else if (this._events[event]) {
290+
} else {
289291
this._events[event] = this._events[event].filter(function(l) {
290292
return l !== listener;
291293
});
292294
}
293295
};
294296
// -----------------------------------------------------------------------
295297
EventEmitter.prototype.once = function(event, listener) {
296-
const self = this;
298+
var self = this;
297299

298300
function wrapper() {
299301
self.off(event, wrapper);
@@ -304,7 +306,7 @@
304306
};
305307
// -----------------------------------------------------------------------
306308
EventEmitter.prototype.wait_for = function(event) {
307-
const deferred = new $.Deferred();
309+
var deferred = new $.Deferred();
308310

309311
this.once(event, function() {
310312
deferred.resolve();
@@ -5398,7 +5400,7 @@
53985400
// -------------------------------------------------------------------------
53995401
$.terminal = {
54005402
version: 'DEV',
5401-
date: 'Mon, 09 Dec 2024 15:38:00 +0000',
5403+
date: 'Mon, 09 Dec 2024 20:12:39 +0000',
54025404
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
54035405
color_names: [
54045406
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',

js/jquery.terminal.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/jquery.terminal.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)