<commit_before>import {Injectable} from '@angular/core';
import * as moment from 'moment';

import {IJenkinsJob} from 'jenkins-api-ts-typings';
import {IJenkinsUser} from 'jenkins-api-ts-typings';
import {IJenkinsView} from 'jenkins-api-ts-typings';
import {IJenkinsBuild} from 'jenkins-api-ts-typings';
import {IJenkinsChangeSet} from 'jenkins-api-ts-typings';
import {IJenkinsNode} from 'jenkins-api-ts-typings';
import {IJenkinsAction} from 'jenkins-api-ts-typings';
import {JenkinsTimeInQueueAction} from 'jenkins-api-ts-typings';
import {JenkinsBuildStatus} from 'jenkins-api-ts-typings';

@Injectable()
export class UtilService {
    private readonly _INSTANCE_ID: string;

    constructor() {
        this._INSTANCE_ID = this.uuidv4();
    }

    isInvalid(objectToCheck: any): boolean {
        if (objectToCheck === undefined || objectToCheck === null) {
            return true;
        }

        if (objectToCheck instanceof Array) {
            return (objectToCheck as Array<any>).length === 0;
        }

        if (objectToCheck instanceof Map) {
            return (objectToCheck as Map<any, any>).size === 0;
        }

        return false;
    }

    padDate(dateTime: Date): string {
        if (this.isInvalid(dateTime)) {
            return new Date().toISOString().slice(0, 10);
        }

        return dateTime.toISOString().slice(0, 10);
    }

    padTime(dateTime: Date): string {
        if (this.isInvalid(dateTime)) {
            return "00:00";
        }

        let hours: string = ('0' + dateTime.getHours()).slice(-2);
        let minutes: string = ('0' + dateTime.getMinutes()).slice(-2);

        return hours + ":" + minutes;
    }

    padTimestamp(timestamp: number): number {

        if (this.isInvalid(timestamp) || timestamp === 0) {
            return 0;
        }

        if (timestamp < 0) {
            timestamp = timestamp * -1;
        }

        if (timestamp < 1) {
            timestamp = 1;
        }

        timestamp = Math.round(timestamp);

        let lowerMargin = 999999999999;
        let upperMargin = lowerMargin * 10;

        let maxIterations: number = 100;
        while (timestamp < lowerMargin && maxIterations > 0) {
            timestamp = timestamp * 10;
            maxIterations--;
        }

        maxIterations = 100;
        while (timestamp > upperMargin && maxIterations > 0) {
            timestamp = Math.round(timestamp / 10);
            maxIterations--;
        }

        timestamp = Math.round(timestamp);
        return timestamp;
    }

    isSameDate(dateTimeA: Date, dateTimeB: Date): boolean {
        var format: string = "DD/MM/YYYY";
        var momentA = moment(dateTimeA, format);
        var momentB = moment(dateTimeB, format);

        return momentA.isSame(momentB, "day") || momentA.format(format) === momentB.format(format);
    }

    isSameOrAfterDate(dateTimeA: Date, dateTimeB: Date): boolean {
        var format: string = "DD/MM/YYYY";
        var momentA = moment(dateTimeA, format);
        var momentB = moment(dateTimeB, format);

        return momentA.isSameOrAfter(momentB, "day");
    }

    isAfter(dateTimeA: Date, dateTimeB: Date): boolean {
        var momentA = moment(dateTimeA);
        var momentB = moment(dateTimeB);

        return momentA.isAfter(momentB);
    }
    
    simpleFormatDuration(durationInMinutes: number, useFullDescriptors: boolean = false): string {
        let days = 0;
        let hours = 0;
        let minutes = 0;
        
        if (durationInMinutes > (60 * 24)) {
            days = Math.floor(durationInMinutes / (60 * 24));
            let durationWithoutDays = durationInMinutes - (days * 60 * 24);
            hours = Math.floor(durationWithoutDays / 60);
            let durationWithoutDaysWithoutHours = durationWithoutDays - (hours * 60);
            minutes = Math.floor(durationWithoutDaysWithoutHours % 60);
        } else if(durationInMinutes > 60) {
            hours = Math.floor(durationInMinutes / 60);
            let durationWithoutHours = durationInMinutes - (hours * 60);
            minutes = Math.floor(durationWithoutHours % 60);
        } else {
            minutes = durationInMinutes;
        }
        
        let formatted: string = (days > 0 ? days + 'd ' : "") + (hours > 0 ? hours + 'h ' : "") + minutes + 'm';
        if(useFullDescriptors) {
            formatted = (days > 0 ? days + 'days ' : "") + (hours > 0 ? hours + 'hours ' : "") + minutes + 'minutes';
        }
        
        return formatted;
    }

    mapToArray<T>(values: Map<any, Array<T>>): Array<T> {

        if (this.isInvalid(values)) {
            return new Array<T>();
        }

        return Array.from(values.values()).reduce((a, b) => a.concat(b), []);
    }

