简体   繁体   中英

Conditionally apply string modifier method

Is it possible to conditionally apply a string modifier method such as .toLowerCase() in a more clever method than doing:

const text = 'ThIs Is SoMe TeXt';
const maybeConvert = (text, toLower = false) => {
    return toLower ? text.toLowerCase() : text;
}

You can do something like this if you only want to do something if condition is true and don't wanna do anything if it's false

  const text = 'ThIs Is SoMe TeXt';
    const maybeConvert = (text, condition) => {
     return condition  && text.toLowerCase();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM