Skip to content

Commit

Permalink
chore(TypeScript): Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 28, 2020
1 parent 924eb00 commit 6e4b64f
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 275 deletions.
38 changes: 19 additions & 19 deletions __tests__/emit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("should return false when there are not events to emit", () => {
const e = new EventEmitter();
Expand All @@ -7,13 +7,13 @@ it("should return false when there are not events to emit", () => {
expect(e.emit("bar")).toBe(false);
});

it("should emit with context", done => {
it("should emit with context", (done) => {
const context = { bar: "baz" };
const e = new EventEmitter();

e.on(
"foo",
function(this: any, bar) {
function (this: unknown, bar) {
expect(bar).toBe("bar");
expect(this).toEqual(context);

Expand All @@ -23,13 +23,13 @@ it("should emit with context", done => {
).emit("foo", "bar");
});

it("should emit with context, multiple arguments (force apply)", done => {
it("should emit with context, multiple arguments (force apply)", (done) => {
const context = { bar: "baz" };
const e = new EventEmitter();

e.on(
"foo",
function(this: any, bar) {
function (this: unknown, bar) {
expect(bar).toBe("bar");
expect(this).toEqual(context);

Expand All @@ -43,14 +43,14 @@ it("should be able to emit the function with multiple arguments", () => {
const e = new EventEmitter();

for (let i = 0; i < 100; i++) {
(function(j) {
const args: any[] = [];
(function (j) {
const args: number[] = [];

for (let i = 0; i < j; i++) {
args.push(j);
}

e.once("args", function() {
e.once("args", function () {
expect(arguments.length).toBe(args.length);
});

Expand All @@ -63,14 +63,14 @@ it("should be able to emit the function with multiple arguments, multiple listen
const e = new EventEmitter();

for (let i = 0; i < 100; i++) {
(function(j) {
const args: any[] = [];
(function (j) {
const args: number[] = [];

for (let i = 0; i < j; i++) {
args.push(j);
}

e.once("args", function() {
e.once("args", function () {
expect(arguments.length).toBe(args.length);
});

Expand All @@ -84,7 +84,7 @@ it("should be able to emit with context, multiple listeners (force loop)", () =>

e.on(
"foo",
function(this: any, bar) {
function (this: unknown, bar) {
expect(this).toEqual({ foo: "bar" });
expect(bar).toBe("bar");
},
Expand All @@ -93,7 +93,7 @@ it("should be able to emit with context, multiple listeners (force loop)", () =>

e.on(
"foo",
function(this: any, bar) {
function (this: unknown, bar) {
expect(this).toEqual({ bar: "baz" });
expect(bar).toBe("bar");
},
Expand All @@ -107,7 +107,7 @@ it("should be able to emit with different contexts", () => {
const e = new EventEmitter();
let pattern = "";

function writer(this: any) {
function writer(this: unknown) {
pattern += this;
}

Expand All @@ -123,7 +123,7 @@ it("should return true when there are events to emit", () => {
const e = new EventEmitter();
let called = 0;

e.on("foo", function() {
e.on("foo", function () {
called++;
});

Expand All @@ -132,10 +132,10 @@ it("should return true when there are events to emit", () => {
expect(called).toBe(1);
});

it("receives the emitted events", done => {
it("receives the emitted events", (done) => {
const e = new EventEmitter();

e.on("data", function(a, b, c, d, undef) {
e.on("data", function (a, b, c, d, undef) {
expect(a).toBe("foo");
expect(b).toEqual(e);
expect(c).toBeInstanceOf(Date);
Expand All @@ -152,11 +152,11 @@ it("emits to all event listeners", () => {
const e = new EventEmitter();
const pattern: string[] = [];

e.on("foo", function() {
e.on("foo", function () {
pattern.push("foo1");
});

e.on("foo", function() {
e.on("foo", function () {
pattern.push("foo2");
});

Expand Down
11 changes: 4 additions & 7 deletions __tests__/eventNames.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("returns an empty array when there are no events", () => {
const e = new EventEmitter();

expect(e.eventNames()).toEqual([]);

// eslint-disable-next-line @typescript-eslint/no-empty-function
e.on("foo", () => {
});
e.on("foo", () => {});
e.removeAllListeners("foo");

expect(e.eventNames()).toEqual([]);
Expand All @@ -17,12 +16,10 @@ it("returns an array listing the events that have listeners", () => {
const e = new EventEmitter();

// eslint-disable-next-line @typescript-eslint/no-empty-function
function bar() {
}
function bar() {}

// eslint-disable-next-line @typescript-eslint/no-empty-function
e.on("foo", () => {
});
e.on("foo", () => {});
e.on("bar", bar);

expect(e.eventNames()).toEqual(["foo", "bar"]);
Expand Down
8 changes: 3 additions & 5 deletions __tests__/listenerCount.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("returns the number of listeners for a given event", () => {
const e = new EventEmitter();

expect(e.listenerCount("foo")).toBe(0);

// eslint-disable-next-line @typescript-eslint/no-empty-function
e.on("foo", function() {
});
e.on("foo", function () {});

expect(e.listenerCount("foo")).toBe(1);

// eslint-disable-next-line @typescript-eslint/no-empty-function
e.on("foo", function() {
});
e.on("foo", function () {});

expect(e.listenerCount("foo")).toBe(2);
});
8 changes: 3 additions & 5 deletions __tests__/listeners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("returns an empty array if no listeners are specified", () => {
const e = new EventEmitter();
Expand All @@ -11,8 +11,7 @@ it("returns an array of function", () => {
const e = new EventEmitter();

// eslint-disable-next-line @typescript-eslint/no-empty-function
function foo() {
}
function foo() {}

e.on("foo", foo);

Expand All @@ -25,8 +24,7 @@ it("is not vulnerable to modifications", () => {
const e = new EventEmitter();

// eslint-disable-next-line @typescript-eslint/no-empty-function
function foo() {
}
function foo() {}

e.on("foo", foo);

Expand Down
24 changes: 14 additions & 10 deletions __tests__/on.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("throws an error if the listener is not a function", () => {
it("should listen to all the emits", () => {
const e = new EventEmitter();
let calls = 0;

try {
e.on("foo", "bar" as any);
} catch (ex) {
expect(ex).toBeInstanceOf(TypeError);
expect(ex.message).toBe("'listener' must be a function");
return;
}
e.on("foo", () => {
calls++;
});

throw new Error("oops");
e.emit("foo");
e.emit("foo");
e.emit("foo");
e.emit("foo");
e.emit("foo");

expect(e.listeners("foo").length).toBe(1);
expect(calls).toBe(5);
});
6 changes: 3 additions & 3 deletions __tests__/once.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("only emits it once", () => {
const e = new EventEmitter();
Expand Down Expand Up @@ -62,13 +62,13 @@ it("only emits once for multiple events", () => {
expect(multi).toBe(5);
});

it("only emits once with context", done => {
it("only emits once with context", (done) => {
const context = { foo: "bar" };
const e = new EventEmitter();

e.once(
"foo",
function(this: any, bar) {
function (this: unknown, bar) {
expect(this).toEqual(context);
expect(bar).toBe("bar");

Expand Down
2 changes: 1 addition & 1 deletion __tests__/removeAllListeners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("removes all events for the specified events", () => {
const e = new EventEmitter();
Expand Down
11 changes: 4 additions & 7 deletions __tests__/removeListener.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("removes only the first listener matching the specified listener", () => {
const e = new EventEmitter();

// eslint-disable-next-line @typescript-eslint/no-empty-function
function foo() {
}
function foo() {}

// eslint-disable-next-line @typescript-eslint/no-empty-function
function bar() {
}
function bar() {}

// eslint-disable-next-line @typescript-eslint/no-empty-function
function baz() {
}
function baz() {}

e.on("foo", foo);
e.on("bar", bar);
Expand Down
6 changes: 3 additions & 3 deletions __tests__/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from "../src";
import EventEmitter from "../src";

it("should work with ES6 symbols", done => {
it("should work with ES6 symbols", (done) => {
const e = new EventEmitter();
const event = Symbol("cows");
const unknown = Symbol("moo");
Expand All @@ -10,7 +10,7 @@ it("should work with ES6 symbols", done => {
expect(e.listeners(unknown)).toEqual([]);
expect(arg).toBe("bar");

function bar(onced: any) {
function bar(onced: unknown) {
expect(e.listenerCount(unknown)).toBe(0);
expect(e.listeners(unknown)).toEqual([]);
expect(onced).toBe("foo");
Expand Down
Loading

0 comments on commit 6e4b64f

Please sign in to comment.