    getAffectedPathsArray(builds: Map<any, Array<IJenkinsChangeSet>>): Array<string> {
        if (this.isInvalid(builds)) {
            return new Array<string>();
        }<commit_msg>Fix spacing when formatting date<commit_after>import {Injectable} from '@angular/core';
import * as moment from 'moment';

import {IJenkinsJob} from 'jenkins-api-ts-typings';
import {IJenkinsUser} from 'jenkins-api-ts-typings';
import {IJenkinsView} from 'jenkins-api-ts-typings';
import {IJenkinsBuild} from 'jenkins-api-ts-typings';
import {IJenkinsChangeSet} from 'jenkins-api-ts-typings';
import {IJenkinsNode} from 'jenkins-api-ts-typings';
import {IJenkinsAction} from 'jenkins-api-ts-typings';
import {JenkinsTimeInQueueAction} from 'jenkins-api-ts-typings';
import {JenkinsBuildStatus} from 'jenkins-api-ts-typings';

@Injectable()
export class UtilService {
    private readonly _INSTANCE_ID: string;

    constructor() {
        this._INSTANCE_ID = this.uuidv4();
    }

    isInvalid(objectToCheck: any): boolean {
        if (objectToCheck === undefined || objectToCheck === null) {
            return true;
        }

        if (objectToCheck instanceof Array) {
            return (objectToCheck as Array<any>).length === 0;
        }

        if (objectToCheck instanceof Map) {
            return (objectToCheck as Map<any, any>).size === 0;
        }

        return false;
    }

    padDate(dateTime: Date): string {
        if (this.isInvalid(dateTime)) {
            return new Date().toISOString().slice(0, 10);
        }

        return dateTime.toISOString().slice(0, 10);
    }

    padTime(dateTime: Date): string {
        if (this.isInvalid(dateTime)) {
            return "00:00";
        }

        let hours: string = ('0' + dateTime.getHours()).slice(-2);
        let minutes: string = ('0' + dateTime.getMinutes()).slice(-2);

        return hours + ":" + minutes;
    }

    padTimestamp(timestamp: number): number {

        if (this.isInvalid(timestamp) || timestamp === 0) {
            return 0;
        }

        if (timestamp < 0) {
            timestamp = timestamp * -1;
        }

        if (timestamp < 1) {
            timestamp = 1;
        }

        timestamp = Math.round(timestamp);

        let lowerMargin = 999999999999;
        let upperMargin = lowerMargin * 10;

        let maxIterations: number = 100;
        while (timestamp < lowerMargin && maxIterations > 0) {
            timestamp = timestamp * 10;
            maxIterations--;
        }

        maxIterations = 100;
        while (timestamp > upperMargin && maxIterations > 0) {
            timestamp = Math.round(timestamp / 10);
            maxIterations--;
        }

        timestamp = Math.round(timestamp);
        return timestamp;
    }

    isSameDate(dateTimeA: Date, dateTimeB: Date): boolean {
        var format: string = "DD/MM/YYYY";
        var momentA = moment(dateTimeA, format);
        var momentB = moment(dateTimeB, format);

        return momentA.isSame(momentB, "day") || momentA.format(format) === momentB.format(format);
    }

    isSameOrAfterDate(dateTimeA: Date, dateTimeB: Date): boolean {
        var format: string = "DD/MM/YYYY";
        var momentA = moment(dateTimeA, format);
        var momentB = moment(dateTimeB, format);

        return momentA.isSameOrAfter(momentB, "day");
    }

    isAfter(dateTimeA: Date, dateTimeB: Date): boolean {
        var momentA = moment(dateTimeA);
        var momentB = moment(dateTimeB);

        return momentA.isAfter(momentB);
    }
    
    simpleFormatDuration(durationInMinutes: number, useFullDescriptors: boolean = false): string {
        let days = 0;
        let hours = 0;
        let minutes = 0;
        
        if (durationInMinutes > (60 * 24)) {
            days = Math.floor(durationInMinutes / (60 * 24));
            let durationWithoutDays = durationInMinutes - (days * 60 * 24);
            hours = Math.floor(durationWithoutDays / 60);
            let durationWithoutDaysWithoutHours = durationWithoutDays - (hours * 60);
            minutes = Math.floor(durationWithoutDaysWithoutHours % 60);
        } else if(durationInMinutes > 60) {
            hours = Math.floor(durationInMinutes / 60);
            let durationWithoutHours = durationInMinutes - (hours * 60);
            minutes = Math.floor(durationWithoutHours % 60);
        } else {
            minutes = durationInMinutes;
        }
        
        let formatted: string = (days > 0 ? days + 'd ' : "") + (hours > 0 ? hours + 'h ' : "") + minutes + 'm';
        if(useFullDescriptors) {
            formatted = (days > 0 ? days + ' days ' : "") + (hours > 0 ? hours + ' hours ' : "") + minutes + ' minutes';
        }
        
        return formatted;
    }

    mapToArray<T>(values: Map<any, Array<T>>): Array<T> {

        if (this.isInvalid(values)) {
            return new Array<T>();
        }

        return Array.from(values.values()).reduce((a, b) => a.concat(b), []);
    }

    getAffectedPathsArray(builds: Map<any, Array<IJenkinsChangeSet>>): Array<string> {
        if (this.isInvalid(builds)) {
            return new Array<string>();
        }
{'fraction_non_alphanumeric': 0.12786220871327253, 'fraction_numerical': 0.016616008105369808, 'mean_word_length': 2.281582446808511, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3103800', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2889, 'n_words': 678}