<commit_before>import colorConverter from '~/utils/color-converter';
import { Icon, RelatedApplication, CodeError } from '~/store/modules/generator';

export const helpers = {
    MEMBER_PREFIX: 'mjs_',
    COLOR_OPTIONS: {
        none: 'none',
        transparent: 'transparent',
        pick: 'pick'
    },

    isValidUrl(siteUrl: string): boolean {
        return /^(http|https):\/\/[^ "]+$/.test(siteUrl);
    },

    getImageIconSize(aSrc: string): Promise<{ width: number, height: number }> {
        return new Promise(resolve => {
            if (typeof document === 'undefined') {
                resolve({ width: -1, height: -1 });
            }

            let tmpImg = document.createElement('img');
            tmpImg.onload = () => {
                resolve({
                    width: tmpImg.width,
                    height: tmpImg.height
                });
            };

            tmpImg.src = aSrc;
        });
    },

    prepareIconsUrls(icons: Icon[], baseUrl: string) {
        return icons.map(icon => {
            if (!icon.src.includes('http')) {
                //remove non-base scope path for sites like mastodon and billthis
                const pathArray = baseUrl.split( '/' );
                const protocol = pathArray[0];
                const host = pathArray[2];
                baseUrl = protocol + '//' + host; 
                //remove posible trailing/leading slashes
                icon.src = `${baseUrl.replace(/\/$/, '')}/${icon.src.replace(/^\/+/g, '')}`;
            }

            return icon;
        });
    },

    async getImageDataURI(file: File): Promise<string> {
        return new Promise<string>(resolve => {
            const reader = new FileReader();

            reader.onload = (aImg: any) => {
                const result: string = aImg.target.result;
                resolve(result);
            };

            reader.readAsDataURL(file);
        });
    },

    hasRelatedApplicationErrors(app: RelatedApplication): string | undefined {
        if (!app.platform) {
            return 'error.enter_platform';
        }

        if (!app.url && !app.id) {
            return 'error.enter_url';
        }

        const urlRegExpr = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.?[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
        if (app.url && !urlRegExpr.test(app.url)) {
            return 'error.enter_valid_url';
        }

        return;
    },

    fixColorFromServer(color: string): string {
        if (!color) {
            return '';
        }

        return '#' + colorConverter.toHexadecimal(color).slice(4, 10);
    },

    sumIssues(errors: CodeError[] | null): number {
        if (!errors) {
            return 0;
        }

        let total = 0;
        errors.forEach(error => {
            if (error.issues && error.issues.length) {
                total += error.issues.length;
            }
        });

        return total;
    }
};<commit_msg>Avoid adding base url to data:images<commit_after>import colorConverter from '~/utils/color-converter';
import { Icon, RelatedApplication, CodeError } from '~/store/modules/generator';

export const helpers = {
    MEMBER_PREFIX: 'mjs_',
    COLOR_OPTIONS: {
        none: 'none',
        transparent: 'transparent',
        pick: 'pick'
    },

    isValidUrl(siteUrl: string): boolean {
        return /^(http|https):\/\/[^ "]+$/.test(siteUrl);
    },

    getImageIconSize(aSrc: string): Promise<{ width: number, height: number }> {
        return new Promise(resolve => {
            if (typeof document === 'undefined') {
                resolve({ width: -1, height: -1 });
            }

            let tmpImg = document.createElement('img');
            tmpImg.onload = () => {
                resolve({
                    width: tmpImg.width,
                    height: tmpImg.height
                });
            };

            tmpImg.src = aSrc;
        });
    },

    prepareIconsUrls(icons: Icon[], baseUrl: string) {
        return icons.map(icon => {
            if (!icon.src.includes('http') && !icon.src.includes('data:image')) {
                //remove non-base scope path for sites like mastodon and billthis
                const pathArray = baseUrl.split( '/' );
                const protocol = pathArray[0];
                const host = pathArray[2];
                baseUrl = protocol + '//' + host; 
                //remove posible trailing/leading slashes
                icon.src = `${baseUrl.replace(/\/$/, '')}/${icon.src.replace(/^\/+/g, '')}`;
            }

            return icon;
        });
    },

    async getImageDataURI(file: File): Promise<string> {
        return new Promise<string>(resolve => {
            const reader = new FileReader();

            reader.onload = (aImg: any) => {
                const result: string = aImg.target.result;
                resolve(result);
            };

            reader.readAsDataURL(file);
        });
    },

    hasRelatedApplicationErrors(app: RelatedApplication): string | undefined {
        if (!app.platform) {
            return 'error.enter_platform';
        }

        if (!app.url && !app.id) {
            return 'error.enter_url';
        }

        const urlRegExpr = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.?[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
        if (app.url && !urlRegExpr.test(app.url)) {
            return 'error.enter_valid_url';
        }

        return;
    },

    fixColorFromServer(color: string): string {
        if (!color) {
            return '';
        }

        return '#' + colorConverter.toHexadecimal(color).slice(4, 10);
    },

    sumIssues(errors: CodeError[] | null): number {
        if (!errors) {
            return 0;
        }

        let total = 0;
        errors.forEach(error => {
            if (error.issues && error.issues.length) {
                total += error.issues.length;
            }
        });

        return total;
    }
};
{'fraction_non_alphanumeric': 0.15582450832072617, 'fraction_numerical': 0.006387628172802151, 'mean_word_length': 1.6050788091068302, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '124121', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1694, 'n_words': 340}