Repository: adobe/react-spectrum
File: packages/@react-stately/utils/src/number.ts (TypeScript)

Code before:
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

/**
 * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
 */
export function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {
  let newValue = Math.min(Math.max(value, min), max);
  return newValue;
}

export function roundToStepPrecision(value: number, step: number): number {
  let roundedValue = value;
  let precision = 0;
  let stepString = step.toString();
  // Handle negative exponents in exponential notation (e.g., "1e-7" → precision 8)
  let eIndex = stepString.toLowerCase().indexOf('e-');
  if (eIndex > 0) {
    let significand = stepString.slice(0, eIndex);
    let exponent = stepString.slice(eIndex + 1);
    let pointIndex = significand.indexOf('.');
    let decimals = pointIndex >= 0 ? significand.length - pointIndex : 0;
    precision = Math.abs(Number(exponent)) + decimals + 1;
  } else {
    let pointIndex = stepString.indexOf('.');
    if (pointIndex >= 0) {
      precision = stepString.length - pointIndex;
    }
  }
  if (precision > 0) {
    let pow = Math.pow(10, precision);
    roundedValue = Math.round(roundedValue * pow) / pow;
  }
  return roundedValue;
}

export function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {
  min = Number(min);
  max = Number(max);
  let remainder = ((value - (isNaN(min) ? 0 : min)) % step);
  let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step
    ? value + Math.sign(remainder) * (step - Math.abs(remainder))
    : value - remainder, step);

  if (!isNaN(min)) {
    if (snappedValue < min) {
      snappedValue = min;
    } else if (!isNaN(max) && snappedValue > max) {
      snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;

Review comment (suggestion):
I think this is a little more succint and explains where the `+ 1` was coming from
```suggestion
  if (eIndex > 0) {
    precision = Math.abs(Math.floor(Math.log10(Math.abs(step)))) + eIndex;
  } else {
```

Code after:
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

/**
 * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
 */
export function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {
  let newValue = Math.min(Math.max(value, min), max);
  return newValue;
}

export function roundToStepPrecision(value: number, step: number): number {
  let roundedValue = value;
  let precision = 0;
  let stepString = step.toString();
  // Handle negative exponents in exponential notation (e.g., "1e-7" → precision 8)
  let eIndex = stepString.toLowerCase().indexOf('e-');
  if (eIndex > 0) {
    precision = Math.abs(Math.floor(Math.log10(Math.abs(step)))) + eIndex;
  } else {
    let pointIndex = stepString.indexOf('.');
    if (pointIndex >= 0) {
      precision = stepString.length - pointIndex;
    }
  }
  if (precision > 0) {
    let pow = Math.pow(10, precision);
    roundedValue = Math.round(roundedValue * pow) / pow;
  }
  return roundedValue;
}

export function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {
  min = Number(min);
  max = Number(max);
  let remainder = ((value - (isNaN(min) ? 0 : min)) % step);
  let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step
    ? value + Math.sign(remainder) * (step - Math.abs(remainder))
    : value - remainder, step);

  if (!isNaN(min)) {
    if (snappedValue < min) {
      snappedValue = min;
    } else if (!isNaN(max) && snappedValue > max) {
      snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;
    }
  } else if (!isNaN(max) && snappedValue > max) {
    snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;
  }