Skip to content

Commit

Permalink
Extract util methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 25, 2016
1 parent b788d93 commit 0781058
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
39 changes: 5 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Module Dependencies
*/

var has = Object.prototype.hasOwnProperty
var Crawler = require('x-ray-crawler')
var assign = require('object-assign')
var cheerio = require('cheerio')
var compact = require('./lib/util').compact
var isArray = require('./lib/util').isArray
var isUrl = require('./lib/util').isUrl
var root = require('./lib/util').root
var enstore = require('enstore')
var isUrl = require('is-url')
var isArray = Array.isArray
var cheerio = require('cheerio')
var fs = require('fs')

/**
Expand Down Expand Up @@ -221,36 +222,6 @@ function load (html, url) {
return $
}

/**
* Get the root, if there is one.
*
* @param {Mixed}
* @return {Boolean|String}
*/
function root (selector) {
return (typeof selector === 'string' || isArray(selector)) &&
!~selector.indexOf('@') &&
!isUrl(selector) &&
selector
}

/**
* Compact an array,
* removing empty objects
*
* @param {Array} arr
* @return {Array}
*/

function compact (arr) {
return arr.filter(function (val) {
if (!val) return false
if (val.length !== undefined) return val.length !== 0
for (var key in val) if (has.call(val, key)) return true
return false
})
}

function WalkHTML (xray, selector, scope) {
return function _walkHTML ($, fn) {
walk(selector, function (v, k, next) {
Expand Down
41 changes: 41 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

var isUrl = require('is-url')
var isArray = Array.isArray
var has = Object.prototype.hasOwnProperty

/**
* Get the root, if there is one.
*
* @param {Mixed}
* @return {Boolean|String}
*/
function root (selector) {
return (typeof selector === 'string' || isArray(selector)) &&
!~selector.indexOf('@') &&
!isUrl(selector) &&
selector
}

/**
* Compact an array,
* removing empty objects
*
* @param {Array} arr
* @return {Array}
*/
function compact (arr) {
return arr.filter(function (val) {
if (!val) return false
if (val.length !== undefined) return val.length !== 0
for (var key in val) if (has.call(val, key)) return true
return false
})
}

module.exports = {
root: root,
isUrl: isUrl,
isArray: isArray,
compact: compact
}

0 comments on commit 0781058

Please sign in to comment.