@rashedmakkouk/dev-utils
Version:
Utility library.
40 lines (39 loc) • 1.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const autolinker_1 = __importDefault(require("autolinker"));
const isString_1 = __importDefault(require("lodash/isString"));
/**
* Parses a text string and returns links matching:
*
* - Hashtag **#**
* - Mention **\@**
* - URL **http**
*
* @returns Result object:
* - Object.links: Array of unique words links (e.g. mention, hashtag, url).
* - Object.matches: Array of all matched links metadata.
*/
function autolinks(text = '', options = {}) {
if (!(0, isString_1.default)(text)) {
return { links: [], matches: [] };
}
const { hashtag = 'twitter', mention = 'twitter', stripTrailingSlash = true, urls = true, } = options;
const matches = autolinker_1.default.parse(text, {
hashtag,
mention,
stripTrailingSlash,
urls,
});
const links = Object.values(matches).map((match) => {
return match.getMatchedText();
});
return {
links: [...new Set(links)],
matches,
};
}
exports.default = autolinks;