Socket
Socket
Sign inDemoInstall

inline-conditional

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    inline-conditional

Create inline conditional statements


Version published
Weekly downloads
2
Maintainers
1
Created
Weekly downloads
 

Readme

Source

npm version

Inline Conditional

Create inline conditional statements!

Table of contents

Installation

To install, run:

$ npm install inline-conditional

Or if you prefer using Yarn:

$ yarn add inline-conditional

Usage

This library allows you to write if/else and switch statements as expressions. This means that you can use them in places where you would normally use a value, most commonly in front-end development scenarios.

Why use this over a ternary expression? I'll show you:

char === "a"
    ? 1
    : char === "b"
    ? 2
    : char === "c"
    ? someCondition
        ? 3
        : 4
    : 0;

// vs

Inline.switch(char)
    .case("a")(1)
    .case("b")(2)
    .case("c")(Inline.if(someCondition)(3).else(4))
    .default(0);

Vanilla JS

import { Inline } from "inline-conditional";

const inlineIfResult = Inline.if(something === somethingElse)(1)
    .elseIf(something.property === expectedPropertyValue)(2)
    .else(3);

const inlineSwitchResult = Inline.switch(flag)
    .case(1)("Hello 1!")
    .case(2)("Hello 2!")
    .default("Who are you?");

React

const Page = () => {
    const isLoggedIn = useSomeAuthenticationHook();

    return (
        <Template>
            {Inline.if(isLoggedIn)(
                // Use functions to prevent components from being called when they're not relevant
                () => <UserPage />
            ).else(() => <AccessDenied />)()}
        </Template>
    );
};

API

TODO

For now, all methods are well-documented using JSDoc.

Authors

License

MIT © Juan de Urtubey

Keywords

FAQs

Last updated on 12 Jan 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc