@@ -127,8 +127,6 @@ dc.utils.toTimeFunc = function (t) {
127
127
128
128
/**
129
129
* Arbitrary add one value to another.
130
- * @method add
131
- * @memberof dc.utils
132
130
*
133
131
* If the value l is of type Date, adds r units to it. t becomes the unit.
134
132
* For example dc.utils.add(dt, 3, 'week') will add 3 (r = 3) weeks (t= 'week') to dt.
@@ -138,10 +136,14 @@ dc.utils.toTimeFunc = function (t) {
138
136
* dc.utils.add(30, 10) will give 40 and dc.utils.add(30, '10') will give 33.
139
137
*
140
138
* They also generate strange results if l is a string.
139
+ * @method add
140
+ * @memberof dc.utils
141
141
* @param {Date|Number } l the value to modify
142
142
* @param {String|Number } r the amount by which to modify the value
143
- * @param {String } [t] if `l` is a `Date`, then possible values are
144
- * 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', and 'year'
143
+ * @param {Function|String } [t=d3.timeDay] if `l` is a `Date`, then this should be a
144
+ * [d3 time interval](https://github.com/d3/d3-time/blob/master/README.md#_interval).
145
+ * For backward compatibility with dc.js 2.0, it can also be the name of an interval, i.e.
146
+ * 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', or 'year'
145
147
* @returns {Date|Number }
146
148
*/
147
149
dc . utils . add = function ( l , r , t ) {
@@ -156,8 +158,11 @@ dc.utils.add = function (l, r, t) {
156
158
if ( t === 'millis' ) {
157
159
return new Date ( l . getTime ( ) + r ) ;
158
160
}
159
- t = t || 'day' ;
160
- return d3 [ dc . utils . toTimeFunc ( t ) ] . offset ( l , r ) ;
161
+ t = t || d3 . timeDay ;
162
+ if ( typeof t !== 'function' ) {
163
+ t = d3 [ dc . utils . toTimeFunc ( t ) ] ;
164
+ }
165
+ return t . offset ( l , r ) ;
161
166
} else if ( typeof r === 'string' ) {
162
167
var percentage = ( + r / 100 ) ;
163
168
return l > 0 ? l * ( 1 + percentage ) : l * ( 1 - percentage ) ;
@@ -168,8 +173,7 @@ dc.utils.add = function (l, r, t) {
168
173
169
174
/**
170
175
* Arbitrary subtract one value from another.
171
- * @method subtract
172
- * @memberof dc.utils
176
+ *
173
177
* If the value l is of type Date, subtracts r units from it. t becomes the unit.
174
178
* For example dc.utils.subtract(dt, 3, 'week') will subtract 3 (r = 3) weeks (t= 'week') from dt.
175
179
*
@@ -178,10 +182,14 @@ dc.utils.add = function (l, r, t) {
178
182
* dc.utils.subtract(30, 10) will give 20 and dc.utils.subtract(30, '10') will give 27.
179
183
*
180
184
* They also generate strange results if l is a string.
185
+ * @method subtract
186
+ * @memberof dc.utils
181
187
* @param {Date|Number } l the value to modify
182
188
* @param {String|Number } r the amount by which to modify the value
183
- * @param {String } [t] if `l` is a `Date`, then possible values are
184
- * 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', and 'year'
189
+ * @param {Function|String } [t=d3.timeDay] if `l` is a `Date`, then this should be a
190
+ * [d3 time interval](https://github.com/d3/d3-time/blob/master/README.md#_interval).
191
+ * For backward compatibility with dc.js 2.0, it can also be the name of an interval, i.e.
192
+ * 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', or 'year'
185
193
* @returns {Date|Number }
186
194
*/
187
195
dc . utils . subtract = function ( l , r , t ) {
@@ -196,8 +204,11 @@ dc.utils.subtract = function (l, r, t) {
196
204
if ( t === 'millis' ) {
197
205
return new Date ( l . getTime ( ) - r ) ;
198
206
}
199
- t = t || 'day' ;
200
- return d3 [ dc . utils . toTimeFunc ( t ) ] . offset ( l , - r ) ;
207
+ t = t || d3 . timeDay ;
208
+ if ( typeof t !== 'function' ) {
209
+ t = d3 [ dc . utils . toTimeFunc ( t ) ] ;
210
+ }
211
+ return t . offset ( l , - r ) ;
201
212
} else if ( typeof r === 'string' ) {
202
213
var percentage = ( + r / 100 ) ;
203
214
return l < 0 ? l * ( 1 + percentage ) : l * ( 1 - percentage ) ;
0 commit comments