Skip to content
Permalink
Browse files

Remove Node 4, 5 and 6 from support. Update npm dependencies. Use of …

…ES6.
  • Loading branch information...
marcomontalbano committed Jun 8, 2019
1 parent a177fb6 commit 69d32a8ef1c59a03406eaa4ad39e76b64b020569
Showing with 88 additions and 4,292 deletions.
  1. +4 −4 .travis.yml
  2. +1 −1 docs/app.min.js
  3. +74 −73 lib/index.js
  4. +0 −4,205 package-lock.json
  5. +7 −7 package.json
  6. +1 −1 test/{test.js → index.spec.js}
  7. +1 −1 test/{readme.md.js → readme.md.spec.js}
@@ -1,11 +1,11 @@
language: node_js

node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
- "10"
- "11"
- "12"

script:
- npm test
@@ -18,7 +18,7 @@ stages:
jobs:
include:
- stage: coverage
node_js: "8"
node_js: "10"
script:
- npm install coveralls codacy-coverage codeclimate-test-reporter
- npm run coverage

Large diffs are not rendered by default.

@@ -1,50 +1,54 @@
'use strict';

var cheerio = require('cheerio');
var _ = require('lodash');
const cheerio = require('cheerio');
const _ = require('lodash');

var _validateSelector = function(selector) {
const isString = s => typeof s === 'string';
const isArrayLike = a => typeof a.length === 'number';
const isObjectLike = o => o !== null && typeof o === 'object';
const isFunction = f => typeof f === 'function';

const validateSelector = selector => {
if (
! _.isString( selector ) &&
! _.isArrayLike( selector ) &&
! _.isObjectLike( selector ) &&
! _.isFunction( selector ) ) {
!isString(selector) &&
!isArrayLike(selector) &&
!isObjectLike(selector) &&
!isFunction(selector)) {
throw new Error('"selector" must be string, array, object or function');
}
};

var _validateHtml = function(html) {
const validateHtml = html => {

// 'html' is undefined
if ( html === undefined || html === null ) {
if (html === undefined || html === null) {
return false;
}

// html must be string or cheerio
if ( ! _.isString(html) && !html.type ) {
if (!isString(html) && !html.type) {
return false;
}

return true;
};

var _fetchSpecialKeys = function($, selector, memo)
{
var fetchSpecialKeys = ($, selector, memo) => {
var _specialKeys = {

_each_ : function($, specialKeySelector, selector, memo) {
_each_: ($, specialKeySelector, selector, memo) => {

var elements, element, $specialKeySelector = $(specialKeySelector);

if ($specialKeySelector.length === 0) {
return [];
}

$specialKeySelector.each(function(i, el) {
element = _htmlMiner(el, selector, {
$document : memo.$document,
globalData : memo.globalData,
manipulator : '_each_'
$specialKeySelector.each(function (i, el) {
element = htmlMiner(el, selector, {
$document: memo.$document,
globalData: memo.globalData,
manipulator: '_each_'
});

// remove empty objects.
@@ -63,73 +67,73 @@ var _fetchSpecialKeys = function($, selector, memo)

},

_container_ : function($, specialKeySelector, selector, memo) {
_container_: ($, specialKeySelector, selector, memo) => {

return _htmlMiner($(specialKeySelector).get(0), selector, {
$document : memo.$document,
globalData : memo.globalData,
manipulator : '_container_'
return htmlMiner($(specialKeySelector).get(0), selector, {
$document: memo.$document,
globalData: memo.globalData,
manipulator: '_container_'
});

}

};

var elements;
_.each(_specialKeys, function(value, key) {
if ( selector[key] !== undefined ) {
var specialKeySelector = selector[ key ];
Object.entries(_specialKeys).forEach(([key, callback]) => {
if (selector[key] !== undefined) {
var specialKeySelector = selector[key];

// selector must be string or function
if ( ! _.isString( specialKeySelector ) && ! _.isFunction( specialKeySelector ) ) {
if (!isString(specialKeySelector) && !isFunction(specialKeySelector)) {
throw new Error('"selector" of a special key must be string or function');
}

// special key - selector could be a complex function
if ( _.isFunction( specialKeySelector ) ) {
if (isFunction(specialKeySelector)) {
specialKeySelector = specialKeySelector.apply(this, [{
$ : memo.$document,
$scope : (memo.manipulator !== undefined) ? $.root().children() : $.root(),
globalData : memo.globalData,
scopeData : {}
$: memo.$document,
$scope: (memo.manipulator !== undefined) ? $.root().children() : $.root(),
globalData: memo.globalData,
scopeData: {}
}]);
}

var _selector = _.cloneDeepWith(selector);
delete _selector[ key ];
delete _selector[key];

elements = value($, specialKeySelector, _selector, memo);
elements = callback($, specialKeySelector, _selector, memo);
}
});

return elements;
};

var _fetchSelector = function(html, selector, memo, $, scopeData) {
const fetchSelector = (html, selector, memo, $, scopeData) => {

var elements = [];
let elements = [];

switch (true) {

case _.isObjectLike( selector ):
elements.push( _htmlMiner(html, selector, {
$document : memo.$document,
globalData : memo.globalData
}) );
case isObjectLike(selector):
elements.push(htmlMiner(html, selector, {
$document: memo.$document,
globalData: memo.globalData
}));
break;

case _.isFunction( selector ):
elements.push( selector.apply(this, [{
$ : memo.$document,
$scope : (memo.manipulator !== undefined) ? $.root().children() : $.root(),
globalData : memo.globalData,
scopeData : scopeData
}]) );
case isFunction(selector):
elements.push(selector.apply(this, [{
$: memo.$document,
$scope: (memo.manipulator !== undefined) ? $.root().children() : $.root(),
globalData: memo.globalData,
scopeData: scopeData
}]));
break;

case _.isString( selector ):
$( selector ).each(function(i, el) {
elements.push( _.trim( $(el).text() ) );
case isString(selector):
$(selector).each(function (i, el) {
elements.push(_.trim($(el).text()));
});
break;

@@ -139,44 +143,41 @@ var _fetchSelector = function(html, selector, memo, $, scopeData) {

};

var _htmlMiner = function(html, originalSelector, memo) {
const htmlMiner = (html, originalSelector, memo) => {

if ( _validateHtml(html) === false ) {
if (validateHtml(html) === false) {
return undefined;
}

_validateSelector( originalSelector );
validateSelector(originalSelector);

var $ = cheerio.load(html)
, selector = _.isObjectLike(originalSelector) ? originalSelector : { _default_ : originalSelector }
, scopeData = _.isArrayLike(originalSelector) ? [] : {}
, elements
;
const $document = cheerio.load(html);
const selector = isObjectLike(originalSelector) ? originalSelector : { _default_: originalSelector };
const scopeData = isArrayLike(originalSelector) ? [] : {};

memo = memo || {
$document : $,
globalData : scopeData,
manipulator : undefined
$document,
globalData: scopeData,
manipulator: undefined
};

if ( (elements = _fetchSpecialKeys($, selector, memo)) !== undefined ) {
let elements = fetchSpecialKeys($document, selector, memo);
if (elements !== undefined) {
return elements;
}

_.each(selector, function(selectorValue, selectorKey) {
elements = _fetchSelector( html, selectorValue, memo, $, scopeData );
Object.entries(selector).forEach(([selectorKey, selectorValue]) => {
elements = fetchSelector(html, selectorValue, memo, $document, scopeData);

// remove 'undefined' values
elements = _.filter(elements, function(o) { return o !== undefined; });
if ( elements.length >= 1 ) {
scopeData[ selectorKey ] = elements.length > 1 ? elements : elements[0];
elements = elements.filter(o => o !== undefined);

if (elements.length >= 1) {
scopeData[selectorKey] = elements.length > 1 ? elements : elements[0];
}
});

return _.isObjectLike(originalSelector) ? scopeData : scopeData._default_;

return isObjectLike(originalSelector) ? scopeData : scopeData._default_;
};

module.exports = function (html, selector) {
return _htmlMiner(html, selector);
};
module.exports = (html, selector) => htmlMiner(html, selector);

0 comments on commit 69d32a8

Please sign in to comment.
You can’t perform that action at this time.