Skip to content

Commit

Permalink
Replace SHORT/LONG_PRESETS by formats() and presets()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrunel committed Jan 9, 2019
1 parent 524f765 commit 81d71fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
24 changes: 11 additions & 13 deletions src/adapters/adapter.moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var moment = require('moment');
var adapter = require('../core/core.adapters')._date;
var helpers = require('../helpers/helpers.core');

var SHORT_PRESETS = {
var FORMATS = {
millisecond: 'h:mm:ss.SSS a',
second: 'h:mm:ss a',
minute: 'h:mm a',
Expand All @@ -18,23 +18,21 @@ var SHORT_PRESETS = {
year: 'YYYY'
};

var LONG_PRESETS = {
millisecond: 'MMM D, YYYY h:mm:ss.SSS a',
second: 'MMM D, YYYY h:mm:ss a',
minute: 'MMM D, YYYY h:mm a',
hour: 'MMM D, YYYY hA',
day: 'MMM D, YYYY',
week: 'll',
month: 'MMM YYYY',
quarter: '[Q]Q - YYYY',
year: 'YYYY'
var PRESETS = {
full: 'MMM D, YYYY h:mm:ss.SSS a',
time: 'MMM D, YYYY h:mm:ss a',
date: 'MMM D, YYYY'
};

helpers.merge(adapter, moment ? {
_id: 'moment', // DEBUG ONLY

presets: function(long) {
return long ? LONG_PRESETS : SHORT_PRESETS;
formats: function() {
return FORMATS;
},

presets: function() {
return PRESETS;
},

parse: function(value, format) {
Expand Down
14 changes: 12 additions & 2 deletions src/core/core.adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ function abstract() {
* @typedef {('millisecond'|'second'|'minute'|'hour'|'day'|'week'|'month'|'quarter'|'year')}
* @memberof Chart._adapters._date
* @name Unit
* */
*/

/** @lends Chart._adapters._date */
module.exports._date = {
/**
* Returns a map of time formats for the supported units.
* @param {boolean} [long] - query for long formats.
* @returns {{string: string}}
*/
formats: function() {
return {};
},

/**
* Returns a map of date/time formats for the following presets:
* 'full': date + time + millisecond
* 'time': date + time
* 'date': date
* @returns {{string: string}}
*/
presets: function() {
Expand Down
10 changes: 5 additions & 5 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,23 +410,23 @@ function ticksFromTimestamps(values, majorUnit) {
* Return the time format for the label with the most parts (milliseconds, second, etc.)
*/
function determineLabelFormat(timestamps) {
var presets = adapter.presets(true);
var presets = adapter.presets();
var ilen = timestamps.length;
var i, ts, hasTime;

for (i = 0; i < ilen; i++) {
ts = timestamps[i];
if (ts % INTERVALS.second.size !== 0) {
return presets.millisecond;
return presets.full;
}
if (ts % INTERVALS.day.size !== 0) {
hasTime = true;
}
}
if (hasTime) {
return presets.second;
return presets.time;
}
return presets.day;
return presets.date;
}

var defaultConfig = {
Expand Down Expand Up @@ -499,7 +499,7 @@ module.exports = Scale.extend({
// supposed to contain *all* unit/string pairs but this can't be resolved
// when loading the scale (adapters are loaded afterward), so let's populate
// missing formats on update
helpers.mergeIf(time.displayFormats, adapter.presets());
helpers.mergeIf(time.displayFormats, adapter.formats());

return Scale.prototype.update.apply(me, arguments);
},
Expand Down

0 comments on commit 81d71fb

Please sign in to comment.