utils/getService: Improve Functionality to run on O(n)

This commit is contained in:
Nishant Arora 2021-10-21 23:49:36 -06:00
parent b785658db1
commit db8543a0b4
No known key found for this signature in database
GPG key ID: 169797D53BE6B357

View file

@ -1,14 +1,15 @@
import { Service } from "../types/segments.model";
export function getService<T extends string>(...value: T[]): Service {
const serviceByName = Object.values(Service).reduce((acc, serviceName) => {
acc[serviceName.toLowerCase()] = serviceName;
return acc;
}, {} as Record<string, Service>);
for (const name of value) {
if (name) {
const service = Object.values(Service).find(
(val) => val.toLowerCase() === name.trim().toLowerCase()
);
if (service) {
return service;
}
if (name?.trim().toLowerCase() in serviceByName) {
return serviceByName[name.trim().toLowerCase()];
}
}