Katxupa
  • 👋Welcome to Katxupa Extension Library
  • Why Katxupa?
    • 🍻What makes Katxupa special?
    • ✨Key Features
  • Get a taste of Katxupa
    • 🍲Installation
    • 🍜Usage
      • 🍻Scope Functions
        • Function Selection
        • Distinctions
        • Functions
          • letIt
          • withIt
          • runIt
          • applyIt
          • alsoIt
          • takeIf and takeUnless
      • 😎Null Safety
        • TypeScript Null-Safety
        • Optional Chaining for JavaScript
      • ⏰Duration
      • ➖Reducer
      • 📏Range
      • 🏃‍♂️Result
      • 🤼Either
      • 🟰Compare
      • ⚔️Global Utility Functions
    • 🤌Dip Dive
      • Optional
      • Range
      • Duration
      • Reducer
      • Collections
        • Array
        • Set
  • 🙏Support
    • Source Code
    • ESLint Config
    • TypeScript Docs
    • Manuel Santos Linkedin
Powered by GitBook
On this page
  1. Get a taste of Katxupa
  2. Usage
  3. Scope Functions
  4. Functions

runIt

runIt(function(){ // do something here with this })

  • The context object is available as a receiver (this).

  • The return value is the lambda result.

runIt does the same as withIt but it is implemented as an extension function. So like letIt, you can call it on the context object using dot notation.

runIt is useful when your lambda function both initializes objects and computes the return value.

const service = new MultiportService("https://api.example.com/data", 80)

const result = service.runIt(function () {
    this.port = 8080;
    const result = this.query(prepareRequest());
    console.debug(`Request sent to port ${this.port}"`);
    return result;
});

// the same code written with letIt() function:
const letResult = service.letIt(it => {
    it.port = 8080;
    const result = it.query(prepareRequest());
    console.debug(`Request sent to port ${it.port}"`);
    return result;
});

You can also invoke runIt as a non-extension function. The non-extension variant of runIt has no context object, but it still returns the lambda result. Non-extension run lets you execute a block of several statements where an expression is required. In code, non-extension runIt can be read as "run the code block and compute the result."

const hexNumberRegex = runIt(() => {
    const digits = "0-9"
    const hexDigits = "A-Fa-f"
    const sign = "+-"

   return  new RegExp(`[${sign}]?[${digits}${hexDigits}]+`, "g");
});

let match;
while ((match = hexNumberRegex.exec("+123 -FFFF !%*& 88 XYZ")) !== null) {
    console.log(match[0]);
}
PreviouswithItNextapplyIt

Last updated 1 year ago

🍜
🍻