Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Controller } from "@hotwired/stimulus";
import {
numberToCompact,
compactToNumber,
} from "../../../../static/js/utilities/compact_number";

export default class extends Controller {
static targets = ["textInput", "range"];
Expand All @@ -18,7 +22,7 @@ export default class extends Controller {
updateText(e) {
if (e.detail >= this.minValue && e.detail <= this.maxValue) {
this.removeErrorState();
this.textInputTarget.value = e.detail;
this.textInputTarget.value = numberToCompact(e.detail);
this.updateDatasetValue();
this.inputUpdated();
} else {
Expand All @@ -27,20 +31,22 @@ export default class extends Controller {
}

textUpdated() {
let value = Number(this.textInputTarget.value);
let value = compactToNumber(this.textInputTarget.value);

if (!value) {
value = this.minValue;
this.textInputTarget.value = value;
this.textInputTarget.value = numberToCompact(this.minValue);
}

if (value > this.maxValue || value < this.minValue) {
this.applyErrorState();
value = value > this.maxValue ? this.maxValue : this.minValue;
value = value < this.minValue ? this.minValue : value;
this.textInputTarget.value = numberToCompact(value);
this.dispatchToRange(value);
} else {
this.removeErrorState();
this.dispatchToRange(value);
this.textInputTarget.value = numberToCompact(value);
this.updateDatasetValue();
this.inputUpdated();
}
Expand Down
28 changes: 28 additions & 0 deletions pgml-dashboard/static/js/utilities/compact_number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

export const numberToCompact = (num) => {
if (num >= 1e12) {
return (num / 1e12).toFixed(1) + 'T'; // Trillion
} else if (num >= 1e9) {
return (num / 1e9).toFixed(1) + 'B'; // Billion
} else if (num >= 1e6) {
return (num / 1e6).toFixed(1) + 'M'; // Million
} else if (num >= 1e3) {
return (num / 1e3).toFixed(1) + 'K'; // Thousand
} else {
return num.toString(); // Less than a thousand
}
};

export const compactToNumber = (compact) => {
const suffixes = { 'K': 1e3, 'M': 1e6, 'B': 1e9, 'T': 1e12 };
const regex = /^(\d+(\.\d+)?)([KMBT])$/;

const match = compact.match(regex);
if (match) {
const number = parseFloat(match[1]);
const suffix = match[3].toUpperCase();
return number * suffixes[suffix];
} else {
return parseFloat(compact); // For numbers without suffixes
}
};