import _ from 'lodash';

export const createFilteredObject = <T>(originalObject: Object, inclusionList: string[]): T => {
  const filteredObject = _.pick(originalObject, inclusionList);
  return filteredObject as T;
};

export const setDelay = (ms: number) => {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
};

export const convertToArray = (selectedBorrowerIds: string | string[]) => {
  return typeof selectedBorrowerIds === 'string' ? [selectedBorrowerIds] : selectedBorrowerIds;
};

export const sanitizeString = (stringToSanitize: string) => {
  const regexString = /[\/\\:?*"<>|']/g;
  return stringToSanitize.replace(regexString, '_');
};

export const reorderObject = <T>(obj: T, keyOrder: string[]): T => {
  const reorderedObject: T = {} as T;
  keyOrder.forEach(key => {
    if (obj.hasOwnProperty(key)) {
      reorderedObject[key] = obj[key];
    }
  });
  return reorderedObject;
}
