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

Duration

PreviousTypeScript Null-SafetyNextReducer

Last updated 1 year ago

An easy, expressive and functional way of declaring a duration of time with support for various units (nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days).

// Example 1
durationOf(1000)
    .inWholeSeconds()
    .letIt(it => {
        console.log(`${(1000).milliseconds()} is the same as ${it} seconds`);
    });
// Output: 0d 0h 0m 1s 0ns is the same as 1 seconds
// Example 2
const oneYearInMinutes = (1).years().inWholeMinutes();
console.log(`1 year is ${oneYearInMinutes} minutes.`);
// Output: 1 year is 525960 minutes.
// Example 3
const duration = (1).years().add((6).months()).toString();
console.log(duration); 
// Output: 548d 0h 0m 0s 0ns
// Example 4
({name: "Manuel Santos", email: "ney.br.santos@gmail.com", age: 35})
    .letIt(it => {
        console.log(`${it.name},`);
        it.age < 30 ? console.log("A Young Man") : console.log("An Old Man");
        return it.age;
    })
    .years() // 35 as duration in years
    .runIt((days, hours, minutes, seconds, nanoseconds) => {
        return `Current Age: ${days}d ${hours}h ${minutes}m ${seconds}s ${nanoseconds}ns`;
    });
// Output:
//      Manuel Santos,
//      An Old Man
//      Current Age: 12783d 18h 0m 0s 0ns
🍜
⏰
Duration