Files
threshold-ui/addons/api/addon/transforms/duration.js
T
Lisbet AlvarezandCarlos Alcaide Corvo 9dd1617b57 chore: 🤖 upgrade ember-data to v5.3.11 (#2745)
* chore: 🤖 upgrade ember-data to v5.3.11

* Add missing copyright headers

* refactor: 💡 remove comment

* chore: 🤖 update yarn.lock due to merge conflict

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* fix: 🐛 undo ember-source upgrade (should be upgraded w/ ember)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* chore: 🤖 move dep to devDeps & resolve test-helper version

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* fix: 🐛 unique record id error bug fix using store.push

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* refactor: 💡 cleanup

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* Delete old deprecation initializer (#2748)

* refactor: 💡 import from'ember-data/store'

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* refactor: 💡 clean up comments

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

* refactor: 💡 remove compatWith comment

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16544

---------

Co-authored-by: Carlos Alcaide Corvo <ccorvo@hashicorp.com>
2025-04-03 09:32:44 -07:00

43 lines
1.2 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
export default class DurationTransform {
/**
* Normalize a duration string in the format "3.0001s" to milliseconds.
* The backend returns a precision of nanoseconds but we round to whole
* milliseconds to avoid any issues with floating point math.
* @param serialized
* @returns {number}
*/
deserialize(serialized) {
if (!serialized) return 0;
const duration = serialized.split('s')[0];
return Math.round(parseFloat(duration) * 1000);
}
/**
* Serialize a duration in ms to a string in seconds.
* @param deserialized
* @returns {string}
*/
serialize(deserialized) {
if (!deserialized) return '0s';
const durationInSeconds = deserialized / 1000;
// We use `toLocaleString` to handle scenarios with very large numbers or
// very small numbers which will be represented as exponents.
// We force the max number of fraction digits to match the precision of the
// backend which is in nanoseconds.
return `${durationInSeconds.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 9,
})}s`;
}
static create() {
return new this();
}
}