Skip to content

Add rc.slice custom operator - #7505

Open
ajpallares wants to merge 13 commits into
pallares/rules-engine-rc-sort-byfrom
pallares/rules-engine-rc-slice
Open

Add rc.slice custom operator#7505
ajpallares wants to merge 13 commits into
pallares/rules-engine-rc-sort-byfrom
pallares/rules-engine-rc-slice

Conversation

@ajpallares

@ajpallares ajpallares commented Aug 25, 2026

Copy link
Copy Markdown
Member

Checklist

  • If applicable, unit tests
  • If applicable, create follow-up issues for purchases-android and hybrids

Motivation

Keeping a run of an array has no operator: it composes from rc.entries + filter + map, but the index arrives as a string and negative-index clamping is hand-written, so simple filters like "the last three events" can be really hard to express and read.

Description

{"rc.slice": [array, start]} or {"rc.slice": [array, start, length]}.

  • Start and length mean what they already mean in substr: negative start counts from the end, negative length drops from the right, and both clamp instead of failing. The third argument is therefore a count, not an end index — deliberately substr's convention rather than JS slice's, so the engine has one indexing model.
  • Arrays only. A string throws and the message points at substr.
  • Indices must be whole numbers. .float is accepted since all arithmetic returns it, so length - 1 works; fractional or non-numeric throws instead of being truncated or coerced as substr does.

Note

Low Risk
Isolated new Rules Engine operator with broad fixture coverage; no changes to existing operator behavior beyond the dispatch switch.

Overview
Adds rc.slice so JSON Logic rules can take a contiguous run from an array without composing rc.entries + filter/map workarounds.

{"rc.slice": [array, start]} or with an optional length third argument. Indexing follows existing substr rules: negative start is from the end, negative length trims from the right, out-of-range values clamp instead of erroring, and the third arg is a count (not a JS-style end index). Only arrays are accepted—strings still use substr, with a hint in the error. Start/length must be whole numbers; whole-valued floats from arithmetic are allowed, but fractional or coerced values throw typeMismatch (stricter than substr).

Wired through CustomOperators.dispatch, added to the Xcode target, and covered by rc_slice.json predicate fixtures (fixture total 544 → 578).

Reviewed by Cursor Bugbot for commit ec948ab. Bugbot is set up for automated code reviews on this repo. Configure here.

Takes a run of elements out of an array, with the start and length
semantics substr already uses for strings. Pairs with rc.sortBy to
express top-N without a reduce carrying a compound accumulator.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ajpallares ajpallares added the pr:feat A new feature label Aug 25, 2026
The tenth operator pushed the switch past the limit. Operators.dispatch
carries the same exemption for the same reason.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ajpallares ajpallares mentioned this pull request Aug 25, 2026
2 tasks
ajpallares and others added 8 commits August 26, 2026 11:49
…ngine-rc-slice

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	Tests/UnitTests/RulesEngine/PredicateFixtureTests.swift
…ngine-rc-slice

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	Tests/UnitTests/RulesEngine/PredicateFixtureTests.swift
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
…ngine-rc-slice

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	Tests/UnitTests/RulesEngine/PredicateFixtureTests.swift
@ajpallares
ajpallares marked this pull request as ready for review August 26, 2026 18:30
@ajpallares
ajpallares requested a review from a team as a code owner August 26, 2026 18:30
@ajpallares
ajpallares requested a review from a team August 26, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants