@dreamit/funpara
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

funpara

Function parameter library to ease development and testing. The library provides Date and Fetch related functions that can be used without complicated spy/mock setups so that an application can easily be tested with function parameters.

Installation

npm install --save @dreamit/funpara

TypeScript declarations are provided within the project.

Short introduction to functions as parameters

Many functions use primitive types and objects as parameters to control the logic in the function. It is also possible to use functions as parameters. This way the you can decide how to work with the function from the parameter. For example the function can be forwarded to another function or the called at any point in the other function.

/** *
 * This function has a primitive string as parameter. It is difficult to test because of the
 * "new Date()" often changing the value.
 */
function createLogMessage(message: string): string {
    return new Date().toISOString() + ':' + message
}

/** *
 * This function has a primitive string and an optional Date object as parameters.
 * This way the Date parameter does not need to be set in production use but can be set
 * in tests for better testing.
 * However, the Date object will contain the Date when the Date is created. For more complicated
 * use cases with multiple "new Date()" calls in the function or forwarding this is not useful.
 */
function createLogMessageWithDate(
    message: string,
    date: Date = new Date(),
): string {
    return date.toISOString() + ':' + message
}

/**
 * This function uses the funpara library. It has a primitive string and an optional DateFunction function parameter (i.e. a functions that returns a Date)
 * The "nowDateFunction" always returns "new Date()" so every time it is called it will create
 * the current Date.
 * If the DateFunction needs to be forwarded to another function without creating a new Date at
 * the time of the function call or something needs to be done before getting the current date
 * the function can be called at any time with "dateFunction()" to create the current Date.
 */
function createLogMessageWithDateFunction(
    message: string,
    dateFunction: DateFunction = nowDateFunction,
): string {
    // const doSomethingThatTakesLonger = someFunctionCall(dateFunction)
    return dateFunction().toISOString() + ':' + message
}

In tests/index.test.ts there are additional examples how the available code in funpara can be used.

Available functionality

Types

  • DateFunction: Type for a function that returns a Date.
  • FetchFunction: Type for a fetch function that given an input (url, request, etc.) and request init returns a Promise

Core functions

The following core functions can be used to create a new function or create dynamic return values in the function.

  • nowDateFunction: Returns a DateFunction that returns the current date.
  • fixedDateFunction: Returns a DateFunction that returns a fixed date that matches the given date string.
  • fixedResponseFetchFunction: Returns a FetchFunction that always returns a fixed response that matches the given body init and response init values.

Fixed value functions

The following functions return a fix value. This can be helpful for testing code with different scenarios.

  • testDateFunction: DateFunction that returns a fixed test date '1001-01-01T00:00:00.000Z'
  • notFoundFetchFunction: FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).
  • brokenJSONFetchFunction: FetchFunction that returns a fixed Response with broken JSON (missing bracket)
  • unknownContentTypeFetchFunction: FetchFunction that returns a fixed Response with an unknown Content-Type ('application/unknown')
  • timeoutFetchFunction: FetchFunction that returns a fixed Response that throws a Timeout error.

Contact

If you have questions or issues please visit our Issue page and open a new issue if there are no fitting issues for your topic yet.

License

funpara is under MIT-License.

Package Sidebar

Install

npm i @dreamit/funpara

Weekly Downloads

99

Version

1.0.0

License

MIT

Unpacked Size

23.1 kB

Total Files

12

Last publish

Collaborators

  • a-team-dreamit
  • dreamit-mira