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 {Predicate} predicateToRemove predicate to remove
  20. * @param {Function} CompoundPredicate CompoundPredicate constructor function
  21. * @param {Function} ComparisonPredicate ComparisonPredicate constructor function
  22. * @return {boolean} true if the predicate to remove is the last ComparisonPredicate
  23. * @memberof rules
  24. */
  25. predicateToRemoveIsTheLastComparisonPredicate: (
  26. root,
  27. CompoundPredicate,
  28. ComparisonPredicate
  29. ) => {
  30. const comparisonPredicateCount = CompoundPredicate.reduce(
  31. root,
  32. (acc, el) => (ComparisonPredicate.is(el) ? acc + 1 : acc),
  33. 0
  34. );
  35. return comparisonPredicateCount === 1;
  36. },
  37. };