mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-11 22:55:55 +05:45
* Add rc.indexOf custom operator Arrays get a position, which nothing else expressed: in answers whether a value is present, never where it sits. Strings report the position through LengthOperator.stringLength, the same function behind rc.length, so both operators always measure in one unit and a later change to that unit moves them together. Co-authored-by: Cursor <cursoragent@cursor.com> * Frame stringLength as the engine's single string measurement Co-authored-by: Cursor <cursoragent@cursor.com> * Trim rc.indexOf doc comment Co-authored-by: Cursor <cursoragent@cursor.com> * Move stringLength out of the rc.length operator It is the engine's measurement, not that operator's, and rc.indexOf had to reach across to another operator to state a position in the same unit. Mirrors the same move in purchases-android. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.8 KiB
Swift
50 lines
1.8 KiB
Swift
//
|
|
// LengthOperator.swift
|
|
//
|
|
// Created by Antonio Pallares.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension RulesEngine {
|
|
|
|
/// `rc.length` — element count for arrays, UTF-16 code-unit count for
|
|
/// strings (JS `String.length` parity).
|
|
enum LengthOperator {
|
|
|
|
/// `{"rc.length": value}` — returns a numeric length suitable for
|
|
/// arithmetic and comparisons.
|
|
///
|
|
/// - **String**: length in **UTF-16 code units**, not Swift grapheme
|
|
/// clusters — matches JS `String.length` and the same choice in
|
|
/// `AccessorOperators.opMissingSome` (`string.utf16.count`).
|
|
/// - **Array**: element count (`.count`).
|
|
/// - **Anything else**: throws `EvaluationError.typeMismatch`.
|
|
///
|
|
/// Non-string/non-array inputs throw rather than defaulting to `0`.
|
|
/// A silent zero would make `{"==": [{"rc.length": {"var": "note"}},
|
|
/// 0]}` quietly true for a key that is present but holds `null`.
|
|
///
|
|
/// Uses `Operators.firstArgEvaluated` spread semantics; extra arguments
|
|
/// are silently ignored. Literal array operands must be wrapped
|
|
/// (e.g. `{"rc.length": [[1, 2, 3]]}`), not passed as a multi-element
|
|
/// arg list.
|
|
static func opLength(args: Value, vars: Scope) throws -> Value {
|
|
let input = try Operators.firstArgEvaluated(args, vars: vars)
|
|
|
|
switch input {
|
|
case .string(let string):
|
|
return .int(Int64(RulesEngineUtils.stringLength(string)))
|
|
|
|
case .array(let items):
|
|
return .int(Int64(items.count))
|
|
|
|
default:
|
|
throw EvaluationError.typeMismatch(
|
|
message: "operator 'rc.length' expected string or array, got \(input)"
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|