-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nullish and debounce functions (#287)
# Motivation Copy/move nullish and debounce functions from NNS-dapp to utils. That way we can use these in NNS-dapp but also in ic-js libraries or gix-cmp.
- Loading branch information
1 parent
7f4c415
commit 22d962c
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { debounce } from "./debounce.utils"; | ||
|
||
describe("debounce-utils", () => { | ||
let callback: jest.Mock; | ||
|
||
beforeAll(() => | ||
jest.spyOn(console, "error").mockImplementation(() => undefined) | ||
); | ||
|
||
afterAll(() => jest.resetAllMocks()); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
jest.spyOn(global, "setTimeout"); | ||
callback = jest.fn(); | ||
}); | ||
|
||
afterEach(() => jest.useRealTimers()); | ||
|
||
it("should debounce function with timeout", () => { | ||
const testDebounce = debounce(callback, 100); | ||
|
||
testDebounce(); | ||
testDebounce(); | ||
testDebounce(); | ||
|
||
expect(setTimeout).toHaveBeenCalledTimes(3); | ||
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 100); | ||
expect(callback).not.toBeCalled(); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should debounce one function call", () => { | ||
debounce(callback)(); | ||
|
||
expect(callback).not.toBeCalled(); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(callback).toBeCalled(); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should debounce multiple functions call", () => { | ||
const anotherCallback = jest.fn(); | ||
|
||
const test = debounce(anotherCallback); | ||
test(); | ||
test(); | ||
test(); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(anotherCallback).toBeCalled(); | ||
expect(anotherCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable-next-line @typescript-eslint/ban-types */ | ||
export const debounce = (func: Function, timeout?: number) => { | ||
let timer: NodeJS.Timer | undefined; | ||
|
||
return (...args: unknown[]) => { | ||
const next = () => func(...args); | ||
|
||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
|
||
timer = setTimeout( | ||
next, | ||
timeout !== undefined && timeout > 0 ? timeout : 300 | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { isNullish, nonNullish, notEmptyString } from "./nullish.utils"; | ||
|
||
describe("nullish-utils", () => { | ||
describe("isNullish", () => { | ||
it("should determine nullable", () => { | ||
expect(isNullish(null)).toBeTruthy(); | ||
expect(isNullish(undefined)).toBeTruthy(); | ||
expect(isNullish(0)).toBeFalsy(); | ||
expect(isNullish(1)).toBeFalsy(); | ||
expect(isNullish("")).toBeFalsy(); | ||
expect(isNullish([])).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe("nonNullish", () => { | ||
it("should determine not nullable", () => { | ||
expect(nonNullish(null)).toBeFalsy(); | ||
expect(nonNullish(undefined)).toBeFalsy(); | ||
expect(nonNullish(0)).toBeTruthy(); | ||
expect(nonNullish(1)).toBeTruthy(); | ||
expect(nonNullish("")).toBeTruthy(); | ||
expect(nonNullish([])).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("isNullish", () => { | ||
it("should determine nullable", () => { | ||
expect(isNullish(null)).toBeTruthy(); | ||
expect(isNullish(undefined)).toBeTruthy(); | ||
expect(isNullish(0)).toBeFalsy(); | ||
expect(isNullish(1)).toBeFalsy(); | ||
expect(isNullish("")).toBeFalsy(); | ||
expect(isNullish([])).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe("notEmptyString", () => { | ||
it("should determine not empty", () => { | ||
expect(notEmptyString(null)).toBeFalsy(); | ||
expect(notEmptyString(undefined)).toBeFalsy(); | ||
expect(notEmptyString("")).toBeFalsy(); | ||
expect(notEmptyString("test")).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** Is null or undefined */ | ||
export const isNullish = <T>( | ||
argument: T | undefined | null | ||
): argument is undefined | null => argument === null || argument === undefined; | ||
|
||
/** Not null and not undefined */ | ||
export const nonNullish = <T>( | ||
argument: T | undefined | null | ||
): argument is NonNullable<T> => !isNullish(argument); | ||
|
||
/** Not null and not undefined and not empty */ | ||
export const notEmptyString = (value: string | undefined | null): boolean => | ||
nonNullish(value) && value !== ""; |