mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat(filters): add squish filter (#943)
This commit is contained in:
@@ -94,6 +94,7 @@ filters:
|
|||||||
sort: sort.html
|
sort: sort.html
|
||||||
sort_natural: sort_natural.html
|
sort_natural: sort_natural.html
|
||||||
split: split.html
|
split: split.html
|
||||||
|
squish: squish.html
|
||||||
strip: strip.html
|
strip: strip.html
|
||||||
strip_html: strip_html.html
|
strip_html: strip_html.html
|
||||||
strip_newlines: strip_newlines.html
|
strip_newlines: strip_newlines.html
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
title: squish
|
||||||
|
---
|
||||||
|
|
||||||
|
{% since %}v10.28.0{% endsince %}
|
||||||
|
|
||||||
|
Removes leading and trailing whitespace from a string, and replaces every run of whitespace inside it with a single space.
|
||||||
|
|
||||||
|
Input
|
||||||
|
```liquid
|
||||||
|
{{ " Hello there,
|
||||||
|
Major Tom. " | squish }}
|
||||||
|
```
|
||||||
|
|
||||||
|
Output
|
||||||
|
```text
|
||||||
|
Hello there, Major Tom.
|
||||||
|
```
|
||||||
@@ -128,6 +128,12 @@ export function strip_newlines (this: FilterImpl, v: string) {
|
|||||||
return str.replace(/\r?\n/gm, '')
|
return str.replace(/\r?\n/gm, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function squish (this: FilterImpl, v: string) {
|
||||||
|
const str = stringify(v)
|
||||||
|
this.context.memoryLimit.use(str.length)
|
||||||
|
return str.replace(/\s+/g, ' ').trim()
|
||||||
|
}
|
||||||
|
|
||||||
export function capitalize (this: FilterImpl, str: string) {
|
export function capitalize (this: FilterImpl, str: string) {
|
||||||
str = stringify(str)
|
str = stringify(str)
|
||||||
this.context.memoryLimit.use(str.length)
|
this.context.memoryLimit.use(str.length)
|
||||||
|
|||||||
@@ -153,6 +153,26 @@ describe('filters/string', function () {
|
|||||||
'{{ string_with_newlines | strip_newlines }}',
|
'{{ string_with_newlines | strip_newlines }}',
|
||||||
'Hellothere')
|
'Hellothere')
|
||||||
})
|
})
|
||||||
|
describe('squish', function () {
|
||||||
|
it('should collapse whitespace between words', function () {
|
||||||
|
return test('{{ "Hello World!" | squish }}', 'Hello World!')
|
||||||
|
})
|
||||||
|
it('should strip leading and trailing whitespace', function () {
|
||||||
|
return test('{{ " HelloWorld! " | squish }}', 'HelloWorld!')
|
||||||
|
})
|
||||||
|
it('should treat newlines and tabs as whitespace', function () {
|
||||||
|
return test('{{ " \n\t\r\nHello \n\t World! \n" | squish }}', 'Hello World!')
|
||||||
|
})
|
||||||
|
it('should return empty string for whitespace only', function () {
|
||||||
|
return test('{{ " \n\t " | squish }}', '')
|
||||||
|
})
|
||||||
|
it('should stringify a number', function () {
|
||||||
|
return test('{{ 5 | squish }}', '5')
|
||||||
|
})
|
||||||
|
it('should return empty string for undefined', function () {
|
||||||
|
return test('{{ nosuchthing | squish }}', '')
|
||||||
|
})
|
||||||
|
})
|
||||||
describe('truncate', function () {
|
describe('truncate', function () {
|
||||||
it('should truncate when string too long', function () {
|
it('should truncate when string too long', function () {
|
||||||
return test('{{ "Ground control to Major Tom." | truncate: 20 }}',
|
return test('{{ "Ground control to Major Tom." | truncate: 20 }}',
|
||||||
|
|||||||
Reference in New Issue
Block a user