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

Expressions to customise return value #26

Closed
Ali1 opened this issue Mar 6, 2021 · 5 comments
Closed

Expressions to customise return value #26

Ali1 opened this issue Mar 6, 2021 · 5 comments
Labels
question Further information is requested

Comments

@Ali1
Copy link

Ali1 commented Mar 6, 2021

Is there scope in this project to allow expressions that calculate a return value?

var returnExpression = 'text in response'';
let myReturn = compileReturnExpression(returnExpression);
return myReturn({response: {code: 200, text: 'hi'}}); // returns hi
@cshaa
Copy link
Owner

cshaa commented Mar 7, 2021

Hi!
This is indeed in the scope of this project. In fact it is perfectly doable right now! You just need to change in to of. The in operator checks whether a value is present in an array, meanwhile the of operator accesses properties of objects.
Here's a working example: https://runkit.com/embed/6o7ejjvmtvri

Let me know if this fixed your issue 😁️

@Ali1
Copy link
Author

Ali1 commented Mar 8, 2021

What a neat library. While I have you, what's the best way to compare a part of the data against time with this? customprop a timestamp or date object?? Couldn't figure out what I need to do to this

function supplyTimestamp(){}
let options = {

};
var myfilter = compileExpression('timestamp of data > _timestamp');
myfilter({data : { timestamp: 4543623626 }});

Thanks!!

@cshaa
Copy link
Owner

cshaa commented Mar 8, 2021

I'm not sure I understand your question. Do you want to find a user-friendly way to choose the cutoff date? Something like timestamp of data > date("2020-21-01")? This would be possible using a custom function:

function date(str) { your implementation }

let options = {
   extraFunctions: { date }
};
let myfilter = compileExpression('timestamp of data > date("2020-21-01")', options);
myfilter({data : { timestamp: 4543623626 }});

Or you just want to pass more data to the expression? The simplest way would be to pass the data the intended way:

function supplyTimestamp(){ your implementation }
let myfilter = compileExpression('timestamp of data > _timestamp');

myfilter({
  data : { timestamp: 4543623626 },

  _timestamp: suplyTimestamp(),

  /* or alternatively */
  get _timestamp() { return suplyTimestamp() }
});

If you didn't like this for some reason, custom prop is of course an option:

function customProp(name, get) {
  if (name === '_timestamp') return supplyTimestamp()
  else return get(name)
}

let options = { customProp };
let myfilter = compileExpression('timestamp of data > _timestamp', options);
myfilter({data : { timestamp: 4543623626 }});

@cshaa cshaa closed this as completed Mar 15, 2021
@Ali1
Copy link
Author

Ali1 commented Mar 16, 2021

function customProp(name, get) {
  if (name === '_timestamp') return supplyTimestamp()
  else return get(name)
}

let options = { customProp };
let myfilter = compileExpression('timestamp of data > _timestamp', options);
myfilter({data : { timestamp: 4543623626 }});

Love this solution thanks.

Is there a reason why you return 1 and 0 for conditional expressions rather than a boolean? booleans would be great to better confirm that the expression entered is a conditional type of expression.

@cshaa
Copy link
Owner

cshaa commented Mar 18, 2021

The decission to use only numbers and strings was made at the very beginning of filtrex to make matters as simple as they can possibly be. (Since then arrays and objects also appeared.) However, you are right that from the user's perspective, it doesn't necessarily make sense that 0 gets interpreted as “filter this thing out” and for example -3 gets interpreted as “leave it there”. It would probably be smart to introduce booleans once I get around to make v3 of filtrex (I've opened issue #27 about it). In the meantime, you can make yourself a fork and mess around ;) If you want to turn off automatic coertion of booleans to numbers, the relevant function is coerceBoolean.

@cshaa cshaa added the question Further information is requested label Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants