mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
fix: truncatewords should use at least one word, #537
This commit is contained in:
@@ -89,9 +89,10 @@ export function truncate (v: string, l = 50, o = '...') {
|
|||||||
return v.substring(0, l - o.length) + o
|
return v.substring(0, l - o.length) + o
|
||||||
}
|
}
|
||||||
|
|
||||||
export function truncatewords (v: string, l = 15, o = '...') {
|
export function truncatewords (v: string, words = 15, o = '...') {
|
||||||
const arr = stringify(v).split(/\s+/)
|
const arr = stringify(v).split(/\s+/)
|
||||||
let ret = arr.slice(0, l).join(' ')
|
if (words <= 0) words = 1
|
||||||
if (arr.length >= l) ret += o
|
let ret = arr.slice(0, words).join(' ')
|
||||||
|
if (arr.length >= words) ret += o
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,6 +172,10 @@ describe('filters/string', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('truncatewords', function () {
|
describe('truncatewords', function () {
|
||||||
|
it('should truncate to 1 words if less than 1', function () {
|
||||||
|
return test('{{ "Ground control to Major Tom." | truncatewords: 0 }}',
|
||||||
|
'Ground...')
|
||||||
|
})
|
||||||
it('should truncate when too many words', function () {
|
it('should truncate when too many words', function () {
|
||||||
return test('{{ "Ground control to Major Tom." | truncatewords: 3 }}',
|
return test('{{ "Ground control to Major Tom." | truncatewords: 3 }}',
|
||||||
'Ground control to...')
|
'Ground control to...')
|
||||||
|
|||||||
Reference in New Issue
Block a user