-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (30 loc) · 941 Bytes
/
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
"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 trimWords(text, numWords, more) {
const sep = ' ';
let wordsArray = [];
if (!numWords) {
numWords = 55;
}
if (!more) {
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);
wordsArray = text.split(/[\n\r\t ]+/, numWords + 1);
if (wordsArray.length > numWords) {
wordsArray.pop();
text = wordsArray.join(sep);
text = text + more;
}
else {
text = wordsArray.join(sep);
}
return text;
}
exports.default = trimWords;