index.js
8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = formatDistanceStrict;
var _index = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
var _index2 = _interopRequireDefault(require("../compareAsc/index.js"));
var _index3 = _interopRequireDefault(require("../toDate/index.js"));
var _index4 = _interopRequireDefault(require("../differenceInSeconds/index.js"));
var _index5 = _interopRequireDefault(require("../_lib/cloneObject/index.js"));
var _index6 = _interopRequireDefault(require("../locale/en-US/index.js"));
var _index7 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var MINUTES_IN_DAY = 1440;
var MINUTES_IN_MONTH = 43200;
var MINUTES_IN_YEAR = 525600;
/**
* @name formatDistanceStrict
* @category Common Helpers
* @summary Return the distance between the given dates in words.
*
* @description
* Return the distance between the given dates in words, using strict units.
* This is like `formatDistance`, but does not use helpers like 'almost', 'over',
* 'less than' and the like.
*
* | Distance between dates | Result |
* |------------------------|---------------------|
* | 0 ... 59 secs | [0..59] seconds |
* | 1 ... 59 mins | [1..59] minutes |
* | 1 ... 23 hrs | [1..23] hours |
* | 1 ... 29 days | [1..29] days |
* | 1 ... 11 months | [1..11] months |
* | 1 ... N years | [1..N] years |
*
* ### v2.0.0 breaking changes:
*
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
*
* - The function was renamed from `distanceInWordsStrict` to `formatDistanceStrict`
* to make its name consistent with `format` and `formatRelative`.
*
* - The order of arguments is swapped to make the function
* consistent with `differenceIn...` functions.
*
* ```javascript
* // Before v2.0.0
*
* distanceInWordsStrict(
* new Date(2015, 0, 2),
* new Date(2014, 6, 2)
* ) //=> '6 months'
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(2014, 6, 2),
* new Date(2015, 0, 2)
* ) //=> '6 months'
* ```
*
* - `partialMethod` option is renamed to `roundingMethod`.
*
* ```javascript
* // Before v2.0.0
*
* distanceInWordsStrict(
* new Date(1986, 3, 4, 10, 32, 0),
* new Date(1986, 3, 4, 10, 33, 1),
* { partialMethod: 'ceil' }
* ) //=> '2 minutes'
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(1986, 3, 4, 10, 33, 1),
* new Date(1986, 3, 4, 10, 32, 0),
* { roundingMethod: 'ceil' }
* ) //=> '2 minutes'
* ```
*
* - If `roundingMethod` is not specified, it now defaults to `round` instead of `floor`.
*
* - `unit` option now accepts one of the strings:
* 'second', 'minute', 'hour', 'day', 'month' or 'year' instead of 's', 'm', 'h', 'd', 'M' or 'Y'
*
* ```javascript
* // Before v2.0.0
*
* distanceInWordsStrict(
* new Date(1986, 3, 4, 10, 32, 0),
* new Date(1986, 3, 4, 10, 33, 1),
* { unit: 'm' }
* )
*
* // v2.0.0 onward
*
* formatDistanceStrict(
* new Date(1986, 3, 4, 10, 33, 1),
* new Date(1986, 3, 4, 10, 32, 0),
* { unit: 'minute' }
* )
* ```
*
* @param {Date|Number} date - the date
* @param {Date|Number} baseDate - the date to compare with
* @param {Object} [options] - an object with options.
* @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
* @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
* @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @returns {String} the distance in words
* @throws {TypeError} 2 arguments required
* @throws {RangeError} `date` must not be Invalid Date
* @throws {RangeError} `baseDate` must not be Invalid Date
* @throws {RangeError} `options.roundingMethod` must be 'floor', 'ceil' or 'round'
* @throws {RangeError} `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
* @throws {RangeError} `options.locale` must contain `formatDistance` property
*
* @example
* // What is the distance between 2 July 2014 and 1 January 2015?
* var result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
* //=> '6 months'
*
* @example
* // What is the distance between 1 January 2015 00:00:15
* // and 1 January 2015 00:00:00?
* var result = formatDistanceStrict(
* new Date(2015, 0, 1, 0, 0, 15),
* new Date(2015, 0, 1, 0, 0, 0)
* )
* //=> '15 seconds'
*
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, with a suffix?
* var result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
* addSuffix: true
* })
* //=> '1 year ago'
*
* @example
* // What is the distance from 1 January 2016
* // to 1 January 2015, in minutes?
* var result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
* unit: 'minute'
* })
* //=> '525600 minutes'
*
* @example
* // What is the distance from 1 January 2015
* // to 28 January 2015, in months, rounded up?
* var result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
* unit: 'month',
* roundingMethod: 'ceil'
* })
* //=> '1 month'
*
* @example
* // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
* import { eoLocale } from 'date-fns/locale/eo'
* var result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
* locale: eoLocale
* })
* //=> '1 jaro'
*/
function formatDistanceStrict(dirtyDate, dirtyBaseDate, dirtyOptions) {
(0, _index7.default)(2, arguments);
var options = dirtyOptions || {};
var locale = options.locale || _index6.default;
if (!locale.formatDistance) {
throw new RangeError('locale must contain localize.formatDistance property');
}
var comparison = (0, _index2.default)(dirtyDate, dirtyBaseDate);
if (isNaN(comparison)) {
throw new RangeError('Invalid time value');
}
var localizeOptions = (0, _index5.default)(options);
localizeOptions.addSuffix = Boolean(options.addSuffix);
localizeOptions.comparison = comparison;
var dateLeft;
var dateRight;
if (comparison > 0) {
dateLeft = (0, _index3.default)(dirtyBaseDate);
dateRight = (0, _index3.default)(dirtyDate);
} else {
dateLeft = (0, _index3.default)(dirtyDate);
dateRight = (0, _index3.default)(dirtyBaseDate);
}
var roundingMethod = options.roundingMethod == null ? 'round' : String(options.roundingMethod);
var roundingMethodFn;
if (roundingMethod === 'floor') {
roundingMethodFn = Math.floor;
} else if (roundingMethod === 'ceil') {
roundingMethodFn = Math.ceil;
} else if (roundingMethod === 'round') {
roundingMethodFn = Math.round;
} else {
throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");
}
var seconds = (0, _index4.default)(dateRight, dateLeft);
var offsetInSeconds = ((0, _index.default)(dateRight) - (0, _index.default)(dateLeft)) / 1000;
var minutes = roundingMethodFn((seconds - offsetInSeconds) / 60);
var unit;
if (options.unit == null) {
if (minutes < 1) {
unit = 'second';
} else if (minutes < 60) {
unit = 'minute';
} else if (minutes < MINUTES_IN_DAY) {
unit = 'hour';
} else if (minutes < MINUTES_IN_MONTH) {
unit = 'day';
} else if (minutes < MINUTES_IN_YEAR) {
unit = 'month';
} else {
unit = 'year';
}
} else {
unit = String(options.unit);
} // 0 up to 60 seconds
if (unit === 'second') {
return locale.formatDistance('xSeconds', seconds, localizeOptions); // 1 up to 60 mins
} else if (unit === 'minute') {
return locale.formatDistance('xMinutes', minutes, localizeOptions); // 1 up to 24 hours
} else if (unit === 'hour') {
var hours = roundingMethodFn(minutes / 60);
return locale.formatDistance('xHours', hours, localizeOptions); // 1 up to 30 days
} else if (unit === 'day') {
var days = roundingMethodFn(minutes / MINUTES_IN_DAY);
return locale.formatDistance('xDays', days, localizeOptions); // 1 up to 12 months
} else if (unit === 'month') {
var months = roundingMethodFn(minutes / MINUTES_IN_MONTH);
return locale.formatDistance('xMonths', months, localizeOptions); // 1 year up to max Date
} else if (unit === 'year') {
var years = roundingMethodFn(minutes / MINUTES_IN_YEAR);
return locale.formatDistance('xYears', years, localizeOptions);
}
throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'");
}
module.exports = exports.default;