UNPKG

audible-api

Version:

A Node.js API for searching the audible website

122 lines (83 loc) 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stripDiacretics = exports.simplify = exports.removeSpaces = exports.removeNonWordChars = exports.getCopyrightYear = exports.fuzzyMatch = exports.cleanUrl = exports.cleanTitle = exports.cleanNarratorUrl = exports.cleanDescription = exports.checkAuthorOverlap = void 0; var cleanDescription = function cleanDescription(description) { var synopsis = description; // Remove any inline tags synopsis = synopsis.replace(/<\/?(i|em|u|b|strong)>/gi, ""); // Remove all empty tags (used for spacing on audible) synopsis = synopsis.replace(/<\w+><\/\w+>/g, ""); // Remove the openings of any block level elements (and surrounding spaces) synopsis = synopsis.replace(/\s*<(ul|ol)>\s*/gi, ""); // Repace the starts/ends of any paragraphs (and surrounding spaces) with a double new line synopsis = synopsis.replace(/\s*<\/?p>\s*/gi, "\n\n"); // Repace the ends of any list elements (and surrounding spaces) with a new line synopsis = synopsis.replace(/\s*<\/(ul|ol|li)>\s*/gi, "\n"); // Repace any break tag (and surrounding spaces) with a new line synopsis = synopsis.replace(/ *<br(\s*\/?)?> */gi, "\n"); // Replace the start of any list items with a bullet character synopsis = synopsis.replace(/<li>\s*/gi, " • "); // Repace the any instances of 3 or more newline characters with 2 newline characters synopsis = synopsis.replace(/\n{3,}/g, "\n\n"); // Remove any trailing or leading spaces synopsis = synopsis.trim(); return synopsis; }; exports.cleanDescription = cleanDescription; var stripDiacretics = function stripDiacretics(str) { return str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); }; exports.stripDiacretics = stripDiacretics; var removeNonWordChars = function removeNonWordChars(str) { return str.replace(/\W+/g, " "); }; exports.removeNonWordChars = removeNonWordChars; var removeSpaces = function removeSpaces(str) { return str.replace(/\s/g, ""); }; exports.removeSpaces = removeSpaces; var simplify = function simplify(str) { return removeSpaces(removeNonWordChars(stripDiacretics(str).toLowerCase())); }; exports.simplify = simplify; var fuzzyMatch = function fuzzyMatch(str1, str2) { var checkIncludes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var simpleStr1 = simplify(str1); var simpleStr2 = simplify(str2); return simpleStr1 === simpleStr2 || checkIncludes && (simpleStr1.includes(simpleStr2) || simpleStr2.includes(simpleStr1)); }; exports.fuzzyMatch = fuzzyMatch; var cleanTitle = function cleanTitle(title) { var newTitle = title.trim(); // If the title ends with a series part, remove it // works for "Book 1" and "Book One" newTitle = newTitle.replace(/, book [\w\s-]+$/i, "").trim(); // If the title ends with "unabridged", with or without parenthesis // remove them; case insensitive newTitle = newTitle.replace(/\(?unabridged\)?$/i, "").trim(); // If there are 2 or more spaces in a row, replace them with a single space newTitle = newTitle.replace(/\s{2,}/g, " "); return newTitle; }; exports.cleanTitle = cleanTitle; var getCopyrightYear = function getCopyrightYear(copyright) { if (!copyright) { return ""; } var yearMatch = copyright.match(/\d{4}/); if (yearMatch) { return yearMatch[0]; } return ""; }; exports.getCopyrightYear = getCopyrightYear; var checkAuthorOverlap = function checkAuthorOverlap(authors1, authors2) { for (var i = 0; i < authors1.length; i += 1) { for (var j = 0; j < authors2.length; j += 1) { if (fuzzyMatch(authors1[i].name, authors2[j].name)) { return true; } } } return false; }; exports.checkAuthorOverlap = checkAuthorOverlap; var cleanUrl = function cleanUrl(url) { return url.replace(/\?.*$/, "").trim(); }; exports.cleanUrl = cleanUrl; var cleanNarratorUrl = function cleanNarratorUrl(url) { return url.replace(/[?&]ref=.*$/, "").trim(); }; exports.cleanNarratorUrl = cleanNarratorUrl; //# sourceMappingURL=string.js.map