All Projects → MeetMartin → conditional-expression

MeetMartin / conditional-expression

Licence: MIT License
JavaScript functional conditional expression

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to conditional-expression

intersection-wasm
Mesh-Mesh and Triangle-Triangle Intersection tests based on the algorithm by Tomas Akenine-Möller
Stars: ✭ 17 (-73.02%)
Mutual labels:  npm-package, npm-module
picsort
Organize your photos by date in one click 👏
Stars: ✭ 22 (-65.08%)
Mutual labels:  npm-package, npm-module
arcscord
A Discord library written in typescript
Stars: ✭ 18 (-71.43%)
Mutual labels:  npm-package, npm-module
Ts ci
✅ Continuous integration setup for TypeScript projects via GitHub Actions.
Stars: ✭ 225 (+257.14%)
Mutual labels:  npm-package, npm-module
React-Pincode
A NPM module which auto fills City, District and State fields when a valid Zip Code in entered!
Stars: ✭ 26 (-58.73%)
Mutual labels:  npm-package, npm-module
Singlespotify
🎵 Create Spotify playlists based on one artist through the command line
Stars: ✭ 254 (+303.17%)
Mutual labels:  npm-package, npm-module
micro-signals
A tiny typed messaging system inspired by js-signals that uses ES2015 sets
Stars: ✭ 39 (-38.1%)
Mutual labels:  npm-package, npm-module
Darkmode.js
🌓 Add a dark-mode / night-mode to your website in a few seconds
Stars: ✭ 2,339 (+3612.7%)
Mutual labels:  npm-package, npm-module
react-multi-context
Manage multiple React 16 contexts with a single component.
Stars: ✭ 19 (-69.84%)
Mutual labels:  npm-package, npm-module
react-folder-tree
A versatile react treeview library that supports custom icons and event handlers
Stars: ✭ 56 (-11.11%)
Mutual labels:  npm-package, npm-module
Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (+250.79%)
Mutual labels:  npm-package, npm-module
react-native-input-prompt
A cross-platform user input prompt component for React Native with Native UI.
Stars: ✭ 45 (-28.57%)
Mutual labels:  npm-package, npm-module
Abort Controller
An implementation of WHATWG AbortController interface.
Stars: ✭ 213 (+238.1%)
Mutual labels:  npm-package, npm-module
js-stack-from-scratch
🌺 Russian translation of "JavaScript Stack from Scratch" from the React-Theming developers https://github.com/sm-react/react-theming
Stars: ✭ 394 (+525.4%)
Mutual labels:  npm-package, npm-module
Jsonexport
{} → 📄 it's easy to convert JSON to CSV
Stars: ✭ 208 (+230.16%)
Mutual labels:  npm-package, npm-module
MinifyAllCli
📦 A lightweight, simple and easy npm tool to 𝗺𝗶𝗻𝗶𝗳𝘆 JSON/C, HTML and CSS! Also known as MinifyAll core! ⭐ Usable as 𝑪𝑳𝑰 tool or 𝒊𝒎𝒑𝒐𝒓𝒕𝒂𝒃𝒍𝒆 in TS/JS as a 𝑴𝑶𝑫𝑼𝑳𝑬 🥰
Stars: ✭ 21 (-66.67%)
Mutual labels:  npm-package, npm-module
Reactopt
A CLI React performance optimization tool that identifies potential unnecessary re-rendering
Stars: ✭ 1,975 (+3034.92%)
Mutual labels:  npm-package, npm-module
Node Regedit
Read, Write, List and do all sorts of funky stuff to the windows registry using node.js and windows script host
Stars: ✭ 178 (+182.54%)
Mutual labels:  npm-package, npm-module
ts-ci
🚀 A starter for TS projects meant to be published on NPM.
Stars: ✭ 282 (+347.62%)
Mutual labels:  npm-package, npm-module
windows-network-drive
Do network drive stuff on Microsoft Window in node
Stars: ✭ 18 (-71.43%)
Mutual labels:  npm-package, npm-module

conditional-expression

npm NpmLicense npm bundle size (minified) npm bundle size (minified + gzip)

Providing 'match' function as a JavaScript functional conditional expression to replace conditional statements 'if' and 'switch' as an alternative to ternary operator expression.

GitHub repository: https://github.com/MartinGentleman/conditional-expression

Medium article explaining motivation and use for the package: How to replace switch and ternaries in functional JavaScript.

Install

npm install conditional-expression --save

Without ES6:

var match = require('conditional-expression').default;

match(1)
  .equals(1).then(function () {
    console.log('hello world');
  }).else(false);

With ES6:

import match from 'conditional-expression';

match(1)
  .equals(1).then(() => console.log('hello world'))
  .else(false);

Usage

First you call match function on an expression that you want to match:

match('functional  programming');

Next you choose how you want to match the expression by choosing appropriate matching function. For example:

match('functional programming')
    .includes('programming');

You follow up by calling 'then' function to tell it what to do in case of a positive match. You can pass a simple value as well as a function that will be automatically evaluated:

match('something')
    .with(/[a-z]/).then('awesome');

// or

match('functional programming')
    .includes('programming').then(() => console.log('awesome'));

You have the option of chaining:

match(42)
    .typeOf('string').then('it is a string')
    .equals('42').then('Answer to the Ultimate Question of Life, the Universe, and Everything');

And you finish everything by calling 'else' which is mandatory:

match('http://domain.tld')
    .on(url => url.substr(0, 5) === 'https').then('It is HTTPS indeed')
    .else('It is not HTTPS and that makes me sad :(');
// returns 'It is not HTTPS and that makes me sad :('

Once a match is found, no other matching is performed:

match('funny joke')
    .equals('sad').then('I am false')
    .with(/[a-z]/).then('I am true and I am the result')
    .includes('joke').then('I am true but I am not evaluated')
    .else('I just execute everything');
// returns 'I am true and I am the result'

Matching functions

on({function})

Evaluates as true if passed function returns boolean true, every other result of a function evaluates as false. Given function is also passed the expression over which we are matching as a parameter.

match({ grumpy: 'cat' })
    .on(x => x.grumpy === 'cat').then(true)
    .else(false);
// returns true

Internally this function is used to implement or other matching functions.

with({RegEx})

Evaluates as true based on passed regular expression.

match(73)
    .with(/[0-9]/).then(true)
    .else(false);
// returns true

equals({*})

Evaluates as true based on strict equality ===.

match('tortoise')
    .equals('tortoise').then(true)
    .else(false);
// returns true

includes({string})

Evaluates as true based on whether a substring is included. Always evaluates as false if not used to match on a string.

match('Martian')
    .includes('arti').then(true)
    .else(false);
// returns true

typeOf({string})

Evaluates as true based a type.

match({})
    .typeOf('object').then(true)
    .else(false);
// returns true

greaterThan({}), lessThan({}), atLeast({}), atMost({})

Evaluates as true based on sizes.

match(2).greaterThan(1).then(true).else(false);
// returns true

match('ab').lessThan('abc').then(true).else(false);
// returns true

match(2).atLeast(2).then(true).else(false);
// returns true

match(2).atLeast(1).then(true).else(false);
// returns true

match(2).atMost(2).then(true).else(false);
// returns true

match(2).atMost(1).then(true).else(false);
// returns true

thenMatch() nested matching

You can use nested matching to create subbranches for evaluation. Only 1 level deep nest is directly supported using thenMatch function.

const param = 'this is string';

match(param)
    .includes('this').thenMatch(param)
        .includes('string').then(true)
        .else(false);
    .else(false);
// returns true

Notice that thenMatch uses its own parameter and that else in the nested branch is still required.

To support deeper branching, you can pass match evaluation as a parameter to then function.

const param = 'this is string';

match(param)
    .includes('this').thenMatch(param)
        .includes('is').then(() => match(param)
            .includes('string').then(true)
            .else(false))
        .else(false);
    .else(false);
// returns true

conditional-expression changelog

1.1.2

  • Fixed README

1.1.1

  • Added greaterThan({}), lessThan({}), atLeast({}), atMost({}) for comparing sizes
  • Fixed Node.js support
  • 100 % test coverage provided.

1.1.0

  • Matching now supports one level of nesting using "thenMatch" function.
  • Added more assertion to test the package and new functionality.
  • Changed the internal structure to promote more internal reusability of the code to add more matching functions in the future if needed.
  • 100 % test coverage provided.

1.0.1

  • "include" function to always returns false if matching over anything else than a string.
  • 100 % test coverage provided.

1.0.0

  • Initial version of conditional-expression providing matching without nesting using.
  • 100 % test coverage provided.

Social

Find me on Twitter: https://twitter.com/PerAsperaEU

Find me on Medium: https://medium.com/@martinnovk_22870

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].