Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move into TypeScript #207

Open
Golodhros opened this issue Mar 11, 2021 · 1 comment
Open

Move into TypeScript #207

Golodhros opened this issue Mar 11, 2021 · 1 comment

Comments

@Golodhros
Copy link
Collaborator

Golodhros commented Mar 11, 2021

Expected Behavior

Move library to Typescript

Difference from Current Behavior

Right now is on JS with declaration files

Possible Implementation


// import { bullet as bulletChart } from 'britecharts';
// import bulletChart from 'britecharts/dist/umd/bullet.min';
// import bulletChart from 'britecharts/src/charts/bullet';
import bulletChart, { BulletChartData, BulletChartType } from './bulletD3Chart';

export const britechartsCustomEvents = ['customMouseOver', 'customMouseMove', 'customMouseOut', 'customClick'];

const isEventConfig = (configName: string) => britechartsCustomEvents.includes(configName);
const isNotEventConfig = (configName: string) => !isEventConfig(configName);

const setChartProperty = (
    chart: BulletChartType,
    configuration: Record<keyof BulletChartType, string>,
    key: keyof BulletChartType,
) => {
    if (configuration[key] || typeof configuration[key] === 'string') {
        chart[key](configuration[key]);
    }
};

export const applyConfiguration = (chart: BulletChartType, configuration: Record<keyof BulletChartType, string>) => {
    const configurationProperties = Object.keys(configuration);

    // Regular properties
    configurationProperties.filter(isNotEventConfig).forEach((key) => {
        setChartProperty(chart, configuration, key as keyof BulletChartType);
    });

    return chart;
};

const isNotCustomEvent = (configName: string) => !britechartsCustomEvents.includes(configName);

export const validateConfiguration = (chart: BulletChartType, configuration: Record<keyof BulletChartType, string>) => {
    const configurationProperties = Object.keys(configuration);
    const configurationPropertiesWithoutEvents = configurationProperties.filter(isNotCustomEvent);
    const supportedMethods = Object.keys(chart.prototype.constructor);

    const notSupportedMethods = configurationPropertiesWithoutEvents.filter(
        (methodName) => !supportedMethods.includes(methodName),
    );

    if (notSupportedMethods.length) {
        throw new Error(`Method not supported: ${notSupportedMethods.join(' ')}`);
    }

    return true;
};

type ContainerAnchor = HTMLElement & { __data__?: string };

const bullet = {
    create: (el: ContainerAnchor, data: BulletChartData[], configuration = {}) => {
        const container = select(el);
        const chart = bulletChart();

        if (validateConfiguration(chart, configuration as Record<keyof BulletChartType, string>)) {
            const configuredChart = applyConfiguration(chart, configuration as Record<keyof BulletChartType, string>);

            // Calls the chart with the container and dataset
            container.datum(data[0]).call(configuredChart);

            return chart;
        }
        return false;
    },

    update: (el: ContainerAnchor, data: BulletChartData[], configuration = {}, chart: any) => {
        const container = select(el);

        if (validateConfiguration(chart, configuration as Record<keyof BulletChartType, string>)) {
            applyConfiguration(chart, configuration as Record<keyof BulletChartType, string>);

            // Calls the chart with the container and dataset
            if (data && data.length) {
                container.datum(data[0]).call(chart);
            } else {
                container.call(chart);
            }

            return chart;
        }
        return false;
    },

    
    destroy: (el: ContainerAnchor) => {
        select(el).remove();
    },
};

export default bullet;

See the issue about the hooks for the component.

Context

Typescript seems to be the standard nowadays for libraries and React development.
It is easy for us doing this than maintaining the declaration files.

@Golodhros
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant