evaluateFilter checks if a cell value matches the filter query based on the operator.
(cellValue string, options FilterOptions, colType int)
| 849 | |
| 850 | // evaluateFilter checks if a cell value matches the filter query based on the operator. |
| 851 | func evaluateFilter(cellValue string, options FilterOptions, colType int) bool { |
| 852 | query := options.Query |
| 853 | operator := options.Operator |
| 854 | |
| 855 | // Handle numeric comparisons first |
| 856 | if colType == colTypeFloat || colType == colTypeDate { |
| 857 | isNumericOperator := false |
| 858 | switch operator { |
| 859 | case ">", "<", ">=", "<=": |
| 860 | isNumericOperator = true |
| 861 | } |
| 862 | |
| 863 | if isNumericOperator { |
| 864 | cellVal := parseNumericValueFast(cellValue) |
| 865 | thresholdVal, err := strconv.ParseFloat(strings.TrimSpace(query), 64) |
| 866 | if err != nil { |
| 867 | return false // Cannot compare if query is not a number |
| 868 | } |
| 869 | |
| 870 | switch operator { |
| 871 | case ">": |
| 872 | return cellVal > thresholdVal |
| 873 | case "<": |
| 874 | return cellVal < thresholdVal |
| 875 | case ">=": |
| 876 | return cellVal >= thresholdVal |
| 877 | case "<=": |
| 878 | return cellVal <= thresholdVal |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // Prepare strings for comparison |
| 884 | cell := cellValue |
| 885 | q := query |
| 886 | if !options.CaseSensitive { |
| 887 | cell = strings.ToLower(cell) |
| 888 | q = strings.ToLower(q) |
| 889 | } |
| 890 | |
| 891 | // Handle string-based operators |
| 892 | switch operator { |
| 893 | case "contains": |
| 894 | return strings.Contains(cell, q) |
| 895 | case "equals": |
| 896 | return cell == q |
| 897 | case "starts with": |
| 898 | return strings.HasPrefix(cell, q) |
| 899 | case "ends with": |
| 900 | return strings.HasSuffix(cell, q) |
| 901 | case "regex": |
| 902 | // When using regex, the user has full control over case sensitivity in the pattern. |
| 903 | re, err := regexp.Compile(options.Query) |
| 904 | if err != nil { |
| 905 | return false // Invalid regex |
| 906 | } |
| 907 | return re.MatchString(cellValue) |
| 908 | default: |
no test coverage detected