From db4198c238b92c45d5de8b52e049a17b69a88cbb Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Thu, 9 Feb 2023 21:35:49 -0500 Subject: [PATCH] [M] Split clamp function --- src/App.tsx | 4 ++-- src/utils.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 562f588..cb16f44 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -10,7 +10,7 @@ import 'tippy.js/animations/shift-away.css'; import './app.sass'; import { Icon } from '@iconify-icon/solid'; -import { sizeFmt } from './utils'; +import { clamp, sizeFmt } from './utils'; interface File { name: string @@ -51,7 +51,7 @@ export default function App() { function wheel(e: WheelEvent) { let direction = (e.detail < 0 || e.deltaY > 0) ? 1 : -1 - setBcLeft(Math.max(Math.min(bcLeft() + direction * 20, bcMax), 0)) + setBcLeft(clamp(bcLeft() + direction * 20, 0, bcMax)) } // Set initial breadcrumb wheel to show the end path diff --git a/src/utils.ts b/src/utils.ts index 5deab9c..87c9e12 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,4 +7,8 @@ export function sizeFmt(size: number): string { var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); return (size / Math.pow(1024, i)).toFixed(1) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; +} + +export function clamp(num: number, min: number, max: number) { + return Math.max(Math.min(num, max), min) } \ No newline at end of file