samueldelesque / salad-ui

A UI toolkit.
https://salad-ui.com
MIT License
2 stars 0 forks source link

Charts without moment.js? #2

Open mikehdt opened 6 years ago

mikehdt commented 6 years ago

Is it possible to use the charts without moment.js? It's a very large dependency (~470kb in memory, ~63kb gzipped), and only seems to be used in one place from what I can tell: https://github.com/samueldelesque/salad-ui/blob/master/src/components/chart/area/area.jsx

Would using something like date-fns be a possible alternative? It'd certainly be a heck of a lot smaller (although it does lack timezone support if that's something important).

samueldelesque commented 6 years ago

Hey @mikehdt - that would be great indeed :) If you open a PR I'll gladly merge.

mikehdt commented 6 years ago

Sure, I'd be happy to have a go.

Another possible idea, perhaps a bit more philosophical, is allowing passing in of either moment or date-fns or (some other project), rejecting formatting if none provided but dates passed. This'd be a more agnostic approach, but I wouldn't go doing that without some general guidance on what would be acceptable.

samueldelesque commented 6 years ago

My personal choice would be to code the function directly in JS since it could be done in just a few lines. Looking at date-fns it seems like its the same bundle size as moment, so not really worth it.

To format the date, we could use a function such as:

const formatDate = (date, format) => {
    return (
        format
            .replace('YYYY', date.getFullYear())
            .replace('MM', date.getMonth())
            .replace('DD', date.getDate())
            // Add more supported formats....
    );
}

// usage

formatDate(new Date(), 'YYYY-MM-DD'); // returns 2018-04-23
mikehdt commented 6 years ago

Sure thing, I'd be happy to raise a PR with this. You've provided a great solution starting point, so I hope that's okay to do ;)

samueldelesque commented 6 years ago

Sounds good. You'll also have to update the indexes.