Skip to content

Added find-first and find-last formatters with corresponding tests#58

Merged
phensley merged 4 commits into
mainfrom
lb/find-formatters
Jun 4, 2026
Merged

Added find-first and find-last formatters with corresponding tests#58
phensley merged 4 commits into
mainfrom
lb/find-formatters

Conversation

@lr-brown

@lr-brown lr-brown commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

This PR adds two new formatters to the compiler, find-first and find-last that allow you to find the first/last element that returns a truthy given a path and lookup. See this ticket for the reason we need these new formatters.

Syntax

find-last/first
find-last/first|PATH
find-last/first|LOOKUP PATH

Arguments

  • PATH (optional)
    • Dot-separated path within each element. Returns the last/first element where the value at this path is truthy.
  • LOOKUP (optional, used with PATH)
    • Context path resolving to an object. The array is treated as a list of keys; each key is looked up in this object and the last/first key whose looked-up value has a truthy value at PATH is returned.

Examples

Return the last element unconditionally:

JSON

{
  "items": ["a", "b", "c"]
}

Json-t

{items|find-last}

Output

c

Return the last element where a nested field is truthy:

JSON

{
  "items": [
    { "id": "a", "enabled": true },
    { "id": "b", "enabled": true },
    { "id": "c", "enabled": false }
  ]
}

Json-t

{.var @last items|find-last enabled}{@last.id}

Output

b

Look up each key in a separate object and return the last key with a truthy value at a path:

JSON

{
  "keys": ["a", "b", "c"],
  "map": {
    "a": { "enabled": false },
    "b": { "enabled": true },
    "c": { "enabled": true }
  }
}

Json-t

#pragma example=lookup
{keys|find-first map enabled}

Output

b

@lr-brown lr-brown self-assigned this Jun 2, 2026
@lr-brown lr-brown requested a review from phensley as a code owner June 2, 2026 19:42
@CLAassistant

CLAassistant commented Jun 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@lr-brown

lr-brown commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator Author

Wondering if having two explicit formatters is preferred over one that takes a first/last prop

@pscheffer

Copy link
Copy Markdown

Wondering if having two explicit formatters is preferred over one that takes a first/last prop

My 2cents is it makes sense to keep them split as it is analogous to :first-child :last-child filters we already use in the frontend.

@phensley

phensley commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

I think my point still stands about the 95% overlap in the code.

You could generalize this and create a function that takes 3 args, then implement 3 formatters that wrap that function find-first, find-last, and find-nth with less overall code (the "bundle bloat" concern for frontend).

CSS supports first-child, last-child, and nth-child, if you are concerned about parity with CSS pseudo-classes.

Comment thread core/src/main/java/com/squarespace/template/plugins/FindUtils.java Outdated
Comment thread core/src/test/java/com/squarespace/template/plugins/CoreFormattersTest.java Outdated
Comment thread core/src/main/java/com/squarespace/template/plugins/CoreFormatters.java Outdated
@lr-brown lr-brown requested a review from phensley June 4, 2026 15:10
@phensley phensley merged commit c8bdad1 into main Jun 4, 2026
4 checks passed
@phensley

phensley commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

This change is publishing to Maven Central (and should be there in a few minutes) in version 2.11.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants