All files listtojson.js

100% Statements 34/34
90% Branches 18/20
100% Functions 4/4
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112    2x 2x               10x               10x   10x   10x 10x   10x 41x   41x   41x   287x               287x     41x 41x     10x                                             4x 4x 4x   4x   1x     1x 1x     1x 1x 3x   1x     1x 1x     2x     2x 2x 1x           2x          
'use strict';
 
const AbstractToJson = require('pagetojson').AbstractToJson;
const cheerio = require('cheerio');
 
class listtojson extends AbstractToJson {
    /**
     * Static conversion of a given HTML Page
     * @return {Object} Converted list as array
     */
    static convert(html, options) {
        options = Object.assign(
            {
                containsClasses: null,
                id: null
            },
            options
        );
 
        const jsonResponse = [];
 
        const $ = cheerio.load(html);
 
        let additionalSelectors = options.containsClasses ? `.${options.containsClasses.join('.')}` : '';
        additionalSelectors = options.id ? `${additionalSelectors}#${options.id}` : additionalSelectors;
 
        $(`ol${additionalSelectors}, ul${additionalSelectors}`).each(function(i, ol) {
            const olAsJson = [];
 
            let lis = $(ol).find('li');
 
            lis.each(function(j, li) {
 
                const content = options.stripHtml
                    ? $(li)
                        .text()
                        .trim()
                    : $(li)
                        .html()
                        .trim();
 
                olAsJson.push(content);
            });
 
            Eif (olAsJson && olAsJson.length !== 0)
                jsonResponse.push(olAsJson);
        });
 
        return jsonResponse;
    }
 
    /**
     * Convert an HTML Page for a given URL
     * @param url URL to be called
     * @param arg1 {Object} Options for html conversion
     * @param arg1.useFirstRowForHeadings Use the first row as header [default=false]
     * @param arg1.stripHtmlFromHeadings Strip all HTML from headings [default=true]
     * @param arg1.stripHtmlFromCells Strip HTML from cells [default=true]
     * @param arg1.stripHtml Strip off HTML [default=null] if set true stripHtmlFromHeadings and stripHtmlFromCells will also be true
     * @param arg1.forceIndexAsNumber Force the index to be used as number [default=false]
     * @param arg1.countDuplicateHeadings If given a _<NUMBER> will be added to the duplicate key [default=false]
     * @param arg1.ignoreColumns {Array} Array of column indices to ignored [default=null]
     * @param arg1.onlyColumns {Array} Array of column indices to be used. Overrides ignoreColumn [default=null]
     * @param arg1.ignoreHiddenRows Ignoring hidden rows [default=true]
     * @param arg1.headings {Array} Array of Strings to be used as headings [default=null]
     * @param arg1.headings {Array} Array of classes to find a specific table [default=null]
     * @param arg1.request Options to be passed to request object
     * @param arg2 Callback function to be called when the conversion finished
     * @return {Promise<*>} Promise containing the result
     */
    static async convertUrl(url, arg1, arg2) {
        let options = null;
        let callback = null;
        let requestOptions = null;
 
        if (typeof arg2 === 'function') {
            // If both options and callback passed
            options = arg1;
            // If you need to pass in options for request (proxy)
            // add them to arg1.request
            requestOptions = options.request || {};
            callback = arg2;
 
            // Use a callback (if passed)
            const html = await AbstractToJson.fetchUrl(url, requestOptions);
            return callback.call(this, listtojson.convert(html, options));
        } else if (typeof arg1 === 'function') {
            // If only callback passed, invoke with no options
            callback = arg1;
 
            // Use a callback (if passed)
            const html = await AbstractToJson.fetchUrl(url);
            return callback.call(this, listtojson.convert(html, options));
        } else {
            // If neither argument is callback, return a promise
            options = arg1 || {};
            // If you need to pass in options for request (proxy)
            // add them to arg1.request
            requestOptions = options.request || {};
            const html = await AbstractToJson.fetchUrl(url, requestOptions);
            return listtojson.convert(html, options);
        }
    }
 
}
 
module.exports = {
    arrayNotEmpty: listtojson.arrayNotEmpty,
    convert: listtojson.convert,
    convertUrl: listtojson.convertUrl
};