UNPKG

771 BJavaScriptView Raw
1/**
2 * @function chop
3 * @param {string} text - Text to chop.
4 * @param {number} length - Length to chop.
5 * @param {object} options - Optional settings.
6 * @param {boolean} [options.ellipsis=false] - Use ellipsis.
7 * @returns {string} - Chopped text.
8 */
9
10"use strict";
11
12var argx = require('argx');
13
14var ELLIPSIS = '...';
15
16/** @lends chop */
17function chop(text, len, options) {
18 var args = argx(arguments);
19 text = args.shift('string');
20 len = args.shift('number');
21 options = args.pop('object') || {};
22 if(text.length < len){
23 return text;
24 }
25 if (options.ellipsis) {
26 return text.substr(0, len - ELLIPSIS.length) + ELLIPSIS;
27 } else {
28 return text.substr(0, len);
29 }
30
31}
32chop.ELLIPSIS = ELLIPSIS;
33
34module.exports = chop;