WhatIfJS
WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.
Installation:
Install using yarn
or npm
npm i --save whatif-jsyarn add whatif-js
Then in the entryPoint
of your app
/module
just require``whatIf
:
;
And that's it, nothing more needed for setup. Everything is handled by the module itself.
Usage
WhatIf
WhatIf
is an expression for invoking an action
when the given value is not-null
, not-undefined
and true
.
The whatIf
function has three parameters:
-
given
- the value -
action
- the action to perform if thegiven
value is not-null
, not-undefined
andtrue
. -
actionWhatIfNot
- (optional) - the action to perform in case thegiven
value returnsfalse
.
Examples:
The most basic (and useless) example would be:
let obj = name: "Awesome App" type: "App" ;obj = obj;//output =//{// name: 'Awesome App',// type: 'App',// description: 'I am an awesome app.'//}
Now let's get a little bit more complex. Usage in a builder
like an Array
or String
also works.
let array = 0 1 2 3 4 5;array = array ;//output = 2,3,4,5,6,7,8,9
WhatIf
can also be used directly with booleans
like so:
{ stopWork;}
Now coming to the the more complex example. Using WhatIf
with functions.
There are two ways of using WhatIf
with functions
, the first is more simple:
{ //three methods for three mathematical operations { return params; } { return params; } { return params; } //the values to work with let paramaters = 21 23 41 51 5 1; //the result let result = add //perform add if both parameters are false //this will only be executed if doDivision=false & doMultiplication=true //if doDivision=true, it will always take precedence no matter the value of doMultiplication ; //the final function call return result;}
We can use the above function
calculateSimple
in the following three ways:
; //142; //0.0000873307965816231; //5049765; //0.0000873307965816231
More complex example includes performing DMAS (Division, Multiplication, Addition, Subtraction) on a string
using only the power ofWhatIf
. See the full example here
WhatIfNotNull
whatIfNotNull
is an expression for invoking an action
when the target object is not null.
let person = getPersonByUsername("John Doe");person.whatIfNotNull( function() { console.log("person is not null."); }, function() { console.log("person is null"); });
WhatIfNotUndefined
whatIfNotUndefined
is exactly like whatIfNotNull
but it checks whether the object is undefined
or not.
WhatIf With Different Return Type
In cases where you want a different return type then the input you can use whatIf
like this:
let stringLength = "i am a string";
WhatIfNotNullOrEmpty
An expression for invoking an action
when the given array
is not null and not empty.
If the array
is null or empty, actionWhatIfNot
will be invoked instead of the action
.
anyArray.whatIfNotNullOrEmpty( function() { console.log("array is not null or empty."); }, function() { console.log("array is null or empty"); });
Using Arrow Functions
All function
s of whatIf
are usable with arrow functions
like so:
anyArray;
Find this library useful? ❤️
Support it by joining stargazers for this repository. ⭐️or buy me a cup of coffee And follow me for my next creations! 🤩
License
Copyright 2019 thecodrr (Abdullah Atta) Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.