UNPKG

@ionic/core

Version:
42 lines (40 loc) 1.48 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ /** * Uses the compareWith param to compare two values to determine if they are equal. * * @param currentValue The current value of the control. * @param compareValue The value to compare against. * @param compareWith The function or property name to use to compare values. */ const compareOptions = (currentValue, compareValue, compareWith) => { if (typeof compareWith === 'function') { return compareWith(currentValue, compareValue); } else if (typeof compareWith === 'string') { return currentValue[compareWith] === compareValue[compareWith]; } else { return Array.isArray(compareValue) ? compareValue.includes(currentValue) : currentValue === compareValue; } }; /** * Compares a value against the current value(s) to determine if it is selected. * * @param currentValue The current value of the control. * @param compareValue The value to compare against. * @param compareWith The function or property name to use to compare values. */ const isOptionSelected = (currentValue, compareValue, compareWith) => { if (currentValue === undefined) { return false; } if (Array.isArray(currentValue)) { return currentValue.some((val) => compareOptions(val, compareValue, compareWith)); } else { return compareOptions(currentValue, compareValue, compareWith); } }; export { compareOptions as c, isOptionSelected as i };