refactor(core): use makeMap for faster string match checks (#282)
This commit is contained in:
15
packages/shared/src/makeMap.ts
Normal file
15
packages/shared/src/makeMap.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Make a map and return a function for checking if a key
|
||||
* is in that map.
|
||||
*/
|
||||
export function makeMap(
|
||||
str: string,
|
||||
expectsLowerCase?: boolean
|
||||
): (key: string) => boolean {
|
||||
const map: Record<string, boolean> = Object.create(null)
|
||||
const list: Array<string> = str.split(',')
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
map[list[i]] = true
|
||||
}
|
||||
return expectsLowerCase ? val => !!map[val.toLowerCase()] : val => !!map[val]
|
||||
}
|
||||
Reference in New Issue
Block a user