From 577bf4f15e826fdf2591cdcfb252a21bc5268cbd Mon Sep 17 00:00:00 2001 From: westlakem Date: Wed, 26 Sep 2018 11:05:24 -0400 Subject: [PATCH 1/3] Added operations to the readme --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index cf86a90..db80535 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,25 @@ jsonLogic.apply(false, i_wasnt_even_supposed_to_be_here); // false ``` +### Registering methods +Sometimes you want to perform custom operations on your dataset. This can be done using `add_operation` + +``` +function lessThanNumber(age) { + return age < 65; +} + +function greatherThanNumber(age) { + return age >= 65; +} + +jsonLogic.add_operation('greatherThanNumber', greatherThanNumber); +jsonLogic.add_operation('lessThanNumber', lessThanNumber); + +var rule = JSON.parse('{"greatherThanNumber":[{"var":"age"}]}'); +var data = JSON.parse('{"age": "62"}'); +``` + ## Compatibility This library makes use of `Array.map` and `Array.reduce`, so it's not *exactly* Internet Explorer 8 friendly. From 9424383a35f552fac53d116e664ffe94aac1619a Mon Sep 17 00:00:00 2001 From: Jeremy Wadhams Date: Mon, 10 Jan 2022 15:27:55 -0600 Subject: [PATCH 2/3] Use westlakem's idea for add_operation examples in the Customization section. --- README.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index db80535..7c331c1 100644 --- a/README.md +++ b/README.md @@ -120,10 +120,21 @@ jsonLogic.apply(false, i_wasnt_even_supposed_to_be_here); // false ``` -### Registering methods -Sometimes you want to perform custom operations on your dataset. This can be done using `add_operation` -``` + +## Compatibility + +This library makes use of `Array.map` and `Array.reduce`, so it's not *exactly* Internet Explorer 8 friendly. + +If you want to use JsonLogic *and* support deprecated browsers, you could easily use [BabelJS's polyfill](https://babeljs.io/docs/usage/polyfill/) or directly incorporate the polyfills documented on MDN for [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce). + +## Customization + +It's not possible to include everyone's excellent ideas without the core library bloating, bringing in a ton of outside dependencies, or occasionally causing use case conflicts (some people need to safely execute untrusted rules, some people need to change outside state). + +You can perform custom operations on your dataset using `add_operation` + +```js function lessThanNumber(age) { return age < 65; } @@ -135,18 +146,8 @@ function greatherThanNumber(age) { jsonLogic.add_operation('greatherThanNumber', greatherThanNumber); jsonLogic.add_operation('lessThanNumber', lessThanNumber); -var rule = JSON.parse('{"greatherThanNumber":[{"var":"age"}]}'); -var data = JSON.parse('{"age": "62"}'); +jsonLogic.apply({"greatherThanNumber":[{"var":"age"}]}, {"age":62}); // false +jsonLogic.apply({"lessThanNumber":[{"var":"age"}]}, {"age":62}); // true ``` -## Compatibility - -This library makes use of `Array.map` and `Array.reduce`, so it's not *exactly* Internet Explorer 8 friendly. - -If you want to use JsonLogic *and* support deprecated browsers, you could easily use [BabelJS's polyfill](https://babeljs.io/docs/usage/polyfill/) or directly incorporate the polyfills documented on MDN for [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce). - -## Customization - -It's not possible to include everyone's excellent ideas without the core library bloating, bringing in a ton of outside dependencies, or occasionally causing use case conflicts (some people need to safely execute untrusted rules, some people need to change outside state). - Check out the [documentation for adding custom operations](http://jsonlogic.com/add_operation.html) and be sure to stop by the [Wiki page of custom operations](https://github.com/jwadhams/json-logic-js/wiki/Custom-Operations) to see if someone has already solved your problem or to share your solution. From df6a9920c8135d0762b4875852e0398b9f29c91e Mon Sep 17 00:00:00 2001 From: Jeremy Wadhams Date: Mon, 10 Jan 2022 15:29:40 -0600 Subject: [PATCH 3/3] Oops, whitespace --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7c331c1..0605224 100644 --- a/README.md +++ b/README.md @@ -120,8 +120,6 @@ jsonLogic.apply(false, i_wasnt_even_supposed_to_be_here); // false ``` - - ## Compatibility This library makes use of `Array.map` and `Array.reduce`, so it's not *exactly* Internet Explorer 8 friendly.