compare-with-utils-df1001d7.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. 'use strict';
  5. /**
  6. * Uses the compareWith param to compare two values to determine if they are equal.
  7. *
  8. * @param currentValue The current value of the control.
  9. * @param compareValue The value to compare against.
  10. * @param compareWith The function or property name to use to compare values.
  11. */
  12. const compareOptions = (currentValue, compareValue, compareWith) => {
  13. if (typeof compareWith === 'function') {
  14. return compareWith(currentValue, compareValue);
  15. }
  16. else if (typeof compareWith === 'string') {
  17. return currentValue[compareWith] === compareValue[compareWith];
  18. }
  19. else {
  20. return Array.isArray(compareValue) ? compareValue.includes(currentValue) : currentValue === compareValue;
  21. }
  22. };
  23. /**
  24. * Compares a value against the current value(s) to determine if it is selected.
  25. *
  26. * @param currentValue The current value of the control.
  27. * @param compareValue The value to compare against.
  28. * @param compareWith The function or property name to use to compare values.
  29. */
  30. const isOptionSelected = (currentValue, compareValue, compareWith) => {
  31. if (currentValue === undefined) {
  32. return false;
  33. }
  34. if (Array.isArray(currentValue)) {
  35. return currentValue.some((val) => compareOptions(val, compareValue, compareWith));
  36. }
  37. else {
  38. return compareOptions(currentValue, compareValue, compareWith);
  39. }
  40. };
  41. exports.compareOptions = compareOptions;
  42. exports.isOptionSelected = isOptionSelected;