* [COMPLIANCE] Add Copyright and License Headers * chore: 🤖 update Copyright headers to satisfy our lint rules --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
75 lines
1.4 KiB
JavaScript
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',
|
|
});
|
|
}
|
|
}
|