Skip to content

Commit

Permalink
apply prettier formatting to all files
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jul 4, 2023
1 parent 3f81679 commit 05f4ce6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export type WildcardHandler<T = Record<string, unknown>> = (

// An array of all currently registered event handlers for a type
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<
WildcardHandler<T>
>;

// A map of event types and their corresponding event handlers.
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
Expand All @@ -24,11 +26,16 @@ export interface Emitter<Events extends Record<EventType, unknown>> {
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
on(type: '*', handler: WildcardHandler<Events>): void;

off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
off<Key extends keyof Events>(
type: Key,
handler?: Handler<Events[Key]>
): void;
off(type: '*', handler: WildcardHandler<Events>): void;

emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
emit<Key extends keyof Events>(
type: undefined extends Events[Key] ? Key : never
): void;
}

/**
Expand All @@ -45,7 +52,6 @@ export default function mitt<Events extends Record<EventType, unknown>>(
all = all || new Map();

return {

/**
* A Map of event names to registered handler functions.
*/
Expand All @@ -61,8 +67,7 @@ export default function mitt<Events extends Record<EventType, unknown>>(
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (handlers) {
handlers.push(handler);
}
else {
} else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
}
},
Expand All @@ -79,8 +84,7 @@ export default function mitt<Events extends Record<EventType, unknown>>(
if (handlers) {
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
else {
} else {
all!.set(type, []);
}
}
Expand Down
16 changes: 4 additions & 12 deletions test/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ describe('mitt#', () => {

describe('properties', () => {
it('should expose the event handler map', () => {
expect(inst)
.to.have.property('all')
.that.is.a('map');
expect(inst).to.have.property('all').that.is.a('map');
});
});

describe('on()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('on')
.that.is.a('function');
expect(inst).to.have.property('on').that.is.a('function');
});

it('should register handler for new type', () => {
Expand Down Expand Up @@ -111,9 +107,7 @@ describe('mitt#', () => {

describe('off()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('off')
.that.is.a('function');
expect(inst).to.have.property('off').that.is.a('function');
});

it('should remove handler for type', () => {
Expand Down Expand Up @@ -165,9 +159,7 @@ describe('mitt#', () => {

describe('emit()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('emit')
.that.is.a('function');
expect(inst).to.have.property('emit').that.is.a('function');
});

it('should invoke handler for type', () => {
Expand Down

0 comments on commit 05f4ce6

Please sign in to comment.