Skip to content

Commit

Permalink
updating code to be more es5 friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
jshemas committed Mar 29, 2018
1 parent b587afd commit c02827f
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 293 deletions.
144 changes: 4 additions & 140 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
{
"parserOptions": {
"ecmaVersion": 6,
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
},
"sourceType": "module"
},

"env": {
"es6": true,
"node": true
},

"plugins": [
"standard",
"promise"
"extends": [
"eslint-config-airbnb-es5"
],

"globals": {
"Promise" : false,
/* MOCHA */
"describe" : false,
"it" : false,
Expand All @@ -29,128 +15,6 @@
},

"rules": {
"accessor-pairs": 2,
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"camelcase": [2, { "properties": "never" }],
"comma-dangle": [2, "never"],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"constructor-super": 2,
"curly": [2, "multi-line"],
"dot-location": [2, "property"],
"dot-notation": 0,
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [1, { "before": true, "after": true }],
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
"new-parens": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-case-declarations": 0,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-console": 0,
"no-const-assign": 2,
"no-constant-condition": 0,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-dupe-args": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-duplicate-imports": 2,
"no-empty": 2,
"no-empty-character-class": 2,
"no-empty-pattern": 2,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": [2, "functions"],
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": [2, { "allowLoop": false, "allowSwitch": false }],
"no-lone-blocks": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [1, { "max": 1 }],
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 2,
"no-new-symbol": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-path-concat": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": [2, "except-parens"],
"no-self-assign": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unmodified-loop-condition": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unsafe-finally": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 2,
"no-useless-computed-key": 2,
"no-useless-constructor": 2,
"no-useless-escape": 2,
"no-whitespace-before-property": 1,
"no-with": 2,
"one-var": 0,
"operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [2, "never"],
"promise/param-names": 2,
"quotes": [2, "single", "avoid-escape"],
"semi": [1, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [1, "always"],
"space-before-function-paren": [1, "always"],
"space-in-parens": [1, "never"],
"space-infix-ops": 1,
"space-unary-ops": [1, { "words": true, "nonwords": false }],
"spaced-comment": [1, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"standard/array-bracket-even-spacing": [2, "either"],
"standard/computed-property-even-spacing": [2, "even"],
"standard/object-curly-even-spacing": [2, "either"],
"template-curly-spacing": [2, "never"],
"use-isnan": 2,
"valid-jsdoc": 0,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
"yield-star-spacing": [2, "both"],
"yoda": [2, "never"]
"func-names": 0
}
}
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ npm install open-graph-scraper

### Usage
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/'};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/'};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -24,8 +24,8 @@ ogs(options, function (error, results) {

You can set a timeout flag like... Example four seconds:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'timeout': 4000};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'timeout': 4000};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -34,8 +34,8 @@ ogs(options, function (error, results) {

You can set custom headers. For example scraping data in a specific language:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'headers': { 'accept-language': 'en' }};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'headers': { 'accept-language': 'en' }};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -44,8 +44,8 @@ ogs(options, function (error, results) {

You can set a blacklist. For example if you want to black list youtube.com:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'blacklist': ['youtube.com']};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'blacklist': ['youtube.com']};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -54,8 +54,8 @@ ogs(options, function (error, results) {

Example of setting encoding(default is `null`):
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'encoding': 'utf8'};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'encoding': 'utf8'};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -64,8 +64,8 @@ ogs(options, function (error, results) {

There is also a followAllRedirects(default is `true`) and a maxRedirects(default is `20`) option:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'followAllRedirects': true, 'maxRedirects': 20};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'followAllRedirects': true, 'maxRedirects': 20};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -74,8 +74,8 @@ ogs(options, function (error, results) {

If you would like the response of the page you scraped you can grab it as the third param:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/', 'timeout': 4000};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/', 'timeout': 4000};
ogs(options, function (error, results, response) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand All @@ -85,8 +85,8 @@ ogs(options, function (error, results, response) {

Promise Example:
```javascript
const ogs = require('open-graph-scraper');
const options = {'url': 'http://ogp.me/'};
var ogs = require('open-graph-scraper');
var options = {'url': 'http://ogp.me/'};
ogs(options)
.then(function (result) {
console.log('result:', result);
Expand All @@ -100,9 +100,9 @@ Note: By default if page dose not have something like a `og:title` tag it will t

It's possible to pass in an HTML string instead of a URL. There won't be a resonse object.
```javascript
const htmlString = /* html string goes here */;
const ogs = require('open-graph-scraper');
const options = {'html': htmlString};
var htmlString = /* html string goes here */;
var ogs = require('open-graph-scraper');
var options = {'html': htmlString};
ogs(options, function (error, results) {
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
console.log('results:', results);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const openGraphScraper = require('./lib/openGraphScraper');
var openGraphScraper = require('./lib/openGraphScraper');

module.exports = openGraphScraper;
15 changes: 8 additions & 7 deletions lib/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THE SOFTWARE.
// note: this was pull at version 1.0.1 from https://github.com/node-modules/charset

// eslint-disable-next-line
const CHARTSET_RE = /(?:charset|encoding)\s{0,10}=\s{0,10}['"]? {0,10}([\w\-]{1,100})/i;
var CHARTSET_RE = /(?:charset|encoding)\s{0,10}=\s{0,10}['"]? {0,10}([\w\-]{1,100})/i;

module.exports = charset;

Expand All @@ -50,19 +50,20 @@ module.exports = charset;
* If can\'t guest, return null
* @api public
*/
function charset (obj, data, peekSize) {
let matchs = null;
let end = 0;
function charset(obj, data, peekSize) {
var matchs = null;
var end = 0;
if (data) {
// eslint-disable-next-line
peekSize = peekSize || 512;
// https://github.com/node-modules/charset/issues/4
end = data.length > peekSize ? peekSize : data.length;
}
// charset('text/html;charset=gbk')
let contentType = obj;
var contentType = obj;
if (contentType && typeof contentType !== 'string') {
// charset(res.headers)
let headers = obj;
var headers = obj;
if (obj.headers) {
// charset(res)
headers = obj.headers;
Expand All @@ -78,7 +79,7 @@ function charset (obj, data, peekSize) {
contentType = data.slice(0, end).toString();
matchs = CHARTSET_RE.exec(contentType);
}
let cs = null;
var cs = null;
if (matchs) {
cs = matchs[1].toLowerCase();
if (cs === 'utf-8') {
Expand Down
2 changes: 1 addition & 1 deletion lib/fields.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const fields = [
var fields = [
{
multiple: false,
property: 'og:title',
Expand Down
25 changes: 12 additions & 13 deletions lib/media.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const fields = require('./fields');
var _ = require('lodash');
var fields = require('./fields');

/*
* media setup
Expand Down Expand Up @@ -66,7 +66,7 @@ exports.mediaSetup = function (ogObject, options) {
ogObject.twitterPlayerStream)
.map(mediaMapperTwitterPlayer).sort(mediaSorter);

// Delete temporary fields
// Devare temporary fields
fields.filter(function (item) {
return item.multiple;
}).forEach(function (item) {
Expand Down Expand Up @@ -112,7 +112,7 @@ exports.mediaSetup = function (ogObject, options) {
return ogObject;
};

const mediaMapperTwitterImage = function (item) {
var mediaMapperTwitterImage = function (item) {
return {
url: item[0],
width: item[1],
Expand All @@ -121,7 +121,7 @@ const mediaMapperTwitterImage = function (item) {
};
};

const mediaMapperTwitterPlayer = function (item) {
var mediaMapperTwitterPlayer = function (item) {
return {
url: item[0],
width: item[1],
Expand All @@ -130,7 +130,7 @@ const mediaMapperTwitterPlayer = function (item) {
};
};

const mediaMapper = function (item) {
var mediaMapper = function (item) {
return {
url: item[0],
width: item[1],
Expand All @@ -139,21 +139,20 @@ const mediaMapper = function (item) {
};
};

const mediaSorter = function (a, b) {
var mediaSorter = function (a, b) {
if (!(a.url && b.url)) {
return 0;
}

var aRes = a.url.match(/\.(\w{2,5})$/),
aExt = (aRes && aRes[1].toLowerCase()) || null;
var bRes = b.url.match(/\.(\w{2,5})$/),
bExt = (bRes && bRes[1].toLowerCase()) || null;
var aRes = a.url.match(/\.(\w{2,5})$/);
var aExt = (aRes && aRes[1].toLowerCase()) || null;
var bRes = b.url.match(/\.(\w{2,5})$/);
var bExt = (bRes && bRes[1].toLowerCase()) || null;

if (aExt === 'gif' && bExt !== 'gif') {
return -1;
} else if (aExt !== 'gif' && bExt === 'gif') {
return 1;
} else {
return Math.max(b.width, b.height) - Math.max(a.width, a.height);
}
return Math.max(b.width, b.height) - Math.max(a.width, a.height);
};
Loading

0 comments on commit c02827f

Please sign in to comment.