@semantic-ui/utils
Advanced tools
Comparing version 0.0.33 to 0.0.34
@@ -11,3 +11,3 @@ { | ||
}, | ||
"version": "0.0.33" | ||
"version": "0.0.34" | ||
} |
@@ -336,2 +336,41 @@ /* | ||
export const debounce = (fn, options) => { | ||
// allow just number to be passed in | ||
if(typeof options == 'number') { | ||
options = { delay: options }; | ||
} | ||
const { | ||
delay = 200, | ||
immediate = false | ||
} = options; | ||
let timeout; | ||
let callbackCalled = false; | ||
function debounced(...args) { | ||
const later = () => { | ||
timeout = null; | ||
if (!immediate) fn.apply(this, args); | ||
callbackCalled = false; | ||
}; | ||
const callNow = immediate && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, delay); | ||
if (callNow && !callbackCalled) { | ||
callbackCalled = true; | ||
fn.apply(this, args); | ||
} | ||
} | ||
debounced.cancel = () => { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
callbackCalled = false; | ||
}; | ||
return debounced; | ||
}; | ||
/*------------------- | ||
@@ -338,0 +377,0 @@ Strings |
@@ -1,2 +0,2 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { describe, beforeEach, afterEach, expect, it, vi } from 'vitest'; | ||
import { tokenize, | ||
@@ -10,2 +10,3 @@ arrayFromObject, | ||
clone, | ||
debounce, | ||
each, | ||
@@ -1276,2 +1277,97 @@ escapeHTML, | ||
}); | ||
describe('debounce', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
it('should delay function execution', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, { delay: 1000 }); | ||
debouncedFunc(); | ||
expect(func).not.toBeCalled(); | ||
vi.advanceTimersByTime(500); | ||
expect(func).not.toBeCalled(); | ||
vi.advanceTimersByTime(500); | ||
expect(func).toBeCalledTimes(1); | ||
}); | ||
it('should only execute once for multiple calls within delay', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, { delay: 1000 }); | ||
debouncedFunc(); | ||
debouncedFunc(); | ||
debouncedFunc(); | ||
vi.advanceTimersByTime(1000); | ||
expect(func).toBeCalledTimes(1); | ||
}); | ||
it('should accept a number as the second argument for delay', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, 500); | ||
debouncedFunc(); | ||
vi.advanceTimersByTime(499); | ||
expect(func).not.toBeCalled(); | ||
vi.advanceTimersByTime(1); | ||
expect(func).toBeCalledTimes(1); | ||
}); | ||
it('should execute immediately if immediate option is true', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, { delay: 1000, immediate: true }); | ||
debouncedFunc(); | ||
expect(func).toBeCalledTimes(1); | ||
debouncedFunc(); | ||
vi.advanceTimersByTime(1000); | ||
expect(func).toBeCalledTimes(1); | ||
}); | ||
it('should allow to cancel delayed execution', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, { delay: 1000 }); | ||
debouncedFunc(); | ||
debouncedFunc.cancel(); | ||
vi.advanceTimersByTime(1000); | ||
expect(func).not.toBeCalled(); | ||
}); | ||
it('should pass correct arguments to the debounced function', () => { | ||
const func = vi.fn(); | ||
const debouncedFunc = debounce(func, { delay: 1000 }); | ||
debouncedFunc('a', 'b'); | ||
vi.advanceTimersByTime(1000); | ||
expect(func).toBeCalledWith('a', 'b'); | ||
}); | ||
it('should maintain correct context', () => { | ||
const obj = { | ||
value: 'test', | ||
method: vi.fn(function() { return this.value; }) | ||
}; | ||
const debouncedMethod = debounce(obj.method, { delay: 1000 }); | ||
debouncedMethod.call(obj); | ||
vi.advanceTimersByTime(1000); | ||
expect(obj.method).toBeCalled(); | ||
expect(obj.method.mock.results[0].value).toBe('test'); | ||
}); | ||
}); | ||
}); | ||
@@ -1278,0 +1374,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95476
2695