Skip to content

Commit

Permalink
chore: revert meter clamp function (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra authored Oct 23, 2020
1 parent 3e15246 commit cf52991
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/meter/MeterState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
useSealedState,
} from "reakit-utils/useSealedState";

import { isFunction, valueToPercent, clampValue } from "../utils";
import { getDefaultOptimumValue, calculateStatus } from "./helpers";
import { isFunction, valueToPercent } from "../utils";
import { getDefaultOptimumValue, calculateStatus, clamp } from "./helpers";

type Status = "safe" | "caution" | "danger" | undefined;

Expand Down Expand Up @@ -83,10 +83,10 @@ export const useMeterState = (
const initialOptimum =
sealed.optimum ?? getDefaultOptimumValue(initialLow, initialHigh);

const value = clampValue(initialValue, min, max);
const optimum = clampValue(initialOptimum, min, max);
let low = clampValue(initialLow, min, max);
let high = clampValue(initialHigh, min, max);
const value = clamp(initialValue, min, max);
const optimum = clamp(initialOptimum, min, max);
let low = clamp(initialLow, min, max);
let high = clamp(initialHigh, min, max);

// More inequalities handled
// low ≤ high (if both low and high are specified)
Expand Down
16 changes: 16 additions & 0 deletions src/meter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export function getDefaultOptimumValue(min: number, max: number) {
return max < min ? min : min + (max - min) / 2;
}

/**
* Handle Inequalities with received values
*
* minimum ≤ value ≤ maximum
* minimum ≤ low ≤ maximum (if low is specified)
* minimum ≤ high ≤ maximum (if high is specified)
* minimum ≤ optimum ≤ maximum (if optimum is specified)
*
* @see https://html.spec.whatwg.org/multipage/form-elements.html#the-meter-element:attr-meter-max-3:~:text=following%20inequalities%20must%20hold
*/
export function clamp(value: number, min: number, max: number) {
if (value == null) return 0;

return Math.min(Math.max(value, min), max);
}

type CalculateStatusProps = {
value: number;
optimum: number;
Expand Down

0 comments on commit cf52991

Please sign in to comment.