Skip to content

[BREAKING CHANGE] Use native Promises #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 38 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,50 @@
/*
Import modules
*/
const BBPromise = require( 'bluebird' );
const cheerio = require( 'cheerio' );
const fs = BBPromise.promisifyAll( require( 'fs' ) );

const index = require( './lib/index.js' );

/**
* Default exported function that takes a url string or
* request library options dictionary and returns a
* BBPromise for all available metadata
* Promise for all available metadata
*
* @param {Object} urlOrOpts url String or options dictionary
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports = module.exports = function ( urlOrOpts ) {
let url, opts;
if ( urlOrOpts instanceof Object ) {
if ( urlOrOpts.uri ) {
url = urlOrOpts.uri;
return new Promise( ( resolve, reject ) => {
let url, opts;
if ( urlOrOpts instanceof Object ) {
if ( urlOrOpts.uri ) {
url = urlOrOpts.uri;
}
opts = urlOrOpts;
} else if ( typeof urlOrOpts === String ) {
url = urlOrOpts;
}
opts = urlOrOpts;
} else if ( typeof urlOrOpts === String ) {
url = urlOrOpts;
}
if ( !url ) {
return BBPromise.reject( 'No uri supplied in argument' );
} else {
return BBPromise.resolve(
// eslint-disable-next-line n/no-unsupported-features/node-builtins
fetch( url, opts ).then(
( response ) => response.text().then(
( body ) => index.parseAll( cheerio.load( body ) )
if ( !url ) {
reject( 'No uri supplied in argument' );
} else {
resolve(
// eslint-disable-next-line n/no-unsupported-features/node-builtins
fetch( url, opts ).then(
( response ) => response.text().then(
( body ) => index.parseAll( cheerio.load( body ) )
)
)
)
);
}
};

/**
* Exported function that takes html file and
* returns a BBPromise for all available metadata
*
* @param {string} path path Path to HTML file
* @param {Object} [opts] opts Additional options such as encoding
* @return {Object} BBPromise for metadata
*/
exports.loadFromFile = function ( path, opts ) {
const defaultEncoding = 'utf-8';

opts = opts || defaultEncoding;

return fs.readFileAsync( path, opts ).then(
( html ) => index.parseAll( cheerio.load( html ) )
);
);
}
} );
};

/**
* c
* Exported function that takes html string and
* returns a BBPromise for all available metadata
* returns a Promise for all available metadata
*
* @param {string} html html String HTML of the page
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.loadFromString = function ( html ) {
return index.parseAll( cheerio.load( html ) );
Expand All @@ -83,7 +64,7 @@ exports.loadFromString = function ( html ) {
* using the same keys as in metadataFunctions.
*
* @param {Object} chtml html Cheerio object to parse
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseAll = function ( chtml ) {
return index.parseAll( chtml );
Expand All @@ -93,7 +74,7 @@ exports.parseAll = function ( chtml ) {
* Scrapes BE Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseBEPress = function ( chtml ) {
return index.parseBEPress( chtml );
Expand All @@ -103,7 +84,7 @@ exports.parseBEPress = function ( chtml ) {
* Scrapes embedded COinS data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseCOinS = function ( chtml ) {
return index.parseCOinS( chtml );
Expand All @@ -113,7 +94,7 @@ exports.parseCOinS = function ( chtml ) {
* Parses value of COinS title tag
*
* @param {string} title String corresponding to value of title tag in span element
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseCOinSTitle = function ( title ) {
return index.parseCOinSTitle( title );
Expand All @@ -123,7 +104,7 @@ exports.parseCOinSTitle = function ( title ) {
* Scrapes Dublin Core data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseDublinCore = function ( chtml ) {
return index.parseDublinCore( chtml );
Expand All @@ -133,7 +114,7 @@ exports.parseDublinCore = function ( chtml ) {
* Scrapes EPrints data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseEprints = function ( chtml ) {
return index.parseEprints( chtml );
Expand All @@ -143,7 +124,7 @@ exports.parseEprints = function ( chtml ) {
* Scrapes general metadata terms given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseGeneral = function ( chtml ) {
return index.parseGeneral( chtml );
Expand All @@ -153,7 +134,7 @@ exports.parseGeneral = function ( chtml ) {
* Scrapes Highwire Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseHighwirePress = function ( chtml ) {
return index.parseHighwirePress( chtml );
Expand All @@ -163,7 +144,7 @@ exports.parseHighwirePress = function ( chtml ) {
* Retrieves JSON-LD for given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for JSON-LD
* @return {Object} Promise for JSON-LD
*/
exports.parseJsonLd = function ( chtml ) {
return index.parseJsonLd( chtml );
Expand All @@ -173,7 +154,7 @@ exports.parseJsonLd = function ( chtml ) {
* Scrapes OpenGraph data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseOpenGraph = function ( chtml ) {
return index.parseOpenGraph( chtml );
Expand All @@ -183,7 +164,7 @@ exports.parseOpenGraph = function ( chtml ) {
* Scrapes schema.org microdata given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseSchemaOrgMicrodata = function ( chtml ) {
return index.parseSchemaOrgMicrodata( chtml );
Expand All @@ -193,7 +174,7 @@ exports.parseSchemaOrgMicrodata = function ( chtml ) {
* Scrapes Twitter data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parseTwitter = function ( chtml ) {
return index.parseTwitter( chtml );
Expand All @@ -203,7 +184,7 @@ exports.parseTwitter = function ( chtml ) {
* Scrapes PRISM data given html object
*
* @param {Object} chtml html Cheerio object
* @return {Object} BBPromise for metadata
* @return {Object} Promise for metadata
*/
exports.parsePrism = function ( chtml ) {
return index.parsePrism( chtml );
Expand Down
Loading