Hi again :smiley: Could you please add support fo...
# sdk-swift
l
Hi again 😃 Could you please add support for Mongo's
mod
operator in SDK?
Copy code
{
  "attribute": {
    "$mod": [
      2,
      1
    ]
  }
}
👀 1
p
Hi Thanks for the request! Just to set expectations: $mod isn't currently part of GrowthBook's condition spec — it's not implemented in the TS SDK (which is the reference implementation) or in any other SDK, and it's not available in the targeting UI. So this isn't a Swift parity gap, it's a new platform-wide feature. Adding it only to Swift would create a divergence: you'd be able to write the condition in Advanced JSON mode, but it would silently evaluate to false on every other platform. There are also semantics to pin down first (negative numbers behave differently across languages, numeric strings, divide-by-zero), which need to land in the shared cases.json spec suite. Could you open an issue on github.com/growthbook/growthbook with your use case? I'm happy to do the Swift implementation once the core team signs off on the operator and its spec.
l
Thanks Mykyta, The main repository usually ignores my requests, unlike you. 😃 But I'll create feature request. As for my case, will the regex option work for now? For example,
[02468]$
in advanced JSON mode?
p
Yes, that'll work - we just verified it against the Swift SDK (
1.1.12
), with id passed as a numeric attribute:
{ "id": { "$regex": "[02468]$" } }
Both JS and Swift coerce the attribute to a string before matching (Swift via stringValue, J_S via RegExp.test()_), so it works whether your id is a string or a number. Three caveats: 1. It only covers divisors of powers of 10. Trailing digits give you
$mod [2,x], [5,x], [10,x], [100,x] ("00$")
, but there's no regex equivalent for
[3,1]
or
[7,2]
— divisibility by 3 or 7 isn't determined by the last digits. It's also not exact at the low end:
"07$"
won't match id = 7, even though 7 % 100 == 7. 2. You only get a 50/50 split if your IDs are decimal numbers. If id is a
UUID
, the last character is hex
(0-9a-f)
, so
[02468]$
matches 5 of 16 possible characters — roughly 31%, not 50%. For UUIDs use
[02468ace]$
(8 of 16) to get an even split. 3. Don't drop the
$
anchor.
$regex
is an unanchored search, so
[02468]
on its own matches any ID containing an even digit anywhere — which is nearly all of them. And if what you actually need is a stable percentage split rather than the arithmetic property itself,
coverage + hashAttribute
is the better tool — it's uniform regardless of ID format and doesn't depend on IDs being sequential or gap-free. @late-ambulance-66508
l
nice, thanks!
🤝 1