fix: restore release build type safety
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import type { FilterCondition } from '../types'
|
||||
|
||||
function toStringValue(value: unknown): string {
|
||||
type ConditionText = string
|
||||
type PropertyValue = string
|
||||
type PropertyArrayOperator = (field: PropertyArrayField, value: ConditionText, cond: FilterCondition) => boolean
|
||||
|
||||
function toStringValue(value: unknown): ConditionText {
|
||||
if (value == null) return ''
|
||||
if (typeof value === 'string') return value
|
||||
return String(value)
|
||||
}
|
||||
|
||||
function conditionList(value: unknown): string[] | null {
|
||||
function conditionList(value: unknown): ConditionText[] | null {
|
||||
return Array.isArray(value) ? value.map(toStringValue) : null
|
||||
}
|
||||
|
||||
@@ -17,42 +21,50 @@ function textMatchResult(op: FilterCondition['op'], matched: boolean): boolean {
|
||||
}
|
||||
|
||||
class PropertyArrayField {
|
||||
private readonly normalizedValues: Set<string>
|
||||
private readonly values: PropertyValue[]
|
||||
private readonly normalizedValues: Set<PropertyValue>
|
||||
|
||||
constructor(private readonly values: string[]) {
|
||||
constructor(values: PropertyValue[]) {
|
||||
this.values = values
|
||||
this.normalizedValues = new Set(values.map((value) => value.toLowerCase()))
|
||||
}
|
||||
|
||||
contains(target: string): boolean {
|
||||
contains(target: ConditionText): boolean {
|
||||
return this.normalizedValues.has(target.toLowerCase())
|
||||
}
|
||||
|
||||
equals(target: string): boolean {
|
||||
equals(target: ConditionText): boolean {
|
||||
return this.values.length === 1 && this.contains(target)
|
||||
}
|
||||
|
||||
matchesAny(targets: string[] | null): boolean {
|
||||
matchesAny(targets: ConditionText[] | null): boolean {
|
||||
return targets?.some((target) => this.contains(target)) ?? false
|
||||
}
|
||||
|
||||
matchesRegex(regex: RegExp): boolean {
|
||||
return this.values.some((value) => regex.test(value))
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
return this.values.length === 0
|
||||
}
|
||||
}
|
||||
|
||||
const PROPERTY_ARRAY_OPERATORS = {
|
||||
const PROPERTY_ARRAY_OPERATORS: Partial<Record<FilterCondition['op'], PropertyArrayOperator>> = {
|
||||
contains: (field, value) => field.contains(value),
|
||||
not_contains: (field, value) => !field.contains(value),
|
||||
equals: (field, value) => field.equals(value),
|
||||
not_equals: (field, value) => !field.equals(value),
|
||||
any_of: (field, _value, cond) => field.matchesAny(conditionList(cond.value)),
|
||||
none_of: (field, _value, cond) => !field.matchesAny(conditionList(cond.value)),
|
||||
} satisfies Partial<Record<FilterCondition['op'], (field: PropertyArrayField, value: string, cond: FilterCondition) => boolean>>
|
||||
is_empty: (field) => field.isEmpty(),
|
||||
is_not_empty: (field) => !field.isEmpty(),
|
||||
}
|
||||
|
||||
export function evaluatePropertyArrayCondition(
|
||||
cond: FilterCondition,
|
||||
values: string[],
|
||||
condVal: string,
|
||||
values: PropertyValue[],
|
||||
condVal: ConditionText,
|
||||
regex: RegExp | null,
|
||||
): boolean {
|
||||
const field = new PropertyArrayField(values)
|
||||
|
||||
Reference in New Issue
Block a user