Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @rws-air/utils

logo

Rijkswaterstaat Utils

Utils AIR projects


Project Status

GitHub

Bundle Sizes

npm bundle size npm bundle size

Versions

npm

Our Badges

website slack trello Pivotal Tracked Sketch Designed Zeplin Based

Installation

yarn add @rws-air/utils

API Documentation

Check out the docs on github pages

Index

Type aliases

KeyedObject

KeyedObject: Record<PropertyKey, unknown>

TableOrder

TableOrder: "desc" | "asc"

Variables

Const DAY

DAY: number = HOUR * 24

Const DAYS

DAYS: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

Const DAYS_NL

DAYS_NL: string[] = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag']

Const HOUR

HOUR: number = MINUTE * 60

Const MINUTE

MINUTE: number = SECOND * 60

Const MONTHS

MONTHS: string[] = ['January','February','March','April','May','June','July','August','September','October','November','December']

Const MONTHS_NL

MONTHS_NL: string[] = ['Januari','Februari','Maart','April','Mei','Juni','July','Augustus','September','October','November','December']

Const SECOND

SECOND: 1000 = 1000

Const isCIEnv

isCIEnv: boolean = Boolean(process.env.CI)

Validates whether the current environment is a CI environment

Const isDevEnv

isDevEnv: boolean = process.env.NODE_ENV === 'development' ? true : process.env.NODE_ENV === 'test'

Validates whether the current environment is either development or test

Const isTestEnv

isTestEnv: boolean = process.env.NODE_ENV === 'test'

Validates whether the current environment is test

Const objectHasProperty

objectHasProperty: (Anonymous function) = deprecate((...args: Parameters<typeof objHasProp>) => objHasProp(args[0], args[1]),'objectHasProperty is deprecated, use Reflect API with "Reflect.has(obj, \'prop\')" instead.')

Type safely checks if an object includes a given property without using the prototype

param

Object to analyze

param

Property to check for

deprecated

use Reflect API instead

example

Reflect.has(obj, prop)

example

objectHasProperty(obj, prop)

Functions

capitilizeFirstLetter

  • capitilizeFirstLetter(str: string): string

Const createEvent

  • createEvent<T>(value: T, additionalData?: KeyedObject): { target: object } & {}
  • Creates a dummy event

    remark

    additionalData is merged using mergeObject so deep merging is possible

    Type parameters

    • T

    Parameters

    • value: T

      Value to put on the target, used for click/change events

    • Default value additionalData: KeyedObject = {}

      Any additional data to put into the event

    Returns { target: object } & {}

createWrappableText

  • createWrappableText(text: string, maxLength?: number): string
  • Inserts a space in any text after every maxLength amount of characters, allowing Material-UI to gracefully wrap it

    Parameters

    • text: string

      text to split

    • Default value maxLength: number = 75

      The maximum length of a text section @default 75

    Returns string

cutText

  • cutText(str: string, length: number): string
  • Split a text by its latest space character in a range from the character 0 to the selected one.

    Parameters

    • str: string

      The text to split.

    • length: number

      The length of the desired string.

    Returns string

groupBy

  • groupBy<I, K, V>(input: Array<I>, keyExtractor: (_: I) => K, valueExtractor: (_: I) => V): Stack<K, V[]>
  • Group the elements of the input array to a Stack using the keyExtractor and valueExtractor the to obtain the key and value for each element.

    see

    Stack

    example
     type HumanGenders = 'male' | 'female' | 'unknown';
    
     interface Human {
       id: number;
       name: string;
       age: number;
       gender: HumanGenders;
     }
    
     const humans: Human[] = [
       { id: 1, name: 'John Connor', age: 9001, gender: 'male' },
       { id: 2, name: 'Sarah Connor', age: 300, gender: 'female' },
       { id: 3, name: 'Luke Skywalker', age: 30, gender: 'male' }
     ];
    
     const humansGroupedByGender = groupBy<Human, HumanGenders, Human>(
       humans,
       (h) => h.gender,
       (h) => h
     ); // Stack<HumanGenders, Human[]>
    
     //  Stack(2) [Map] {
     //   'male' => [
     //     { id: 1, name: 'John Connor', age: 9001, gender: 'male' },
     //     { id: 3, name: 'Luke Skywalker', age: 30, gender: 'male' }
     //   ],
     //   'female' => [ { id: 2, name: 'Sarah Connor', age: 300, gender: 'female' } ]
     // }

    Type parameters

    • I

    • K

    • V

    Parameters

    • input: Array<I>

      The array of data to group into a Stack

    • keyExtractor: (_: I) => K

      A function that describes where to find the key to use for the Stack

        • (_: I): K
        • Parameters

          • _: I

          Returns K

    • valueExtractor: (_: I) => V

      A function that describes where to find the value to use for the Stack

        • (_: I): V
        • Parameters

          • _: I

          Returns V

    Returns Stack<K, V[]>

    A Stack<Key, Value[]> of the grouped values

