Repository: harttle/liquidjs
Pull request #648 (2023-08-23 22:10:01)

fix: sort and where bug when using `strictVariables`
Apply the same fix for `map` to `sort` and `where` which also rely on `_getFromScope`.

djanhjo: Oops, wrong PR reviewed! 

Patch:
diff --git a/src/filters/array.ts b/src/filters/array.ts
index 9c3b8ac33..9aece587d 100644
--- a/src/filters/array.ts
+++ b/src/filters/array.ts
@@ -14,7 +14,7 @@ export function * sort<T> (this: FilterImpl, arr: T[], property?: string): Itera
   for (const item of toArray(toValue(arr))) {
     values.push([
       item,
-      property ? yield this.context._getFromScope(item, stringify(property).split('.')) : item
+      property ? yield this.context._getFromScope(item, stringify(property).split('.'), false) : item
     ])
   }
   return values.sort((lhs, rhs) => {
@@ -70,7 +70,7 @@ export function * where<T extends object> (this: FilterImpl, arr: T[], property:
   const values: unknown[] = []
   arr = toArray(toValue(arr))
   for (const item of arr) {
-    values.push(yield this.context._getFromScope(item, stringify(property).split('.')))
+    values.push(yield this.context._getFromScope(item, stringify(property).split('.'), false))
   }
   return arr.filter((_, i) => {
     if (expected === undefined) return isTruthy(values[i], this.context)
diff --git a/src/filters/html.ts b/src/filters/html.ts
index 73be53cb6..b296dc63d 100644
--- a/src/filters/html.ts
+++ b/src/filters/html.ts
@@ -28,7 +28,7 @@ export function escape_once (str: string) {
 }
 
 export function newline_to_br (v: string) {
-  return stringify(v).replace(/\n/g, '<br />\n')
+  return stringify(v).replace(/\r?\n/gm, '<br />\n')
 }
 
 export function strip_html (v: string) {
diff --git a/src/filters/string.ts b/src/filters/string.ts
index e7955387b..2a1013581 100644
--- a/src/filters/string.ts
+++ b/src/filters/string.ts
@@ -74,7 +74,7 @@ export function strip (v: string, chars?: string) {
 }
 
 export function strip_newlines (v: string) {
-  return stringify(v).replace(/\n/g, '')
+  return stringify(v).replace(/\r?\n/gm, '')
 }
 
 export function capitalize (str: string) {


Test patch:
diff --git a/test/integration/filters/html.spec.ts b/test/integration/filters/html.spec.ts
index f40c12d09..460a7aeb1 100644
--- a/test/integration/filters/html.spec.ts
+++ b/test/integration/filters/html.spec.ts
@@ -30,7 +30,7 @@ describe('filters/html', function () {
     it('should support string_with_newlines', function () {
       const src = '{% capture string_with_newlines %}\n' +
               'Hello\n' +
-              'there\n' +
+              'there\r\n' +
               '{% endcapture %}' +
               '{{ string_with_newlines | newline_to_br }}'
       const dst = '<br />\n' +
diff --git a/test/integration/filters/string.spec.ts b/test/integration/filters/string.spec.ts
index d8bdf93f7..bc54e07ce 100644
--- a/test/integration/filters/string.spec.ts
+++ b/test/integration/filters/string.spec.ts
@@ -137,6 +137,12 @@ describe('filters/string', function () {
             '{{ string_with_newlines | strip_newlines }}',
     'Hellothere')
   })
+  it('should support strip_newlines on Windows newlines ', function () {
+    return test('{% capture string_with_newlines %}\n' +
+            'Hello\r\nthere\n{% endcapture %}' +
+            '{{ string_with_newlines | strip_newlines }}',
+    'Hellothere')
+  })
   describe('truncate', function () {
     it('should truncate when string too long', function () {
       return test('{{ "Ground control to Major Tom." | truncate: 20 }}',