{"version":3,"file":"clamp.js","sources":["../../src/shared/clamp.ts"],"sourcesContent":["/**\n * The `clamp` function restricts a number within a specified range by returning the value itself if it\n * falls within the range, or the closest boundary value if it exceeds the range.\n * @param {number} value - The `value` parameter represents the number that you want to clamp within\n * the specified range defined by `min` and `max` values.\n * @param {number} min - If the `value` parameter is less than the `min` value, the\n * function will return the `min` value.\n * @param {number} max - If the `value` parameter is greater than the `max` value,\n * the function will return `max`.\n * @returns The `clamp` function returns the value of `value` constrained within the range defined by\n * `min` and `max`.\n */\nexport function clamp(value: number, min: number = Number.NEGATIVE_INFINITY, max: number = Number.POSITIVE_INFINITY): number {\n return Math.min(max, Math.max(min, value))\n}\n\n/**\n * The function `roundToStepPrecision` rounds a number to a specified precision step.\n * @param {number} value - The `value` parameter is the number that you want to round to a specific\n * precision based on the `step` parameter.\n * @param {number} step - The `step` parameter in the `roundToStepPrecision` function represents the\n * interval at which you want to round the `value`. For example, if `step` is 0.5, the `value` will be\n * rounded to the nearest half.\n * @returns the `roundedValue` after rounding it to the precision specified by the `step`.\n */\nexport function roundToStepPrecision(value: number, step: number) {\n let roundedValue = value\n const stepString = step.toString()\n const pointIndex = stepString.indexOf('.')\n const precision = pointIndex >= 0 ? stepString.length - pointIndex : 0\n if (precision > 0) {\n const pow = 10 ** precision\n roundedValue = Math.round(roundedValue * pow) / pow\n }\n return roundedValue\n}\n\n/**\n * The function `snapValueToStep` snaps a given value to the nearest step within a specified range.\n * @param {number} value - The `value` parameter represents the number that you want to snap to the\n * nearest step value.\n * @param {number | undefined} min - The `min` parameter represents the minimum value that the `value`\n * should be snapped to. If `value` is less than `min`, it will be snapped to `min`. If `min` is not\n * provided (undefined), then the snapping will not consider a minimum value.\n * @param {number | undefined} max - The `max` parameter represents the maximum value that the `value`\n * should be snapped to. It ensures that the snapped value does not exceed this maximum value.\n * @param {number} step - The `step` parameter in the `snapValueToStep` function represents the\n * interval at which the `value` should be snapped to. It determines the granularity of the snapping\n * operation. For example, if `step` is 5, the `value` will be snapped to the nearest multiple of\n * @returns a number that has been snapped to the nearest step value within the specified range of minimum and maximum values.\n */\nexport function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {\n min = Number(min)\n max = Number(max)\n const remainder = ((value - (Number.isNaN(min) ? 0 : min)) % step)\n let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder, step)\n\n if (!Number.isNaN(min)) {\n if (snappedValue < min)\n snappedValue = min\n else if (!Number.isNaN(max) && snappedValue > max)\n snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step\n }\n else if (!Number.isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step\n }\n\n // correct floating point behavior by rounding to step precision\n snappedValue = roundToStepPrecision(snappedValue, step)\n\n return snappedValue\n}\n"],"names":[],"mappings":"AAYO,SAAS,MAAM,KAAe,EAAA,GAAA,GAAc,OAAO,iBAAmB,EAAA,GAAA,GAAc,OAAO,iBAA2B,EAAA;AAC3H,EAAA,OAAO,KAAK,GAAI,CAAA,GAAA,EAAK,KAAK,GAAI,CAAA,GAAA,EAAK,KAAK,CAAC,CAAA;AAC3C;AAWgB,SAAA,oBAAA,CAAqB,OAAe,IAAc,EAAA;AAChE,EAAA,IAAI,YAAe,GAAA,KAAA;AACnB,EAAM,MAAA,UAAA,GAAa,KAAK,QAAS,EAAA;AACjC,EAAM,MAAA,UAAA,GAAa,UAAW,CAAA,OAAA,CAAQ,GAAG,CAAA;AACzC,EAAA,MAAM,SAAY,GAAA,UAAA,IAAc,CAAI,GAAA,UAAA,CAAW,SAAS,UAAa,GAAA,CAAA;AACrE,EAAA,IAAI,YAAY,CAAG,EAAA;AACjB,IAAA,MAAM,MAAM,EAAM,IAAA,SAAA;AAClB,IAAA,YAAA,GAAe,IAAK,CAAA,KAAA,CAAM,YAAe,GAAA,GAAG,CAAI,GAAA,GAAA;AAAA;AAElD,EAAO,OAAA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