UNPKG

audible-api

Version:

A Node.js API for searching the audible website

71 lines (55 loc) 3.02 kB
export var 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; }; export var stripDiacretics = str => str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); export var removeNonWordChars = str => str.replace(/\W+/g, " "); export var removeSpaces = str => str.replace(/\s/g, ""); export var simplify = str => removeSpaces(removeNonWordChars(stripDiacretics(str).toLowerCase())); export 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)); }; export var 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; }; export var getCopyrightYear = copyright => { if (!copyright) { return ""; } var yearMatch = copyright.match(/\d{4}/); if (yearMatch) { return yearMatch[0]; } return ""; }; export var 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; }; export var cleanUrl = url => url.replace(/\?.*$/, "").trim(); export var cleanNarratorUrl = url => url.replace(/[?&]ref=.*$/, "").trim(); //# sourceMappingURL=string.js.map