@devmehq/open-graph-extractor
Version:
Fast, lightweight Open Graph, Twitter Card, and structured data extractor for Node.js with caching and validation
68 lines (67 loc) • 1.66 kB
JavaScript
;
/*
* validates the url
* @param string var - the url we want to scrape
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isUrlValid = isUrlValid;
exports.findImageTypeFromUrl = findImageTypeFromUrl;
exports.isImageTypeValid = isImageTypeValid;
exports.removeNestedUndefinedValues = removeNestedUndefinedValues;
function isUrlValid(url) {
return typeof url === "string" && url.length > 0 && url.indexOf("http") === 0 && url.indexOf(" ") === -1;
}
/*
* forces url to start with http://
* @param string var - the url we want to scrape
*/
/*
* validate timeout - how long should we wait for a request
* @param number var - the time we want to wait
*/
/*
* findImageTypeFromUrl
* @param string url - image url
*/
function findImageTypeFromUrl(url) {
let type = url.split(".").pop();
[type] = type.split("?");
return type;
}
/*
* isImageTypeValid
* @param string type - image type
*/
function isImageTypeValid(type) {
const validImageTypes = [
"apng",
"bmp",
"gif",
"ico",
"cur",
"jpg",
"jpeg",
"jfif",
"pjpeg",
"pjp",
"png",
"svg",
"tif",
"tiff",
"webp",
];
return validImageTypes.includes(type);
}
/*
* removeNestedUndefinedValues
* @param object - an object
*/
function removeNestedUndefinedValues(object) {
Object.entries(object).forEach(([key, value]) => {
if (value && typeof value === "object")
removeNestedUndefinedValues(value);
else if (value === undefined)
delete object[key];
});
return object;
}