You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

periods.ts 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { translate, translateWithParameters } from './l10n';
  21. import { parseDate } from './dates';
  22. function getPeriod<T extends T.Period | T.PeriodMeasure>(periods: T[] | undefined, index: number) {
  23. if (!Array.isArray(periods)) {
  24. return undefined;
  25. }
  26. return periods.find(period => period.index === index);
  27. }
  28. export function getLeakPeriod<T extends T.Period | T.PeriodMeasure>(periods: T[] | undefined) {
  29. return getPeriod(periods, 1);
  30. }
  31. export function getPeriodLabel(
  32. period: T.Period | undefined,
  33. dateFormatter: (date: string) => string
  34. ) {
  35. if (!period) {
  36. return undefined;
  37. }
  38. let parameter = period.modeParam || period.parameter;
  39. if (period.mode === 'previous_version' && !parameter) {
  40. return translate('overview.period.previous_version_only_date');
  41. }
  42. if (period.mode === 'date' && parameter) {
  43. parameter = dateFormatter(parameter);
  44. }
  45. return translateWithParameters(`overview.period.${period.mode}`, parameter || '');
  46. }
  47. export function getPeriodDate(period?: { date?: string }): Date | undefined {
  48. return period && period.date ? parseDate(period.date) : undefined;
  49. }