import isPlainObject from 'lodash/isPlainObject' import reduce from 'lodash/reduce' import { InvalidParametersError } from './customErrors' export const accountType = (type: string) => { const options = [ 'patient', 'provider', 'marketer', 'company', 'staffing_company', 'guest', ] } export const loginObject = ( data: any, { type, throw: _throw = false }: any = {}, ) => { let errorMsg if (!data) { errorMsg = 'Missing email and password for login' } else if (data.email === undefined) { errorMsg = 'The email cannot be undefined' } else if (data.password === undefined) { errorMsg = 'The password cannot be undefined' } if (errorMsg) { if (_throw) throw new InvalidParametersError(errorMsg) return false } return true } interface ObjectKeysResult { keys: string[] validKeys: string[] invalidKeys: string[] } export const objectKeys = ( keys: string[], collection: Array | object, ): ObjectKeysResult => { let result: ObjectKeysResult = { keys: [], validKeys: [], invalidKeys: [] } let collKeys: Array = [] if (Array.isArray(collection)) collKeys = collection else if (isPlainObject(collection)) collKeys = Object.keys(collection) result = reduce( collKeys, (acc: any, collKey: string | number) => { const stringified = String(collKey) const valid = keys.includes(stringified) const insideValid = acc.validKeys.includes(stringified) const insideInvalid = acc.invalidKeys.includes(stringified) if (valid && !insideValid) acc.validKeys.push(stringified) else if (!valid && !insideInvalid) acc.invalidKeys.push(stringified) return acc }, result, ) return result }