utils.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { scale } from "chroma-js";
  2. export function checkDateOrder(first: string, second: string) {
  3. const firstTs = Date.parse(first);
  4. const secondTs = Date.parse(second);
  5. return firstTs <= secondTs;
  6. }
  7. export function mustCheckDateOrder(first: string, second: string) {
  8. if (!checkDateOrder(first, second)) {
  9. throw new Error(
  10. "La fecha inicial no puede ser posterior a la fecha final."
  11. );
  12. }
  13. }
  14. function campYear(ISOFrom: string): number {
  15. // El año de la temporada 2021 - 2022 es 2022
  16. // "El año empieza el 1 de Octubre"
  17. // Offset desde el 1 de Octubre hasta el 1 de Enero
  18. const offset = 7948800000;
  19. const offsetDate = new Date(Date.parse(ISOFrom) + offset);
  20. return offsetDate.getFullYear();
  21. }
  22. // Computa un string para que el usuario identifique la temporada que esta viendo
  23. export function campString(
  24. ISOFrom: string,
  25. ISOTo: string,
  26. withVintage?: boolean
  27. ) {
  28. const yr = campYear(ISOFrom);
  29. // Con vendimia
  30. if (withVintage) {
  31. return `${yr - 1} - ${yr} - Vendimia ${yr} `;
  32. }
  33. // Sin vendimia
  34. return `${yr - 1} - ${yr}`;
  35. }
  36. // Escalas de colores
  37. export const orangeScale = (domain: number[]) =>
  38. scale(["white", "orange"]).domain(domain);
  39. export const redScale = (domain: number[]) =>
  40. scale(["white", "rgb(192, 0, 0)"]).domain(domain);
  41. export const blueScale = (domain: number[]) =>
  42. scale(["white", "rgb(58, 145, 207)"]).domain(domain);
  43. export const greenScale = (domain: number[]) =>
  44. scale(["white", "rgb(169, 208, 142)"]).domain(domain);
  45. export const yellowScale = (domain: number[]) =>
  46. scale(["white", "rgb(255, 255, 0)"]).domain(domain);
  47. export const grayScale = (domain: number[]) =>
  48. scale(["white", "rgb(169, 169, 169)"]).domain(domain);