diff --git a/dist/chartjs-adapter-luxon.esm.js b/dist/chartjs-adapter-luxon.esm.js new file mode 100644 index 0000000..19a6be1 --- /dev/null +++ b/dist/chartjs-adapter-luxon.esm.js @@ -0,0 +1,91 @@ +/*! + * chartjs-adapter-luxon v0.2.2 + * https://www.chartjs.org + * (c) 2020 Chart.js Contributors + * Released under the MIT license + */ +import Chart from 'chart.js'; +import { DateTime } from 'luxon'; + +const FORMATS = { + datetime: DateTime.DATETIME_MED_WITH_SECONDS, + millisecond: 'h:mm:ss.SSS a', + second: DateTime.TIME_WITH_SECONDS, + minute: DateTime.TIME_SIMPLE, + hour: { hour: 'numeric' }, + day: { day: 'numeric', month: 'short' }, + week: 'DD', + month: { month: 'short', year: 'numeric' }, + quarter: "'Q'q - yyyy", + year: { year: 'numeric' } +}; + +Chart._adapters._date.override({ + _id: 'luxon', // DEBUG + + /** + * @private + */ + _create: function(time) { + return DateTime.fromMillis(time, this.options); + }, + + formats: function() { + return FORMATS; + }, + + parse: function(value, format) { + const options = this.options; + + if (value === null || typeof value === 'undefined') { + return null; + } + + const type = typeof value; + if (type === 'number') { + value = this._create(value); + } else if (type === 'string') { + if (typeof format === 'string') { + value = DateTime.fromFormat(value, format, options); + } else { + value = DateTime.fromISO(value, options); + } + } else if (type === 'object' && !(value instanceof DateTime)) { + value = DateTime.fromObject(value); + } else if (value instanceof Date) { + value = DateTime.fromJSDate(value, options); + } + + return value.isValid ? value.valueOf() : null; + }, + + format: function(time, format) { + const datetime = this._create(time); + return typeof format === 'string' + ? datetime.toFormat(format, this.options) + : datetime.toLocaleString(format); + }, + + add: function(time, amount, unit) { + const args = {}; + args[unit] = amount; + return this._create(time).plus(args).valueOf(); + }, + + diff: function(max, min, unit) { + return this._create(max).diff(this._create(min)).as(unit).valueOf(); + }, + + startOf: function(time, unit, weekday) { + if (unit === 'isoWeek') { + weekday = Math.trunc(Math.min(Math.max(0, weekday), 6)); + const dateTime = this._create(time); + return dateTime.minus({ days: (dateTime.weekday < weekday ? 7 : 0) + dateTime.weekday - weekday }).startOf('day').valueOf(); + } + return unit ? this._create(time).startOf(unit).valueOf() : time; + }, + + endOf: function(time, unit) { + return this._create(time).endOf(unit).valueOf(); + } +}); diff --git a/dist/chartjs-adapter-luxon.esm.min.js b/dist/chartjs-adapter-luxon.esm.min.js new file mode 100644 index 0000000..b59d943 --- /dev/null +++ b/dist/chartjs-adapter-luxon.esm.min.js @@ -0,0 +1,7 @@ +/*! + * chartjs-adapter-luxon v0.2.2 + * https://www.chartjs.org + * (c) 2020 Chart.js Contributors + * Released under the MIT license + */ +import t from"chart.js";import{DateTime as e}from"luxon";const r={datetime:e.DATETIME_MED_WITH_SECONDS,millisecond:"h:mm:ss.SSS a",second:e.TIME_WITH_SECONDS,minute:e.TIME_SIMPLE,hour:{hour:"numeric"},day:{day:"numeric",month:"short"},week:"DD",month:{month:"short",year:"numeric"},quarter:"'Q'q - yyyy",year:{year:"numeric"}};t._adapters._date.override({_id:"luxon",_create:function(t){return e.fromMillis(t,this.options)},formats:function(){return r},parse:function(t,r){const n=this.options;if(null==t)return null;const o=typeof t;return"number"===o?t=this._create(t):"string"===o?t="string"==typeof r?e.fromFormat(t,r,n):e.fromISO(t,n):"object"!==o||t instanceof e?t instanceof Date&&(t=e.fromJSDate(t,n)):t=e.fromObject(t),t.isValid?t.valueOf():null},format:function(t,e){const r=this._create(t);return"string"==typeof e?r.toFormat(e,this.options):r.toLocaleString(e)},add:function(t,e,r){const n={};return n[r]=e,this._create(t).plus(n).valueOf()},diff:function(t,e,r){return this._create(t).diff(this._create(e)).as(r).valueOf()},startOf:function(t,e,r){if("isoWeek"===e){r=Math.trunc(Math.min(Math.max(0,r),6));const e=this._create(t);return e.minus({days:(e.weekday