rules.js

  1. /**
  2. * Rules
  3. * @namespace rules
  4. * @since 1.0.0
  5. * @note rules are 100% tested from PredicateCore.test.js
  6. */
  7. module.exports = {
  8. /**
  9. * @param {CompoundPredicate} root root predicate
  10. * @param {Predicate} predicateToRemove predicate to remove
  11. * @return {boolean} true if the predicate to remove is the root predicate
  12. * @memberof rules
  13. */
  14. predicateToRemoveIsRootPredicate: (root, predicateToRemove) => {
  15. return root === predicateToRemove;
  16. },
  17. /**
  18. * @param {CompoundPredicate} root root predicate
  19. * @param {Function} CompoundPredicate CompoundPredicate constructor function
  20. * @param {Function} ComparisonPredicate ComparisonPredicate constructor function
  21. * @return {boolean} true if the predicate to remove is the last ComparisonPredicate
  22. * @memberof rules
  23. */
  24. predicateToRemoveIsTheLastComparisonPredicate: (
  25. root,
  26. CompoundPredicate,
  27. ComparisonPredicate
  28. ) => {
  29. const comparisonPredicateCount = CompoundPredicate.reduce(
  30. root,
  31. (acc, el) => (ComparisonPredicate.is(el) ? acc + 1 : acc),
  32. 0
  33. );
  34. return comparisonPredicateCount === 1;
  35. },
  36. };