-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (36 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const trim_php_1 = __importDefault(require("trim-php"));
function trimCharacters(text, charLength, breakWord, more) {
let sep = ' ';
if (typeof text === 'undefined') {
text = '';
}
if (typeof charLength !== 'number') {
charLength = 155;
}
if (typeof breakWord !== 'boolean') {
breakWord = true;
}
if (typeof more !== 'string') {
more = '…';
}
text = text.replace(/<(script|style)([\S\s]*?)>([\S\s]*?)<\/(script|style)>/ig, '');
text = text.replace(/(<([^>]+)>)/ig, '');
text = new trim_php_1.default().trim(text);
text = text.replace(/[\n\r\t ]+/g, ' ');
if (text.length > charLength) {
if (breakWord) {
text = text.substring(0, charLength);
}
else {
text = text.substr(0, text.lastIndexOf(sep, charLength));
}
text = text + more;
}
return text;
}
module.exports = trimCharacters;