Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Accept custom relative thresholds in duration.humanize #4296

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion moment.d.ts
Expand Up @@ -153,10 +153,23 @@ declare namespace moment {
milliseconds: number;
}

interface HumanizeOptions {
withSuffix?: boolean;
thresholds?: {
ss?: number;
s?: number;
m?: number;
h?: number;
d?: number;
M?: number;
y?: number;
};
}

interface Duration {
clone(): Duration;

humanize(withSuffix?: boolean): string;
humanize(withSuffixOrOptions?: boolean | HumanizeOptions): string;

abs(): Duration;

Expand Down
28 changes: 25 additions & 3 deletions src/lib/duration/humanize.js
Expand Up @@ -15,7 +15,7 @@ function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
}

function relativeTime (posNegDuration, withoutSuffix, locale) {
function relativeTime (posNegDuration, withoutSuffix, thresholds, locale) {
var duration = createDuration(posNegDuration).abs();
var seconds = round(duration.as('s'));
var minutes = round(duration.as('m'));
Expand Down Expand Up @@ -69,13 +69,35 @@ export function getSetRelativeTimeThreshold (threshold, limit) {
return true;
}

export function humanize (withSuffix) {
export function humanize (withSuffixOrOptions) {
if (!this.isValid()) {
return this.localeData().invalidDate();
}

var withSuffix = false;
var th = thresholds;

if (typeof withSuffixOrOptions === 'boolean') {
withSuffix = withSuffixOrOptions;
}
else if (typeof withSuffixOrOptions === 'object') {
var ws = withSuffixOrOptions.withSuffix;
if (typeof ws === 'boolean') {
withSuffix = ws;
}

var t = withSuffixOrOptions.thresholds;
if (typeof t === 'object') {
// Fill in missing keys with the current values
th = Object.assign({}, thresholds, t);
if (typeof t.s === 'number') {
th.ss = t.s - 1;
}
}
}

var locale = this.localeData();
var output = relativeTime(this, !withSuffix, locale);
var output = relativeTime(this, !withSuffix, th, locale);

if (withSuffix) {
output = locale.pastFuture(+this, output);
Expand Down
18 changes: 18 additions & 0 deletions src/test/moment/duration.js
Expand Up @@ -464,6 +464,24 @@ test('humanize duration with suffix', function (assert) {
assert.equal(moment.duration({seconds: 44}).humanize(true), 'in a few seconds', '44 seconds = a few seconds');
assert.equal(moment.duration({seconds: -44}).humanize(true), 'a few seconds ago', '44 seconds = a few seconds');
assert.equal(moment.duration({seconds: +44}).humanize(true), 'in a few seconds', '44 seconds = a few seconds');
assert.equal(moment.duration({seconds: 44}).humanize({withSuffix: true}), 'in a few seconds', '44 seconds = a few seconds');
assert.equal(moment.duration({seconds: -44}).humanize({withSuffix: true}), 'a few seconds ago', '44 seconds = a few seconds');
assert.equal(moment.duration({seconds: +44}).humanize({withSuffix: true}), 'in a few seconds', '44 seconds = a few seconds');
});

test('humanize duration with options', function (assert) {
var thresholds = {s: 9};
moment.locale('en');
assert.equal(
moment.duration({seconds: -10}).humanize({thresholds: thresholds}),
'a minute',
'10 seconds = a minute (with thresholds)'
);
assert.equal(
moment.duration({seconds: 10}).humanize({thresholds: thresholds, withSuffix: true}),
'in a minute',
'10 seconds = a minute (with thresholds and suffix)'
);
});

test('bubble value up', function (assert) {
Expand Down