Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/helpers/strings.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -21,7 +28,7 @@ export function excerpt(string, length) {
return string;
}

return string.slice(0, length) + '...';
return string.slice(0, length) + suffix;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/helpers/strings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down