Files
threshold-ui/addons/core/addon/helpers/relative-datetime-live.js
T
Zhihe Li bcf004b8a4 Reformat codebase with trailing commas in functions (#2006)
* style: 💄 Update trailing commas in API

* style: 💄 Update trailing commas in core

* style: 💄 Update trailing commas in rose

* style: 💄 Update trailing commas in admin

* style: 💄 Update trailing commas in desktop
2023-11-22 14:07:41 -05:00

75 lines
1.4 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
const MILLISECOND = 1;
const SECOND = MILLISECOND * 1000;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const MONTH = DAY * 31;
const YEAR = MONTH * 12;
const unitMappings = [
{
unit: 'second',
value: SECOND,
},
{
unit: 'minute',
value: MINUTE,
},
{
unit: 'hour',
value: HOUR,
},
{
unit: 'day',
value: DAY,
},
{
unit: 'month',
value: MONTH,
},
{
unit: 'year',
value: YEAR,
},
];
export default class extends Helper {
// =services
@service clockTick;
@service intl;
// =methods
compute([date]) {
if (!date) return;
const delta = date.valueOf() - this.clockTick.now;
const greatestMeaningfulUnit = unitMappings.reduce(
(currentValue, previousValue) => {
return Math.abs(delta) >= previousValue.value
? previousValue
: currentValue;
},
unitMappings[0],
);
const scaledDelta =
delta < 0
? Math.ceil(delta / greatestMeaningfulUnit.value)
: Math.floor(delta / greatestMeaningfulUnit.value);
const unit = greatestMeaningfulUnit.unit;
return this.intl.formatRelative(scaledDelta, {
unit,
numeric: 'auto',
});
}
}