import { Injectable } from '@angular/core'; import { Timezone, iCompareOnId, TournamentDay, TournamentTicketSlice, Font } from 'tournee.businesslogic'; import * as Moment from 'moment'; import * as number_to_words from 'number-to-words'; import { ScrollToConfigOptions } from '@nicky-lenaers/ngx-scroll-to'; import { HttpWrapperService } from './http-wrapper.service'; @Injectable() export class UtilityService { constructor(private _http: HttpWrapperService) {} static scroll_to_head: ScrollToConfigOptions = { target: 'content' }; ensure_http(href: string) { if (href.startsWith('http://') || href.startsWith('https://')) { return; } return 'https://' + href; } elipsify(input: string) { if (input.length > 200) { return input.slice(0, 199) + '...' } return input; } customise_branding_font_size(font: Font, size: string) { let result = font; result['font-size'] = size; return result; } static sortMoments(moments: Array) { return moments.sort((a, b) => { if (a.isBefore(b)) { return -1; } if (b.isBefore(a)) { return 1 } return 0; }); } momentRanges(moments: Array) : Array { if (moments.length == 0) { return [""]; } if (moments.length == 1) { return [moments[0].format('DD MMM YYYY')]; } moments = UtilityService.sortMoments(moments); let _result : Array = []; let _firstMoment = moments[0] for (let i = 0; i < moments.length; i++) { // check the NEXT date let _thisMoment = moments[i]; let _nextMoment = moments[i + 1]; if (!_nextMoment) { _result.push(_firstMoment.format('DD MMM YYYY') + ' - ' + _thisMoment.format('DD MMM YYYY')); break; } else { if (_nextMoment.diff(_thisMoment, 'days') > 1) { _result.push(_firstMoment.format('DD MMM YYYY') + ' - ' + _thisMoment.format('DD MMM YYYY')); _firstMoment = _nextMoment; } } } return _result; } dateRanges(dates: Array) : Array { return this.momentRanges(dates.map((d) => { return Moment(d); })); } stringRanges(date_strings: Array) : Array { return this.momentRanges(date_strings.map((d) => { return Moment(d, 'DD MMM YYYY'); })); } dayRanges(days: Array) { return this.momentRanges(days.map((d) => { return d.date; })); } formatDateRange(fromDate?:Moment.Moment, toDate?: Moment.Moment) : string { if (!fromDate && !toDate) { return '-' } if (!fromDate) { return '- ' + toDate.format('DD MMM YYYY'); } if (!toDate) { return fromDate.format('DD MMM YYYY') + '-'; } if (fromDate.diff(toDate, 'days') == 0) { return fromDate.format('DD MMM YYYY'); } else { return fromDate.format('DD MMM YYYY') + ' - ' + toDate.format('DD MMM YYYY'); } } formatStringRange(fromDate:string, toDate: string) : string { if (fromDate == toDate) { return fromDate; } else { return fromDate + ' - ' + toDate; } } formatDateRangeFromDayArray(days: Array) { if (days.length == 0) { return ""; } return this.formatDateRange(days[0].original_date, days[days.length - 1].original_date); } textify_number(number) { if (number == undefined || number == null) { return '-' } return number_to_words.toWords(number); } compare_by_id(o1: iCompareOnId, o2 : iCompareOnId) { if (o1 && o2) { return o1.id == o2.id; } if (!o1 && !o2) { return true; } return false; } compare_timezones(t1: Timezone, t2: Timezone) { if (t1 && t2) { return t1.name == t2.name; } if (!t1 && !t2) { return true; } return false; } compare_slice(ts1: TournamentTicketSlice, ts2: TournamentTicketSlice) { if (ts1 && ts2) { return ts1.slice_slug == ts2.slice_slug; } if (!ts1 && !ts2) { return true; } return false; } validate_url(url: string) { return this._http.bypassGet(url); } }