core/src/common/full-matcher.ts

50 lines
1.1 KiB
TypeScript

/**
* Compare full IDs of rooms or users.
* @example
* // returns true
* fullIDMatcher('foo/bar/#test', 'foo/bar/*')
* @param compare ID to compare
* @param id ID to compare against
*/
export function fullIDMatcher(compare: string, id: string): boolean {
if (!compare) {
return false;
}
const splitter = compare.split('/');
if (id === '*') {
return true;
}
const spl = id.split('/');
if (spl[0] !== splitter[0] || (spl[1] !== splitter[1] && spl[1] !== '*')) {
return false;
}
// Match server cases, things like Discord need this
if (spl.length > 3 && spl[2].indexOf('s:') === 0 && splitter.length > 3) {
if (spl[2] !== splitter[2] && spl[2] !== '*') {
return false;
}
if (spl[3] !== splitter[3] && spl[3] !== '*') {
return false;
}
return true;
// Match regular cases
} else if (spl.length === 3 && splitter.length >= 3) {
if (spl[2] !== splitter[2] && spl[2] !== '*') {
return false;
}
return true;
// Match wildcard instance
} else if (spl.length === 2 && spl[1] === '*') {
return true;
}
return false;
}