Skip to content

Conformance Examples 0.1.0 — Transforms: transform-truncate

truncate-default-ellipsis

Default ellipsis is "..."; ellipsis is included in the total length.

Template

{{ phrase | truncate: 11 }}

Context

{
    "phrase": "Ground control to Major Tom"
}

Output

Ground c...

truncate-custom-ellipsis

Custom ellipsis replaces the default and is counted toward total length.

Template

{{ phrase | truncate: 11 ", and so on" }}

Context

{
    "phrase": "Ground control to Major Tom"
}

Output

, and so on

truncate-input-shorter-than-length

When input is shorter than length, return input unchanged.

Template

{{ phrase | truncate: 100 }}

Context

{
    "phrase": "short"
}

Output

short

truncate-length-equals-input-length

When input length exactly matches the requested length, return input unchanged.

Template

{{ phrase | truncate: 5 }}

Context

{
    "phrase": "short"
}

Output

short

truncate-length-smaller-than-ellipsis

When length is smaller than the ellipsis length, return the first length chars of the ellipsis.

Template

{{ phrase | truncate: 2 }}

Context

{
    "phrase": "hello world"
}

Output

..

truncate-empty-ellipsis

Empty ellipsis means a plain take(length) truncation.

Template

{{ phrase | truncate: 5 "" }}

Context

{
    "phrase": "hello world"
}

Output

hello

truncate-non-integer-length-is-null

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

Template

{{ phrase | truncate: "nope" }}

Context

{
    "phrase": "hi"
}

Output

null

truncate-negative-length-is-null

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

Template

{{ phrase | truncate: -1 }}

Context

{
    "phrase": "hi"
}

Output

null

truncate-astral-counts-code-points

Length counts code points, so truncating an all-astral string keeps whole characters rather than splitting a surrogate pair.

Template

{{ s | truncate: 2 "" }}

Context

{
    "s": "😀😁😂🤣"
}

Output

😀😁

Source fixture: transforms/transform-truncate.json