Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit 2495e18

Browse files
committed
Merge pull request #27 from jbaudanza/master
Add `now` option to format
2 parents 45b892d + 90e0372 commit 2495e18

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎src/core.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function RelativeFormat(locales, options) {
4040
// "Bind" `format()` method to `this` so it can be passed by reference like
4141
// the other `Intl` APIs.
4242
var relativeFormat = this;
43-
this.format = function format(date) {
44-
return relativeFormat._format(date);
43+
this.format = function format(date, options) {
44+
return relativeFormat._format(date, options);
4545
};
4646
}
4747

@@ -138,11 +138,13 @@ RelativeFormat.prototype._compileMessage = function (units) {
138138
return new IntlMessageFormat(message, locales);
139139
};
140140

141-
RelativeFormat.prototype._format = function (date) {
142-
var now = dateNow();
143-
144-
if (date === undefined) {
145-
date = now;
141+
RelativeFormat.prototype._format = function (date, options) {
142+
var now = options && options.now !== undefined ? options.now : dateNow();
143+
if (!isFinite(now)) {
144+
throw new RangeError(
145+
'The now option provided to IntlRelativeFormat#format() is not ' +
146+
'in valid range.'
147+
);
146148
}
147149

148150
// Determine if the `date` is valid, and throw a similar error to what

‎tests/index.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ describe('IntlRelativeFormat', function () {
213213
var output = rt.format(future(1 * 1000));
214214
expect(output).to.equal('dentro de 1 segundo');
215215
});
216+
217+
it('should accept a custom value for now', function() {
218+
var now = 1425839825400;
219+
var output = rt.format(new Date(now-(60 * 1000)), {now: new Date(now)});
220+
expect(output).to.equal('hace 1 minuto');
221+
});
216222
});
217223

218224
describe('and relative time under the English locale', function () {
@@ -322,6 +328,37 @@ describe('IntlRelativeFormat', function () {
322328
var output = rt.format(future(1 * 1000));
323329
expect(output).to.equal('in 1 second');
324330
});
331+
332+
it('should accept a custom value for now', function() {
333+
var now = 1425839825400;
334+
var output = rt.format(new Date(now-(60 * 1000)), {now: new Date(now)});
335+
expect(output).to.equal('1 minute ago');
336+
});
337+
});
338+
339+
describe('the now option', function () {
340+
var rt = new IntlRelativeFormat('en');
341+
342+
it('should accept the epoch value', function () {
343+
var output = rt.format(new Date(60000), {now: 0});
344+
expect(output).to.equal('in 1 minute');
345+
});
346+
347+
it('should use the real value of now when undefined', function () {
348+
var output = rt.format(past(2 * 60 * 1000), {now: undefined});
349+
expect(output).to.equal('2 minutes ago');
350+
});
351+
352+
it('should throw on non-finite values', function() {
353+
expect(function () {
354+
rt.format(past(2 * 60 * 1000), {now: Infinity});
355+
}).to.throwException();
356+
});
357+
358+
it('should treat null like the epoch', function () {
359+
var output = rt.format(new Date(120000), {now: null});
360+
expect(output).to.equal('in 2 minutes');
361+
});
325362
});
326363

327364
// INTERNAL FORMAT DELEGATION

0 commit comments

Comments
 (0)