From 7f96bbd6c30621a785987f3c5959c85d39bc4656 Mon Sep 17 00:00:00 2001 From: JonahKr Date: Mon, 18 Jan 2021 23:42:39 +0100 Subject: [PATCH 1/2] Added Debounce and formatNumber --- docs/modules/_debounce_.html | 190 ++++++++++++++++++++++++++ docs/modules/_formatnumber_.html | 222 +++++++++++++++++++++++++++++++ src/debounce.ts | 32 +++++ src/formatNumber.ts | 43 ++++++ 4 files changed, 487 insertions(+) create mode 100644 docs/modules/_debounce_.html create mode 100644 docs/modules/_formatnumber_.html create mode 100644 src/debounce.ts create mode 100644 src/formatNumber.ts diff --git a/docs/modules/_debounce_.html b/docs/modules/_debounce_.html new file mode 100644 index 0000000..eb7647a --- /dev/null +++ b/docs/modules/_debounce_.html @@ -0,0 +1,190 @@ + + + + + + "debounce" | custom-card-helpers + + + + + +
+
+
+
+ +
+
+ Options +
+
+ All +
    +
  • Public
  • +
  • Public/Protected
  • +
  • All
  • +
+
+ + + + + + +
+
+ Menu +
+
+
+
+
+
+ +

Module "debounce"

+
+
+
+
+
+
+
+

Index

+
+
+
+

Functions

+ +
+
+
+
+
+

Functions

+
+ +

Const debounce

+
    +
  • 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

      +
      +
      +
    • +
    +

    Returns T

    +

    Debounced Function

    +
  • +
+
+
+
+ +
+
+ +
+

Generated using TypeDoc

+
+
+ + + + \ No newline at end of file diff --git a/docs/modules/_formatnumber_.html b/docs/modules/_formatnumber_.html new file mode 100644 index 0000000..ae3bcce --- /dev/null +++ b/docs/modules/_formatnumber_.html @@ -0,0 +1,222 @@ + + + + + + "formatNumber" | custom-card-helpers + + + + + +
+
+
+
+ +
+
+ Options +
+
+ All +
    +
  • Public
  • +
  • Public/Protected
  • +
  • All
  • +
+
+ + + + + + +
+
+ Menu +
+
+
+
+
+
+ +

Module "formatNumber"

+
+
+
+
+
+
+
+

Index

+
+
+
+

Functions

+ +
+
+
+
+
+

Functions

+
+ +

Const formatNumber

+
    +
  • formatNumber(num: string | number, language: string, options?: NumberFormatOptions): string
  • +
+
    +
  • + +
    +
    +

    Formats a number based on the specified language with thousands separator(s) and decimal character for better legibility.

    +
    +
    +

    Parameters

    +
      +
    • +
      num: string | number
      +
      +
      +

      The number to format

      +
      +
      +
    • +
    • +
      language: string
      +
      +
      +

      The language to use when formatting the number

      +
      +
      +
    • +
    • +
      Optional options: NumberFormatOptions
      +
    • +
    +

    Returns string

    +
  • +
+
+
+ +

Const getDefaultFormatOptions

+
    +
  • getDefaultFormatOptions(num: string | number, options?: NumberFormatOptions): NumberFormatOptions
  • +
+
    +
  • + +
    +
    +

    Generates default options for Intl.NumberFormat

    +
    +
    +

    Parameters

    +
      +
    • +
      num: string | number
      +
      +
      +

      The number to be formatted

      +
      +
      +
    • +
    • +
      Optional options: NumberFormatOptions
      +
      +
      +

      The Intl.NumberFormatOptions that should be included in the returned options

      +
      +
      +
    • +
    +

    Returns NumberFormatOptions

    +
  • +
+
+
+
+ +
+
+ +
+

Generated using TypeDoc

+
+
+ + + + \ 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; +}; From 8d2ea38c6153eae1e358dc324fc68ee308c3c5f9 Mon Sep 17 00:00:00 2001 From: JonahKr Date: Mon, 18 Jan 2021 23:44:48 +0100 Subject: [PATCH 2/2] Changed Repository Link --- docs/modules/_debounce_.html | 2 +- docs/modules/_formatnumber_.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/_debounce_.html b/docs/modules/_debounce_.html index eb7647a..f5fbecd 100644 --- a/docs/modules/_debounce_.html +++ b/docs/modules/_debounce_.html @@ -91,7 +91,7 @@

Const debounce

  • diff --git a/docs/modules/_formatnumber_.html b/docs/modules/_formatnumber_.html index ae3bcce..829d65e 100644 --- a/docs/modules/_formatnumber_.html +++ b/docs/modules/_formatnumber_.html @@ -92,7 +92,7 @@

    Const formatNumber