Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] treat undefined keys as false #36

Open
lexey111 opened this issue May 18, 2020 · 2 comments
Open

[Feature Request] treat undefined keys as false #36

lexey111 opened this issue May 18, 2020 · 2 comments

Comments

@lexey111
Copy link

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".

@nippur72
Copy link
Owner

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

@lexey111
Copy link
Author

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants