All files / src/utils utils.ts

19.64% Statements 11/56
8.82% Branches 3/34
17.39% Functions 4/23
20.83% Lines 10/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122      2x 232x 1x       2x           2x       4x       2x                                                         2x                                                   2x     2x                                                                                
import { LOCALES } from "./constants";
import { snakeCase } from "lodash";
 
export const findLocale = (localeCode: string, fallback = "en") =>
  LOCALES.find(currentLocale => currentLocale.value === localeCode) ||
  (LOCALES.find(currentLocale => currentLocale.value === fallback) as {
    [key: string]: string;
  });
 
export const STATUS_CODES_TO_MESSAGES: { [key: number]: string } = {
  // if this map starts growing big, try to find more standardized language
  403: "You don't have permission to do this",
  404: "Could not find that resource"
};
 
const MESSAGES_TO_STATUS_CODES: { [key: string]: number } = Object.entries(
  // todo reformat this to use lodash's invert
  STATUS_CODES_TO_MESSAGES
).reduce<{ [key: string]: number }>(
  (result, [key, value]) => ({ ...result, [value]: parseInt(key) }),
  {}
);
 
export const getPrettyError = (
  errors: { [key: string]: string[] | undefined } | undefined | string,
  field?: string
) => {
  if (!errors) return;
 
  if (typeof errors === "string") return field ? undefined : errors;
 
  const errorList: string[] | undefined = field
    ? errors[field]
    : errors["__all__"];
  if (!errorList) return;
 
  return errorList.join(", ");
};
 
export function getCustomErrorMessage(
  errorMessage: string | undefined,
  statusCodesWeCareAbout: { [key: number]: string }
) {
  const statusCode = errorMessage
    ? MESSAGES_TO_STATUS_CODES[errorMessage]
    : undefined;
  const statusMessage = statusCode
    ? statusCodesWeCareAbout[statusCode]
    : undefined;
  return statusMessage || errorMessage;
}
 
export const keysToSnakeCase = (item?: any) => {
  const isArray = (a: any) => {
    return Array.isArray(a);
  };
 
  const isObject = (o: any) => {
    return o === Object(o) && !isArray(o) && typeof o !== "function";
  };
 
  if (isObject(item)) {
    const newItem: { [key: string]: any } = {};
 
    Object.keys(item).forEach(k => {
      newItem[snakeCase(k)] = keysToSnakeCase(item[k]);
    });
 
    return newItem;
  } else if (isArray(item)) {
    return item.map((i: any) => {
      return keysToSnakeCase(i);
    });
  }
 
  return item;
};
 
export const buildPartialSearchQuery = (query: string): string =>
  `${query.replace(new RegExp(" ", "g"), "* ")}`;
 
export const delay = (seconds: number) =>
  new Promise(resolve => setTimeout(resolve, seconds * 1000));
 
export function debug(...messages: string[]) {
  console.log(messages);
}
 
type Order = "asc" | "desc";
 
function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
  if (b[orderBy] < a[orderBy]) {
    return -1;
  }
  if (b[orderBy] > a[orderBy]) {
    return 1;
  }
  return 0;
}
 
export function getComparator<Key extends keyof any>(
  order: Order,
  orderBy: Key
): (
  a: { [key in Key]: number | string },
  b: { [key in Key]: number | string }
) => number {
  return order === "desc"
    ? (a, b) => descendingComparator(a, b, orderBy)
    : (a, b) => -descendingComparator(a, b, orderBy);
}
 
export function stableSort<T>(array: T[], comparator: (a: T, b: T) => number) {
  const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
  stabilizedThis.sort((a, b) => {
    const order = comparator(a[0], b[0]);
    if (order !== 0) return order;
    return a[1] - b[1];
  });
  return stabilizedThis.map(el => el[0]);
}