Skip to content

Commit f9dff9d

Browse files
author
John Doherty
committed
added eslint
1 parent 0fb5220 commit f9dff9d

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
test

.eslintrc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"extends": "airbnb-base",
3+
"env": {
4+
"es6": true,
5+
"jasmine": true,
6+
"node": true,
7+
"browser": false
8+
},
9+
"globals": {
10+
"spyOn": true,
11+
"window": true,
12+
"document": true,
13+
"app": true,
14+
"helpers": true
15+
},
16+
"rules": {
17+
"brace-style": ["error", "stroustrup"],
18+
"comma-dangle": ["error", "never"],
19+
"func-names": 0,
20+
"indent": ["error", 4, { "SwitchCase": 1 }],
21+
"max-len": [2, 180, 4, {
22+
"ignoreUrls": true,
23+
"ignoreComments": false
24+
}],
25+
"new-cap": ["error", {"capIsNewExceptions": ["Router", "ObjectId", "DEBUG"], "properties": false}],
26+
"no-underscore-dangle": 0,
27+
"no-unused-vars": ["warn"],
28+
"no-use-before-define": ["error", { "functions": false }],
29+
"no-var": ["off"],
30+
"one-var": ["off"],
31+
"vars-on-top": ["off"],
32+
"no-param-reassign": ["off"],
33+
"padded-blocks": 0,
34+
"prefer-template": ["off"],
35+
"prefer-arrow-callback": ["off"],
36+
"require-jsdoc": ["warn", {
37+
"require": {
38+
"FunctionDeclaration": true,
39+
"MethodDefinition": true,
40+
"ClassDeclaration": true
41+
}
42+
}],
43+
"object-shorthand": ["error", "never"],
44+
"space-before-function-paren": "off",
45+
"strict": "off",
46+
"valid-jsdoc": ["error"]
47+
}
48+
}

example/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Server() {
2929
});
3030

3131
// start the server
32-
app.listen(8080, function(){
32+
app.listen(8080, function() {
3333
console.log('Server running... Visit http://localhost:8080 in your browser');
3434
});
3535
}

lib/node-iframe-replacement.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,112 @@
33
var isUrl = require('is-url'), // module use to verify the sourceUrl is an actual URL
44
cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors
55
request = require('request-promise'), // promise based request object
6-
NodeCache = require( "node-cache" ), // auto expiring in memory caching collection
6+
NodeCache = require('node-cache'), // auto expiring in memory caching collection
77
cache = new NodeCache({ stdTTL: 300 }); // cache source HTML for 5 minutes, after which we fetch a new version
88

9-
module.exports = function(req, res, next){
9+
module.exports = function(req, res, next) {
1010

11-
// add res.merge to response object to allow us to fetch template url or use html template string
12-
res.merge = function(view, model, callback){
13-
14-
if (!model.sourceUrl){
11+
// add res.merge to response object to allow us to fetch template url or use html template string
12+
res.merge = function(view, model, callback) {
13+
14+
if (!model.sourceUrl) {
1515
// no .sourceUrl, therefore process this as normal
1616
res.render(view, model, callback);
17-
}
17+
}
1818
else {
1919

2020
// if no template selector provided, use body tag
2121
var sourcePlaceholder = model.sourcePlaceholder || 'body';
22-
22+
2323
// resolve the template, either url to jquery object or html
24-
resolveTemplate(model.sourceUrl).then(function($template){
25-
24+
resolveTemplate(model.sourceUrl).then(function($template) {
25+
2626
// if a transform method is provided, execute
27-
if (model.transform){
27+
if (model.transform) {
2828
// pass a jquery version of the html along with a copy of the model
2929
model.transform($template, model);
3030
}
31-
31+
3232
// convert view into html to be inserted
3333
res.render(view, model, function(err, html) {
3434
if (err) next(err);
35-
35+
3636
// convert view into jquery object
3737
var $view = cheerio.load(html);
38-
38+
3939
// if the view contains a head, append its contents to the original
4040
var $viewHead = $view('head');
41-
if ($viewHead && $viewHead.length>0){
41+
if ($viewHead && $viewHead.length > 0) {
4242
// append meta, link, script and noscript to head
4343
$template('head').append($viewHead.html());
4444
}
45-
46-
// if the view has a body, use its contents otherwise use the entire content
45+
46+
// if the view has a body, use its contents otherwise use the entire content
4747
var $viewBody = $view('body');
4848

49-
if ($viewBody && $viewBody.length>0){
49+
if ($viewBody && $viewBody.length > 0) {
5050
// inject content into selector or body
5151
$template(sourcePlaceholder).html($viewBody.html());
5252
}
5353
else {
5454
// no body tag in view, insert all content
5555
$template(sourcePlaceholder).html($view.html());
5656
}
57-
57+
5858
// return merged content
5959
res.status(200).send($template.html());
6060
});
6161
})
62-
.catch(function(err){
62+
.catch(function(err) {
6363
// request failed, inform the user
6464
res.status(500).send(err).end();
6565
});
6666
}
6767
};
68-
68+
6969
next();
70-
}
70+
};
7171

7272
/**
73-
* Converts source url to $ object
73+
* @description Converts source url to $ object
7474
* @param {string} sourceUrl - url to external html content
75+
* @returns {Promise} returns promise of html source from sourceUrl
7576
*/
76-
function resolveTemplate(sourceUrl){
77-
78-
return new Promise(function(resolve, reject){
79-
77+
function resolveTemplate(sourceUrl) {
78+
79+
return new Promise(function(resolve, reject) {
80+
8081
// if its a url and we have the contents in cache
81-
if (isUrl(sourceUrl) && cache.get(sourceUrl)){
82-
82+
if (isUrl(sourceUrl) && cache.get(sourceUrl)) {
83+
8384
// get source html from cache
8485
var html = cache.get(sourceUrl);
85-
86+
8687
// covert html into jquery object
8788
var $ = cheerio.load(html);
88-
89+
8990
// return source as a jquery style object
9091
resolve($);
9192
}
92-
else if (isUrl(sourceUrl)){
93-
93+
else if (isUrl(sourceUrl)) {
94+
9495
// request the source url
9596
return request({ uri: sourceUrl }).then(function(html){
96-
97+
9798
// convert html into jquery style object so we can use selectors
9899
var $ = cheerio.load(html);
99-
100+
100101
// insert base tag to ensure links/scripts/styles load correctly
101102
$('head').prepend('<base href="' + sourceUrl + '">');
102-
103+
103104
// cache result as HTML so we dont have to keep getting it for future requests and it remains clean
104105
cache.set(sourceUrl, $.html());
105-
106+
106107
// resolve with jquery object containing content
107108
resolve($);
108109
})
109110
.catch(function(err) {
110-
111+
111112
// request failed
112113
reject('Unable to retrieve ' + sourceUrl);
113114
});
@@ -117,4 +118,4 @@ function resolveTemplate(sourceUrl){
117118
resolve(sourceUrl);
118119
}
119120
});
120-
};
121+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-iframe-replacement",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "NodeJS + Express replacement for the HTML iframe",
55
"main": "index.js",
66
"scripts": {
@@ -40,6 +40,9 @@
4040
"request-promise": "^3.0.0"
4141
},
4242
"devDependencies": {
43+
"eslint": "^3.12.2",
44+
"eslint-config-airbnb-base": "^11.0.0",
45+
"eslint-plugin-import": "^2.2.0",
4346
"jasmine-node": "^1.14.5",
4447
"nock": "^9.0.2",
4548
"supertest": "^2.0.1"

0 commit comments

Comments
 (0)