Skip to content

Conversation

@briandamaged
Copy link

This change exposes the JSONLogic factory function so that multiple independent instances of JSONLogic can be instantiated at once. For example:

// The module itself still acts like a JSONLogic instance.
const defaultInstance = require('json-logic-js');
defaultInstance.apply({"<": [2, 3]});

// However, now you can create additional instances of
// JSONLogic as well.  Each instance can have its own
// custom operators.
const myInstance = new defaultInstance.JSONLogic();

myInstance.add_operation("join", function(ary, glue) {
  return ary.join(glue);
});

myInstance.apply({"join": [ ["foo", "bar"], ","] });

}
return a;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the changes in this file are just due to indentation. (Specifically: wrapping the existing logic w/ another function)


retval.JSONLogic = JSONLogic;

return retval;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 468 - 472 are the only real "change" to this file. They simply:

  • Create an instance of JSONLogic. (ie: by invoking the factory function)
  • Inject the factory function into this instance. (So that developers can create separate isolated instances of JSONLogic)





Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few tests to ensure that the individual JSONLogic instances weren't accidentally sharing state.

@briandamaged
Copy link
Author

@jwadhams : Howdy! Is there any possibility that you could review/release this update in the next few days?

The change is 100% backwards-compatible with the previous version of the code, and it does not change any of the underlying logic. It simply allows developers to create separate distinct instances of the JSONLogic interpreter. We need this capability on an upcoming project because we intend to use JSONLogic within multiple distinct contexts. We can fork this project if necessary, but we'd prefer to continue using the mainline release.

Thanks in advance!

@briandamaged
Copy link
Author

Actually: I've come up w/ a way to achieve the same thing wo/ changing the indentation. So, I'll create a new PR soon.

@briandamaged
Copy link
Author

Closing this out in favor of #71 . This achieves exactly the same goal, but it eliminates a lot of the re-indentation. (Which should make it a lot easier to avoid merge conflicts in other PRs)

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

Successfully merging this pull request may close these issues.

1 participant