json-logic-js icon indicating copy to clipboard operation
json-logic-js copied to clipboard

Adding typeof support

Open jottinger opened this issue 7 years ago • 2 comments

Adds typeof support for the simple operators.

jottinger avatar Oct 22 '16 14:10 jottinger

This is awesome functionality to have. May I propose some refinements?

A) We keep your proposed syntax as a shortcut, and also if you call it with one argument it returns the type:

jsonLogic.apply({"typeof" : [42, "number"] }) // returns true
jsonLogic.apply({"typeof" : 42}) // returns "number"

B) Because JsonLogic also has parsers in other languages that don't have JavaScript's peculiarities, sometimes we have to/get to make better choices. (e.g., our own truthy table ) So this method can/should call an array an array:

//It's a bad idea to use the unary shortcut with array arguments.
jsonLogic.apply({"typeof" :[ [1,2,3] ]}) // returns "array". 

jsonLogic.apply({"typeof" :[ [1,2,3], "object" ]}) // returns false

On the other hand, I would side with JavaScript (over, say, PHP) and not differentiate between integers and floats:

jsonLogic.apply({"typeof" : 42}) // returns "number" not "integer"
jsonLogic.apply({"typeof" : 3.14159}) // returns "number" not "float" or "double"

Do those make sense?

jwadhams avatar Oct 26 '16 16:10 jwadhams

Yeah, I don't particularly care for the multiple return types myself but it does make a lot of sense. I'll look into incorporating that api ASAP.

On Oct 26, 2016 12:13 PM, "Jeremy Wadhams" [email protected] wrote:

This is awesome functionality to have. May I propose some refinements?

A) We keep your proposed syntax as a shortcut, and also if you call it with one argument it returns the type:

jsonLogic.apply({"typeof" : [42, "number"] }) // returns true jsonLogic.apply({"typeof" : 42}) // returns "number"

B) Because JsonLogic also has parsers in other languages that don't have JavaScript's peculiarities, sometimes we have to/get to make better choices. (e.g., our own truthy table http://jsonlogic.com/truthy.html ) So this method can/should call an array an array:

//It's a bad idea to use the unary shortcut with array arguments. jsonLogic.apply({"typeof" :[ [1,2,3] ]}) // returns "array".

jsonLogic.apply({"typeof" :[ [1,2,3], "object" ]}) // returns false

On the other hand, I would side with JavaScript (over, say, PHP) and not differentiate between integers and floats:

jsonLogic.apply({"typeof" : 42}) // returns "number" not "integer" jsonLogic.apply({"typeof" : 3.14159}) // returns "number" not "float" or "double"

Do those make sense?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jwadhams/json-logic-js/pull/10#issuecomment-256399093, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcmmii-w7iB7tPtIfnTmxYui4UNk1U-ks5q33wOgaJpZM4Kd3iC .

jottinger avatar Oct 26 '16 16:10 jottinger