<commit_before>/*
 * Generate a svg timeline with javascript.
 * Based on https://github.com/jasonreisman/Timeline written in python.
 * Slightly documented: https://github.com/linkviii/Timeline
 *
 * Usage: `new Timeline(tlData, "timelineID").build();`
 *
 * v 2017-1-7 *
 *   (Try to change with new features. Not strict.)
 * 
 * MIT licenced
 */


/**
 *color constant
 */
let Colors: {black: string, gray: string} = {black: '#000000', gray: '#C0C0C0'};


function p(o: any): void {
    console.log(o);
}

/**
 * Interface of controlling json
 * start/end YYYY-MM-DD (currently `new Date(str);`)
 */
//Base interface is useless
type TimelineData = TimelineDataV1 | TimelineDataV2;

//v1
type TimelineCalloutV1 = [string, string]|[string, string, string];
type TimelineEraV1 = [string, string, string]|[string, string, string, string];
interface TimelineDataV1 {
    width: number;
    start: string;
    end: string;
    num_ticks?: number;
    tick_format?: string;
    //[[description, date, ?color],...]
    callouts?: TimelineCalloutV1[];
    //[[name, startDate, endDate, ?color],...]
    eras?: TimelineEraV1[];
}

//v2
interface TimelineCalloutV2 {
    description: string;
    date: string;
    color?: string;
}

interface TimelineEraV2 {
    name: string;
    startDate: string;
    endDate: string;
    color?: string;
}

interface TimelineDataV2 {
    apiVersion: number; //2
    width: number;
    startDate: string;
    endDate: string;
    numTicks?: number;
    tickFormat?: string;
    callouts?: TimelineCalloutV2[];
    eras?: TimelineEraV2[];
}

function convertTimelineDataV1ToV2(oldData: TimelineDataV1): TimelineDataV2 {

    function convertCallouts(oldCallouts: TimelineCalloutV1[]): TimelineCalloutV2[] {
        const callouts: TimelineCalloutV2[] = [];

        for (let oldCallout of oldCallouts) {
            const newCallout: TimelineCalloutV2 = {
                description: oldCallout[0],
                date: oldCallout[1]
            };
            if (oldCallout.length == 3) {
                newCallout.color = oldCallout[2]
            }
            callouts.push(newCallout);
        }
        return callouts;
    }

    function convertEras(oldEras: TimelineEraV1[]): TimelineEraV2[] {
        const eras: TimelineEraV2[] = [];
        for (let oldEra of oldEras) {
            const newEra: TimelineEraV2 = {
                name: oldEra[0],
                startDate: oldEra[1],
                endDate: oldEra[2]
            };
            if (oldEra.length == 4) {
                newEra.color = oldEra[3];
            }
            eras.push(newEra);
        }
        return eras;
    }

    const newData: TimelineDataV2 = {
        apiVersion: 2,
        width: oldData.width,
        startDate: oldData.start,
        endDate: oldData.end
    };

    // camelCase names
    if ('num_ticks' in oldData) {
        newData.numTicks = oldData.num_ticks;
    }
    if ('tick_format' in oldData) {
        newData.tickFormat = oldData.tick_format;
    }

    // Convert tuples to objects
    if ('callouts' in oldData) {
        newData.callouts = convertCallouts(oldData.callouts);
    }
    if ('eras' in oldData) {
        newData.eras = convertEras(oldData.eras);
    }

    return newData;
}

/**
 * addAxisLabel kw
 */
interface LabelKW {
    tick?: boolean;
    stroke?: string;
    fill?: string;
}


//
//
//

class Timeline {

    public data: TimelineDataV2;

    public startDate: Date;
    public endDate: Date;

    public date0: number;
    public date1: number;
    public totalSeconds: number;

    //public callout_size: [number, number, number];
    public calloutProperties: {width: number, height: number, increment: number};


    public textFudge: [number, number];
    public tickFormat: string;
    public markers;

    //public fonts;

    public maxLabelHeight: number;

    public width: number;

    public drawing;
    public axisGroup;

    // initializes data for timeline
    constructor(data: TimelineData, id: string) {

        if ((<TimelineDataV2>data).apiVersion == 2) {
            this.data = <TimelineDataV2>data;
        } else {
            this.data = convertTimelineDataV1ToV2(<TimelineDataV1>data);
        }


        this.width = this.data.width;

        this.drawing = SVG(id);
        this.axisGroup = this.drawing.group();

        this.startDate = new Date(this.data.startDate);
        this.endDate = new Date(this.data.endDate);

        const delta: number = (this.endDate.valueOf() - this.startDate.valueOf());
        const padding: number = (new Date(delta * 0.1)).valueOf();

        this.date0 = this.startDate.valueOf() - padding;<commit_msg>Change fields to readonly because whynot<commit_after>/*
 * Generate a svg timeline with javascript.
 * Based on https://github.com/jasonreisman/Timeline written in python.
 * Slightly documented: https://github.com/linkviii/Timeline
 *
 * Usage: `new Timeline(tlData, "timelineID").build();`
 *
 * v 2017-1-7 *
 *   (Try to change with new features. Not strict.)
 * 
 * MIT licenced
 */


/**
 *color constant
 */
let Colors: {black: string, gray: string} = {black: '#000000', gray: '#C0C0C0'};


function p(o: any): void {
    console.log(o);
}

/**
 * Interface of controlling json
 * start/end YYYY-MM-DD (currently `new Date(str);`)
 */
//Base interface is useless
type TimelineData = TimelineDataV1 | TimelineDataV2;

//v1
type TimelineCalloutV1 = [string, string]|[string, string, string];
type TimelineEraV1 = [string, string, string]|[string, string, string, string];
interface TimelineDataV1 {
    width: number;
    start: string;
    end: string;
    num_ticks?: number;
    tick_format?: string;
    //[[description, date, ?color],...]
    callouts?: TimelineCalloutV1[];
    //[[name, startDate, endDate, ?color],...]
    eras?: TimelineEraV1[];
}

//v2
interface TimelineCalloutV2 {
    description: string;
    date: string;
    color?: string;
}

interface TimelineEraV2 {
    name: string;
    startDate: string;
    endDate: string;
    color?: string;
}

interface TimelineDataV2 {
    apiVersion: number; //2
    width: number;
    startDate: string;
    endDate: string;
    numTicks?: number;
    tickFormat?: string;
    callouts?: TimelineCalloutV2[];
    eras?: TimelineEraV2[];
}

function convertTimelineDataV1ToV2(oldData: TimelineDataV1): TimelineDataV2 {

    function convertCallouts(oldCallouts: TimelineCalloutV1[]): TimelineCalloutV2[] {
        const callouts: TimelineCalloutV2[] = [];

        for (let oldCallout of oldCallouts) {
            const newCallout: TimelineCalloutV2 = {
                description: oldCallout[0],
                date: oldCallout[1]
            };
            if (oldCallout.length == 3) {
                newCallout.color = oldCallout[2]
            }
            callouts.push(newCallout);
        }
        return callouts;
    }

    function convertEras(oldEras: TimelineEraV1[]): TimelineEraV2[] {
        const eras: TimelineEraV2[] = [];
        for (let oldEra of oldEras) {
            const newEra: TimelineEraV2 = {
                name: oldEra[0],
                startDate: oldEra[1],
                endDate: oldEra[2]
            };
            if (oldEra.length == 4) {
                newEra.color = oldEra[3];
            }
            eras.push(newEra);
        }
        return eras;
    }

    const newData: TimelineDataV2 = {
        apiVersion: 2,
        width: oldData.width,
        startDate: oldData.start,
        endDate: oldData.end
    };

    // camelCase names
    if ('num_ticks' in oldData) {
        newData.numTicks = oldData.num_ticks;
    }
    if ('tick_format' in oldData) {
        newData.tickFormat = oldData.tick_format;
    }

    // Convert tuples to objects
    if ('callouts' in oldData) {
        newData.callouts = convertCallouts(oldData.callouts);
    }
    if ('eras' in oldData) {
        newData.eras = convertEras(oldData.eras);
    }

    return newData;
}

/**
 * addAxisLabel kw
 */
interface LabelKW {
    tick?: boolean;
    stroke?: string;
    fill?: string;
}


//
//
//

class Timeline {

    public readonly data: TimelineDataV2;

    public readonly startDate: Date;
    public readonly endDate: Date;

    public readonly date0: number;
    public readonly date1: number;
    public readonly totalSeconds: number;

    //public callout_size: [number, number, number];
    public readonly calloutProperties: {width: number, height: number, increment: number};


    public readonly textFudge: [number, number];
    public readonly tickFormat: string;
    public readonly markers;

    //public fonts;

    public maxLabelHeight: number;

    public readonly width: number;

    public readonly drawing;
    public readonly axisGroup;

    // initializes data for timeline
    constructor(data: TimelineData, id: string) {

        if ((<TimelineDataV2>data).apiVersion == 2) {
            this.data = <TimelineDataV2>data;
        } else {
            this.data = convertTimelineDataV1ToV2(<TimelineDataV1>data);
        }


        this.width = this.data.width;

        this.drawing = SVG(id);
        this.axisGroup = this.drawing.group();

        this.startDate = new Date(this.data.startDate);
        this.endDate = new Date(this.data.endDate);

        const delta: number = (this.endDate.valueOf() - this.startDate.valueOf());
        const padding: number = (new Date(delta * 0.1)).valueOf();

        this.date0 = this.startDate.valueOf() - padding;
{'fraction_non_alphanumeric': 0.12140439932318105, 'fraction_numerical': 0.013747884940778343, 'mean_word_length': 2.483241252302026, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '2173480', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2987, 'n_words': 719}