diff --git a/src/helpers/strings.js b/src/helpers/strings.js index 61b770f..fc95073 100644 --- a/src/helpers/strings.js +++ b/src/helpers/strings.js @@ -1,4 +1,4 @@ -import { isFunction, isObject, isString, isArray } from '../util/utils'; +import { isFunction, isObject, isString, isArray, isUndefined } from '../util/utils'; /** * Extract a few characters from a string. Default number of characters is 50. @@ -8,10 +8,17 @@ import { isFunction, isObject, isString, isArray } from '../util/utils'; * * @param {string} string * @param {int} length + * @param {any} args * @returns {string} */ -export function excerpt(string, length) { +export function excerpt(string, length, ...args) { length = parseInt(length) || 50; + + let params = {}; + if (isObject(args[0]) && isObject(args[0].hash)) { + params = args[0].hash; + } + let suffix = !isUndefined(params.suffix) ? params.suffix : '...'; if (typeof (string) !== 'string' || typeof (length) !== 'number') { return string; @@ -21,7 +28,7 @@ export function excerpt(string, length) { return string; } - return string.slice(0, length) + '...'; + return string.slice(0, length) + suffix; } /** diff --git a/tests/helpers/strings.spec.js b/tests/helpers/strings.spec.js index d89352c..475f058 100644 --- a/tests/helpers/strings.spec.js +++ b/tests/helpers/strings.spec.js @@ -35,6 +35,20 @@ describe('strings', () => { expect(template({ string: 'That can only mean one thing' })).toEqual('That can o...'); }); + + it('should work as expected after compilation with user-provided suffix', () => { + const template = compile('{{excerpt string 10 suffix=""}}'); + + expect(template({ string: 'That can only mean one thing' })).toEqual('That can o'); + }); + + it('should work as expected after compilation with user-provided suffix', () => { + const template = compile('{{excerpt string 10 suffix="asdf"}}'); + + expect(template({ string: 'That can only mean one thing' })).toEqual('That can oasdf'); + }); + + }); describe('sanitize', () => {