Skip to content

Conformance Examples 0.1.0 — Transforms: transform-slice

slice-one-arg-mid-string

Single offset returns 1 character at that position.

Template

{{ phrase | slice: 1 }}

Context

{
    "phrase": "hello"
}

Output

e

slice-two-args

Offset + length returns a substring of the requested length.

Template

{{ phrase | slice: 1 3 }}

Context

{
    "phrase": "hello"
}

Output

ell

slice-negative-offset

Negative offset counts from the end.

Template

{{ phrase | slice: -3 2 }}

Context

{
    "phrase": "hello"
}

Output

ll

slice-length-exceeds-remaining

Length is clamped to the remaining string.

Template

{{ phrase | slice: 2 100 }}

Context

{
    "phrase": "hello"
}

Output

llo

slice-offset-fully-out-of-bounds

Positive offset past end yields empty string.

Template

{{ phrase | slice: 100 5 }}

Context

{
    "phrase": "hello"
}

Output


slice-negative-offset-out-of-bounds

Negative offset past start yields empty string.

Template

{{ phrase | slice: -100 5 }}

Context

{
    "phrase": "hello"
}

Output


slice-non-integer-offset-is-null

Non-integer offset is a soft failure, so slice yields null.

Template

{{ phrase | slice: "oops" }}

Context

{
    "phrase": "hi"
}

Output

null

slice-negative-length-is-null

Negative length is a soft failure, so slice yields null.

Template

{{ phrase | slice: 0 -1 }}

Context

{
    "phrase": "hi"
}

Output

null

slice-non-integer-length-is-null

Non-integer length is a soft failure, so slice yields null.

Template

{{ phrase | slice: 0 "oops" }}

Context

{
    "phrase": "hi"
}

Output

null

slice-astral-does-not-split-surrogate

Offsets count code points, so a surrogate pair is sliced out whole rather than split into a lone half.

Template

{{ s | slice: 1 1 }}

Context

{
    "s": "a😀b"
}

Output

😀

Source fixture: transforms/transform-slice.json