diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5db2980..3cc99b2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,11 +7,11 @@ These steps will guide you through contributing to this project: - Fork the repo - Clone it and install dependencies - git clone https://github.com/YOUR-USERNAME/typescript-library-starter + git clone https://github.com/YOUR-USERNAME/rulerx npm install Keep in mind that after running `npm install` the git repo is reset. So a good way to cope with this is to have a copy of the folder to push the changes, and the other to try them. Make and commit your changes. Make sure the commands npm run build and npm run test:prod are working. -Finally send a [GitHub Pull Request](https://github.com/alexjoverm/typescript-library-starter/compare?expand=1) with a clear list of what you've done (read more [about pull requests](https://help.github.com/articles/about-pull-requests/)). Make sure all of your commits are atomic (one feature per commit). +Finally send a [GitHub Pull Request](https://github.com/yuxblank/rulerx/compare?expand=1) with a clear list of what you've done (read more [about pull requests](https://help.github.com/articles/about-pull-requests/)). Make sure all of your commits are atomic (one feature per commit). diff --git a/code-of-conduct.md b/code-of-conduct.md index 4a9568b..0135bad 100644 --- a/code-of-conduct.md +++ b/code-of-conduct.md @@ -55,7 +55,7 @@ further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at alexjovermorales@gmail.com. All +reported by contacting the project team at yuxblank@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. diff --git a/src/rule/operators.ts b/src/rule/operators.ts index c341e45..0e20733 100644 --- a/src/rule/operators.ts +++ b/src/rule/operators.ts @@ -62,3 +62,7 @@ export function noneOfList(object: any[], value: any[]): boolean { ).length === object.length ) } + +export function sumGreaterThan(object: any[], value: any[]): boolean{ + return object.reduce((a, b) => a + b, 0) > value; +} diff --git a/src/rule/rule-rx.ts b/src/rule/rule-rx.ts index f51e090..17eb6ca 100644 --- a/src/rule/rule-rx.ts +++ b/src/rule/rule-rx.ts @@ -25,7 +25,7 @@ export class RuleRx implements RuleEvaluator { rules: evaluateRule }) } - }) + 0}) } if (rules.any) { diff --git a/test/rulerx.functional.test.ts b/test/rulerx.functional.test.ts new file mode 100644 index 0000000..f94be2d --- /dev/null +++ b/test/rulerx.functional.test.ts @@ -0,0 +1,98 @@ +import {RuleRx} from '../src/rule/rule-rx' +import {sumGreaterThan} from '../src/rule/operators' +import {BehaviorSubject} from 'rxjs' +import {finalize, mergeAll, tap} from 'rxjs/operators' + +interface Basket { + products: Product[] + customer: {name:string} + discount?: number; +} +interface Product { + name: string; + price: number; +} + +describe('RuleRX functional uses cases', () => { + + it('RuleRX can be used to trigger a discount whenever the user carts reach a certain amount', done => { + + let customerCart1 = new BehaviorSubject({ + products: [{ + name: "Art1", + price: 10 + }], + customer: {name: "Jhon"} + }); + + let customerCart2 = new BehaviorSubject( { + products: [{ + name: "Art3", + price: 50 + }], + customer: {name: "Ada"} + }); + + let carts= [customerCart1,customerCart2]; + + new RuleRx() + .evaluate( + { + all: [ + { + fact: 'user cart sum is beyond 100', + operator: sumGreaterThan, + path: '$.products..price', + value: 100 + } + ] + }, + ...carts + ) + .pipe( + mergeAll(), + tap(p => p.element.discount = 10), + finalize(() => { + done(); + }) + ) + .subscribe(next => { + expect(next.element.customer.name).toEqual("Jhon"); + expect(next.element.discount).toEqual(10); + }); + + customerCart1.next( + { + products: [{ + name: "Art1", + price: 10 + }, + { + name: "Art2", + price: 100 + }], + customer: {name: "Jhon"} + } + ); + + + customerCart2.next( + { + products: [{ + name: "Art3", + price: 50 + }, + { + name: "Art4", + price: 50 + }], + customer: {name: "Ada"} + } + ); + + customerCart1.complete(); + customerCart2.complete(); + + + }) +})