Skip to content

array_to_sentence_string throws on nil, number and string input #964

Closed

Description

@josefguenther

array_to_sentence_string uses its input as an array without checking what it is. So a missing variable, a number or a string throws a RenderError, and the whole template fails to render. The other array filters (join, reverse, sort, compact, uniq, …) all pass their input through toArray first, so this filter is the odd one out.

Version: 10.29.0

Repro:

const { Liquid } = require('liquidjs')
const engine = new Liquid()

await engine.parseAndRender('{{ missing | array_to_sentence_string }}', {})
// RenderError: Cannot read properties of undefined (reading 'length')

await engine.parseAndRender('{{ n | array_to_sentence_string }}', { n: 5 })
// RenderError: array.slice is not a function

await engine.parseAndRender('{{ s | array_to_sentence_string }}', { s: 'abc' })
// RenderError: array.slice(...).join is not a function

await engine.parseAndRender('{{ s | array_to_sentence_string }}', { s: 'ab' })
// "a and b"  (the string's characters are treated as items)

For comparison, join handles the same inputs without trouble:

await engine.parseAndRender('{{ missing | join: ", " }}', {})       // ""
await engine.parseAndRender('{{ s | join: ", " }}', { s: 'abc' })   // "abc"

Cause: the filter reads array.length and calls array.slice on the raw input:

function array_to_sentence_string(array, connector = 'and') {
    connector = stringify(connector);
    let outputSize = connector.length + array.length * 2;
    ...
    default:
        return `${array.slice(0, -1).join(', ')}, ${connector} ${array[array.length - 1]}`;
}

Suggested fix: coerce the input with toArray the way the other array filters do.

function array_to_sentence_string(input, connector = 'and') {
    const array = toArray(input);
    connector = stringify(connector);
    ...
}

With that change, nil renders "", 5 renders "5", "abc" renders "abc", and ["x", "y", "z"] still renders "x, y, and z".

Happy to open a PR if that's useful.

Activity

  1. added a commit that references this issue on Oct 28, 2026
    e843466
  2. github-actions commented on Oct 28, 2026

    @github-actions

    🎉 This issue has been resolved in version 10.30.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions