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

[bugfix] Fix humanize api #5477

Merged
merged 1 commit into from
Apr 30, 2020
Merged
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
30 changes: 12 additions & 18 deletions src/lib/duration/humanize.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,27 @@ export function getSetRelativeTimeThreshold(threshold, limit) {
return true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After your changes I got "export 'duration' (imported as 'moment') was not found in 'moment'"

}

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

var withSuffix = false,
th = thresholds,
ws,
t,
locale,
output;

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

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;
}
if (typeof argWithSuffix === 'object') {
argThresholds = argWithSuffix;
argWithSuffix = false;
}
if (typeof argWithSuffix === 'boolean') {
withSuffix = argWithSuffix;
}
if (typeof argThresholds === 'object') {
th = Object.assign({}, thresholds, argThresholds);
if (argThresholds.s != null && argThresholds.ss == null) {
th.ss = argThresholds.s - 1;
}
}

Expand Down
32 changes: 9 additions & 23 deletions src/test/moment/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,45 +1153,31 @@ test('humanize duration with suffix', function (assert) {
'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) {
test('humanize duration with thresholds', function (assert) {
var thresholds = { s: 9 };
moment.locale('en');
assert.equal(
moment.duration({ seconds: -10 }).humanize({ thresholds: thresholds }),
moment.duration({ seconds: -10 }).humanize(thresholds),
'a minute',
'10 seconds = a minute (with thresholds)'
);
assert.equal(
moment
.duration({ seconds: 10 })
.humanize({ thresholds: thresholds, withSuffix: true }),
moment.duration({ seconds: 10 }).humanize(true, thresholds),
'in a minute',
'10 seconds = a minute (with thresholds and suffix)'
);
assert.equal(
moment
.duration({ weeks: 3 })
.humanize({ thresholds: { d: 7, w: 4 }, withSuffix: true }),
moment.duration({ weeks: 3 }).humanize(true, { d: 7, w: 4 }),
'in 3 weeks',
'in 3 weeks = in 3 weeks (with thresholds and suffix)'
);
assert.equal(
moment.duration({ weeks: 3 }).humanize(false, { d: 7, w: 4 }),
'3 weeks',
'3 weeks = 3 weeks (with thresholds and suffix == false)'
);
});

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