isObject

  • isObject(input: unknown): input is Record<PropertyKey, unknown>
  • Verify if the input is an object literal (or class).

    Parameters

    • input: unknown

      The object to verify

    Returns input is Record<PropertyKey, unknown>

mergeObjects

  • mergeObjects<A, B>(objTarget: A, objSource: B): A & B

sentenceCase

  • sentenceCase(text: string): string
  • Transforms given text to sentence case (First letter uppercase, rest lowercase)

    Parameters

    • text: string

      Text to transform

    Returns string

snakeToCamelCase

  • snakeToCamelCase(str: string): string

splitText

  • splitText(str: string, length: number, char?: string): string
  • Split a string by its latest space character in a range from the character 0 to the selected one.

    Parameters

    • str: string

      The text to split.

    • length: number

      The length of the desired string.

    • Default value char: string = " "

      The character to split with

    Returns string

stableTableSort

  • stableTableSort<T>(array: T[], orderBy: keyof T, order: TableOrder, headerMapping: Map<keyof T, string>): T[]
  • Stabily sorts an array of objects using lodash.orderby

    example
    interface ArrType {
      key: string;
      prop: string;
    }
    
    const arr: ArrType[] = [
      { key: '1', prop: '2' },
      { key: '4', prop: '4' }
    ];
    
    const mappings: Map<keyof ArrType, string> = new Map(
      [
        [ 'key', ((type: ArrType) => type.key.toString()) as unknown as string ],
        [ 'prop', ((type: ArrType) => type.prop) as unknown as string ]
      ]
    );
    
    stableTableSort(arr, 'key', 'asc', mappings);

    Type parameters

    • T

    Parameters

    • array: T[]

      Array of objects to order

    • orderBy: keyof T

      Key to order by

    • order: TableOrder

      Order ascending or descending

    • headerMapping: Map<keyof T, string>

      Mapping of table headers for sorting on nested properties

    Returns T[]

toBeCalled

  • toBeCalled<A, B>(functionToBeCalled: Mock<A, B> | SpyInstance<A, B>, calledTimes: number, ...args: any[]): void
  • Checks if a Jest mocked function is called a certain amount of times with a certain amount of arguments

    Type parameters

    • A

    • B: any[]

    Parameters

    • functionToBeCalled: Mock<A, B> | SpyInstance<A, B>

      The function that has to be called

    • calledTimes: number

      The amount of times the function should be called

    • Rest ...args: any[]

      Any arguments the function should be called with

    Returns void

toStack

  • toStack<I, K, V>(input: Array<I>, keyExtractor: (_: I) => K, valueExtractor: (_: I) => V): Stack<K, V>
  • Create a Stack using the keyExtractor and valueExtractor the to obtain the key and value for each element.

    remark

    If multiple elements have the same key only the last element with that key will remain.

    example
      type HumanGenders = 'male' | 'female' | 'unknown';
    
      interface Human {
        id: number;
        name: string;
        age: number;
        gender: HumanGenders;
      }
    
      const humans: Human[] = [
        { id: 1, name: 'John Connor', age: 9001, gender: 'male' },
        { id: 2, name: 'Sarah Connor', age: 300, gender: 'female' },
        { id: 3, name: 'Luke Skywalker', age: 30, gender: 'male' }
      ];
    
      const humansInAStack = toStack<Human, HumanGenders, Human>(
        humans,
        (h) => h.gender,
        (h) => h
      ); // Stack<HumanGenders, Human>
    
    // Stack(2) [Map] {
    //   'male' => { id: 3, name: 'Luke Skywalker', age: 30, gender: 'male' },
    //   'female' => { id: 2, name: 'Sarah Connor', age: 300, gender: 'female' }
    // }

    Type parameters

    • I

    • K

    • V

    Parameters

    • input: Array<I>

      The array of data to transform into a Stack

    • keyExtractor: (_: I) => K

      A function that describes where to find the key to use for the Stack

        • (_: I): K
        • Parameters

          • _: I

          Returns K

    • valueExtractor: (_: I) => V

      A function that describes where to find the value to use for the Stack

        • (_: I): V
        • Parameters

          • _: I

          Returns V

    Returns Stack<K, V>

    A Stack<Key, Value> of the values, mapped by the given key

useWindowSize

  • useWindowSize(): [number, number]
  • React hook to get the current window size Triggers on window resize.

    Use with use-debounce to ensure it is not triggered instantly!!

    example
    import { useDebounce } from 'use-debounce';
    
    const [ width, height ] = useDebounce(useWindowSize(), 1000);
    
    useEffect(() => {
      // Do something here
    }, [ width, height ]);

    Returns [number, number]

Generated using TypeDoc