GitHub

lopif()

Complex conditional execution function mimicking 'if-else' logic without using 'if' statements.

Core Function
Conditional
Logic
Syntax
lopif(condition, onTrue, onFalse, options)
Parameters

condition
any

The condition to evaluate (truthy/falsy).

onTrue
Function|any

Function to execute or value to return if condition is truthy.

onFalse
Function|any
optional

Function or value if condition is falsy.

options
object
optional

Optional hooks and chaining options.

  • pre - Function to run before main execution
  • post - Function to run after main execution
  • chain - Array of further conditions for chaining
Returns

any
- Result of the matched handler or fallback.

Examples

Basic Usage

// Simple conditional execution
lopif(true, "Success", "Failure"); // "Success"
lopif(false, "Success", "Failure"); // "Failure"

// With functions
lopif(10 > 5, () => "Greater", () => "Less or equal"); // "Greater"

With Hooks

lopif(
  10 > 5, 
  () => "Yes", 
  () => "No", 
  { 
    pre: cond => console.log("Checking:", cond),
    post: res => console.log("Result:", res)
  }
);
// Logs: Checking: true
// Logs: Result: Yes
// Returns: "Yes"

Chained Conditions

const score = 85;

lopif(
  false, // Initial condition
  () => "Initial",
  () => "Default",
  {
    chain: [
      { cond: score >= 90, onTrue: () => "A Grade" },
      { cond: score >= 80, onTrue: () => "B Grade" },
      { cond: score >= 70, onTrue: () => "C Grade" },
      { cond: true, onTrue: () => "F Grade" }
    ]
  }
); // "B Grade"

Complex Logic

const user = { role: "admin", active: true };

lopif(
  user.role === "admin" && user.active,
  () => ({
    access: "full",
    permissions: ["read", "write", "delete"]
  }),
  () => ({
    access: "limited", 
    permissions: ["read"]
  }),
  {
    pre: () => console.log("Checking user permissions..."),
    post: result => console.log("Access granted:", result.access)
  }
);