TotalTechGeek / json-logic-engine

Construct complex rules with JSON & process them.
MIT License
46 stars 9 forks source link

Improve Permissive Implementation; Allows Data Detection Override #29

Closed TotalTechGeek closed 6 months ago

TotalTechGeek commented 6 months ago

Based on the discussion in #28, I decided to improve how the Permissive implementation is handled to allow users to better customize how the data detection is implemented.

I do realize that as I add these capabilities, we might be making it slightly more difficult to maintain a fully compatible cross-language json-logic ecosystem, however, I'm comfortable supporting some of these extensions as long as it is clear to folks that these behaviors might not translate to other libraries by other authors.

Anyway:

I've changed the engine implementation to allow a user to specify how "data" is detected in one's logic.

class DataEngine extends LogicEngine {
  isData (logic, firstKey) {
    if (Object.keys(logic).length > 1) return true
    return !(firstKey in this.methods)
  }
}

You can extend logic engine and implement an isData method to specify if an object embedded in the logic should be treated as data, rather than treating it as a command.

This should give developers full flexibility in how they believe this should work, as there will be different opinions:

Etc.