@@ -41,6 +41,12 @@ define([
41
41
DAYS = 24 * HOURS ,
42
42
MONTHS = ( 365 / 12 ) * DAYS ;
43
43
44
+ /**
45
+ * @typedef Scale
46
+ * @property {number } min the minimum scale value, in ms
47
+ * @property {number } max the maximum scale value, in ms
48
+ */
49
+
44
50
/**
45
51
* Formatter for UTC timestamps. Interprets numeric values as
46
52
* milliseconds since the start of 1970.
@@ -57,42 +63,43 @@ define([
57
63
* the threshold required.
58
64
* @private
59
65
*/
60
- function getScaledFormat ( d , threshold ) {
61
- //Adapted from D3 formatting rules
62
- if ( ! ( d instanceof Date ) ) {
63
- d = new Date ( moment . utc ( d ) ) ;
64
- }
66
+ function getScaledFormat ( d ) {
67
+ var m = moment . utc ( d ) ;
68
+ /**
69
+ * Uses logic from d3 Time-Scales, v3 of the API. See
70
+ * https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Scales.md
71
+ *
72
+ * Licensed
73
+ */
65
74
return [
66
- [ ".SSS" , function ( d ) { return d . getMilliseconds ( ) >= threshold ; } ] ,
67
- [ ":ss" , function ( d ) { return d . getSeconds ( ) * SECONDS >= threshold ; } ] ,
68
- [ "HH:mm" , function ( d ) { return d . getMinutes ( ) * MINUTES >= threshold ; } ] ,
69
- [ "HH" , function ( d ) { return d . getHours ( ) * HOURS >= threshold ; } ] ,
70
- [ "ddd DD" , function ( d ) {
71
- return d . getDay ( ) * DAYS >= threshold &&
72
- d . getDate ( ) != 1 ;
75
+ [ ".SSS" , function ( m ) { return m . milliseconds ( ) ; } ] ,
76
+ [ ":ss" , function ( m ) { return m . seconds ( ) ; } ] ,
77
+ [ "HH:mm" , function ( m ) { return m . minutes ( ) ; } ] ,
78
+ [ "HH" , function ( m ) { return m . hours ( ) ; } ] ,
79
+ [ "ddd DD" , function ( m ) {
80
+ return m . days ( ) &&
81
+ m . date ( ) != 1 ;
73
82
} ] ,
74
- [ "MMM DD" , function ( d ) { return d . getDate ( ) != 1 ; } ] ,
75
- [ "MMMM" , function ( d ) {
76
- return d . getMonth ( ) * MONTHS >= threshold ;
83
+ [ "MMM DD" , function ( m ) { return m . date ( ) != 1 ; } ] ,
84
+ [ "MMMM" , function ( m ) {
85
+ return m . month ( ) ;
77
86
} ] ,
78
87
[ "YYYY" , function ( ) { return true ; } ]
79
88
] . filter ( function ( row ) {
80
- return row [ 1 ] ( d ) ;
89
+ return row [ 1 ] ( m ) ;
81
90
} ) [ 0 ] [ 0 ] ;
82
91
} ;
83
92
84
93
/**
85
94
*
86
95
* @param value
87
- * @param {number } [threshold] Optionally provides context to the
88
- * format request, allowing for scale-appropriate formatting. This value
89
- * should be the minimum unit to be represented by this format, in ms. For
90
- * example, to display seconds, a threshold of 1 * 1000 should be provided.
96
+ * @param {Scale } [scale] Optionally provides context to the
97
+ * format request, allowing for scale-appropriate formatting.
91
98
* @returns {string } the formatted date
92
99
*/
93
- UTCTimeFormat . prototype . format = function ( value , threshold ) {
94
- if ( threshold !== undefined ) {
95
- var scaledFormat = getScaledFormat ( value , threshold ) ;
100
+ UTCTimeFormat . prototype . format = function ( value , scale ) {
101
+ if ( scale !== undefined ) {
102
+ var scaledFormat = getScaledFormat ( value , scale ) ;
96
103
if ( scaledFormat ) {
97
104
return moment . utc ( value ) . format ( scaledFormat ) ;
98
105
}
0 commit comments