@redsift/text-utilities
Version:
Tiny library wrapping text processing code snippets.
46 lines (35 loc) • 1.67 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.text_utilities = global.text_utilities || {})));
}(this, (function (exports) { 'use strict';
var version = "0.1.4";
function splitWords(s) {
return s.split(/[\t\r\n\u00A0 !\"#$%&()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]/).filter(function (t) { return t !== ''; });
}
var PATTERNS = [
// looks for a line that includes an email address between angle brackets
/^.*[\d]{2,}.*(?:\r\n|\n)?<?[\r\n\s]{0,3}[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,10}[\r\n\s]{0,3}>?[\n]{0,2}.*:$[\s>]+/m, // https://regex101.com/r/midajo/3
// looks for a line that starts with from: and includes an email address between angle brackets
/^[>\s]*(?:from|to):.*[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,10}[\r\n\s]{0,3}>+/mi, // https://regex101.com/r/EBb7Y9/1
// looks for a line that includes "----- Original Message" and related variants
/^\s*---[-]*\s*\b(?:original|forward[\w]*|boundary)\b/mi
];
function trimThreads(s) {
for (var i=0; i<PATTERNS.length; i++) {
var idx = s.search(PATTERNS[i]);
if (idx !== -1) {
return trimThreads(s.substr(0, idx));
}
}
return s;
}
function replaceURLsinText(text, fn) {
return text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, fn);
}
exports.version = version;
exports.splitWords = splitWords;
exports.trimEmailThreads = trimThreads;
exports.replaceURLsinText = replaceURLsinText;
Object.defineProperty(exports, '__esModule', { value: true });
})));