ifdef-loader icon indicating copy to clipboard operation
ifdef-loader copied to clipboard

[Feature Request] treat undefined keys as false

Open lexey111 opened this issue 5 years ago • 2 comments

Hi! Thank you very much for the loader, it is very useful.

It would be very nice to add one minor feature to it - some option in config to treat "absent" (or unset) keys as "false".

lexey111 avatar May 18 '20 07:05 lexey111

but if they are absent, how do you know their names in order to turn them into false?

nippur72 avatar May 18 '20 13:05 nippur72

Like this (dirty patch) -

preprocessor.ts

function evaluate(condition: string, defs: OptionObject): boolean {
   const code = `return (${condition}) ? true : false;`;
   const args = Object.keys(defs);

   let result: boolean;
   try {
     const f = new Function(...args, code);
     result = f(...args.map((k) => defs[k]));
      //console.log(`evaluation of (${condition}) === ${result}`);
   }
   catch(error) {
      // throw `error evaluation #if condition(${condition}): ${error}`;
      return false; // ...if option "treatAbsentAsFalse" is set and error is ReferenceError
   }

   return result;
}

lexey111 avatar May 18 '20 13:05 lexey111