Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.

License

NotificationsYou must be signed in to change notification settings

thecodrr/WhatIfJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

total downloads of WhatIfJSWhatIfJS's Licenselatest version of WhatIfJS

WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.

Installation:

Install usingyarn ornpm

npm i --save whatif-jsyarn add whatif-js

Then in theentryPoint of yourapp/modulejustrequire``whatIf:

require("whatif-js");

And that's it, nothing more needed for setup. Everything is handled by the module itself.

Usage

WhatIf

WhatIf is an expression for invoking anaction when the given value is not-null, not-undefined andtrue.

ThewhatIffunction hasthree parameters:

  1. given - the value

  2. action - the action to perform if thegiven value is not-null, not-undefined andtrue.

  3. actionWhatIfNot - (optional) - the action to perform in case thegiven value returnsfalse.

Examples:

The most basic (and useless) example would be:

letobj={name:"Awesome App",type:"App"};obj=obj.whatIf(obj.type==="App",function(){this["description"]="I am an awesome app.";});//output =//{//  name: 'Awesome App',//  type: 'App',//  description: 'I am an awesome app.'//}

Now let's get alittle bit more complex. Usage in abuilder like anArray orStringalso works.

letarray=[0,1,2,3,4,5];array=array.whatIf(array.length===6,ctx=>{returnctx.concat(6,7,8,9);}).slice(2,4).join(",");//output = 2,3,4,5,6,7,8,9

WhatIf can also be used directly withbooleanslike so:

functionworkingWithBoolean(stopWork){stopWork.whatIf(function(){console.log("Stop all the work!");});}

Now coming to thethe more complex example. UsingWhatIf with functions.

There are two ways of usingWhatIf withfunctions, the first is more simple:

functioncalculateSimple(doMultiplication,doDivision){//three methods for three mathematical operationsfunctionadd(...params){returnparams.reduce((p,c)=>p+c);}functionmultiply(...params){returnparams.reduce((p,c)=>p*c);}functiondivide(...params){returnparams.reduce((p,c)=>p/c);}//the values to work withletparamaters=[21,23,41,51,5,1];//the resultletresult=add//perform add if both parameters are false.whatIf(doMultiplication,multiply)//this will only be executed if doDivision=false & doMultiplication=true.whatIf(doDivision,divide)//if doDivision=true, it will always take precedence no matter the value of doMultiplication.call(this, ...paramaters);//the final function callreturnresult;}

We can use the abovefunctioncalculateSimplein the following three ways:

calculateSimple(false,false);//142calculateSimple(false,true);//0.0000873307965816231calculateSimple(true,false);//5049765calculateSimple(true,true);//0.0000873307965816231

More complex example includes performing DMAS (Division, Multiplication, Addition, Subtraction) on astringusing only the power ofWhatIf. See thefull example here

WhatIfNotNull

whatIfNotNull is an expression for invoking anaction 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 likewhatIfNotNullbut it checks whether the object isundefined or not.

WhatIf With Different Return Type

In cases where you want a different return type then the input you can usewhatIf like this:

letstringLength="i am a string".whatIf(getLength,function(){returnthis.length;});

WhatIfNotNullOrEmpty

An expression for invoking anaction when the givenarray is not null and not empty.If thearray is null or empty,actionWhatIfNot will be invoked instead of theaction.

anyArray.whatIfNotNullOrEmpty(  function() {    console.log("array is not null or empty.");  },  function() {    console.log("array is null or empty");  });

Using Arrow Functions

Allfunctions ofwhatIfare usable witharrow functions like so:

anyArray.whatIfNotNullOrEmpty(ctx=>console.log("array is not null or empty."),ctx=>console.log("array is null or empty"));

Find this library useful? ❤️

Support it by joiningstargazers for this repository. ⭐orbuy me a cup of coffeeAndfollow 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.0Unless 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.

About

WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp