compare-with-utils-a96ff2ea.js 1.4 KB

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