harttle / liquidjs

A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
https://liquidjs.com
MIT License
1.52k stars 238 forks source link

sort filter modifies original array #475

Closed pdehaan closed 2 years ago

pdehaan commented 2 years ago

Noticed this in Eleventy v1.0.0 (w/ liquidjs@9.34.0).

If I have an array, and I sort it using the sort filter, it seems to modify my original array. I think I may have expected the sorted array to be a copy of my original and not modifying the reference to the array. I can work around it, but thought maybe worth filing (although i'm sure changing the default behavior may have other backwards compatibility issues).

const { Liquid } = require('liquidjs');

const engine = new Liquid();
const tpl = engine.parse('{{ arr | sort }}');

const arr = ["one", "two", "three", "four", "five"];

engine.render(tpl, {arr}).then(console.log); // ["five", "four", "one", "three", "two"]
console.log(arr); // Actual: "five,four,one,three,two"
                  // Expected: "one,two,three,four,five"