debounce<T>(func: T, wait: number, immediate?: boolean): T
+
+
+
+
+
+
+
Returns a function, that, as long as it continues to be invoked, will not be triggered. It will be called after it stops being called for wait ms.
+ This can be usefull for ResizeObservers for example.
+
+
+
Type parameters
+
+
+
T: (...args: any[]) => unknown
+
+
+
Parameters
+
+
+
func: T
+
+
+
The function you want to debounce
+
+
+
+
+
wait: number
+
+
+
Period to wait in ms
+
+
+
+
+
Default value immediate: boolean = false
+
+
+
Triggering on the leading edge instead of the trailing
+
+
+
+
+
\ No newline at end of file
diff --git a/src/debounce.ts b/src/debounce.ts
new file mode 100644
index 0000000..17b30ae
--- /dev/null
+++ b/src/debounce.ts
@@ -0,0 +1,32 @@
+/**
+ * Returns a function, that, as long as it continues to be invoked, will not be triggered. It will be called after it stops being called for `wait` ms.
+ * This can be usefull for ResizeObservers for example.
+ * @param func The function you want to debounce
+ * @param wait Period to wait in ms
+ * @param immediate Triggering on the leading edge instead of the trailing
+ * @returns Debounced Function
+ */
+// eslint-disable-next-line: ban-types
+export const debounce = unknown>(func: T, wait: number, immediate = false): T => {
+ let timeout;
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ return function (...args) {
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
+ const context = this;
+ const later = () => {
+ timeout = null;
+ if (!immediate) {
+ func.apply(context, args);
+ }
+ };
+ const callNow = immediate && !timeout;
+ clearTimeout(timeout);
+ timeout = setTimeout(later, wait);
+ if (callNow) {
+ func.apply(context, args);
+ }
+ };
+};
diff --git a/src/formatNumber.ts b/src/formatNumber.ts
new file mode 100644
index 0000000..d4ebc20
--- /dev/null
+++ b/src/formatNumber.ts
@@ -0,0 +1,43 @@
+/**
+* Formats a number based on the specified language with thousands separator(s) and decimal character for better legibility.
+* @param num The number to format
+* @param language The language to use when formatting the number
+*/
+export const formatNumber = (num: string | number, language: string, options?: Intl.NumberFormatOptions): string => {
+ // Polyfill for Number.isNaN, which is more reliable than the global isNaN()
+ Number.isNaN =
+ Number.isNaN ||
+ function isNaN(input) {
+ return typeof input === 'number' && isNaN(input);
+ };
+
+ if (!Number.isNaN(Number(num)) && Intl) {
+ return new Intl.NumberFormat(language, getDefaultFormatOptions(num, options)).format(Number(num));
+ }
+ return num.toString();
+};
+
+/**
+* Generates default options for Intl.NumberFormat
+* @param num The number to be formatted
+* @param options The Intl.NumberFormatOptions that should be included in the returned options
+*/
+const getDefaultFormatOptions = (
+ num: string | number,
+ options?: Intl.NumberFormatOptions,
+): Intl.NumberFormatOptions => {
+ const defaultOptions: Intl.NumberFormatOptions = options || {};
+
+ if (typeof num !== 'string') {
+ return defaultOptions;
+ }
+
+ // Keep decimal trailing zeros if they are present in a string numeric value
+ if (!options || (!options.minimumFractionDigits && !options.maximumFractionDigits)) {
+ const digits = num.indexOf('.') > -1 ? num.split('.')[1].length : 0;
+ defaultOptions.minimumFractionDigits = digits;
+ defaultOptions.maximumFractionDigits = digits;
+ }
+
+ return defaultOptions;
+};
Returns a function, that, as long as it continues to be invoked, will not be triggered. It will be called after it stops being called for
+wait
ms. + This can be usefull for ResizeObservers for example.