HenrikJoreteg / wildemitter

A super lightweight EventEmitter similar to what comes in Node.js, but with a support for wildcard events '*'
MIT License
213 stars 21 forks source link

Wildcard matching issue #4

Closed aaronmccall closed 9 years ago

aaronmccall commented 10 years ago

This code at wildemitter.js#L131 unintentionally matches all wildcard events that have been registered:

split = item.split('*');

if (item === '*' || (split.length === 2 && eventName.slice(0, split[1].length) === split[1])) {
    result = result.concat(this.callbacks[item]);
}

The reason: split[1] is the empty part of the string after the '*' so we're comparing a 0 length slice of eventName ("") to split1.

Simply changing to

split = item.split('*');

if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) {
    result = result.concat(this.callbacks[item]);
}

will solve the problem because now we're comparing the eventName to the part of the wildcard before the asterisk.