Skip to content

Commit c78530f

Browse files
author
Hans Kristian Flaatten
committed
Merge branch 'refactor/eslint'
2 parents 3131381 + 203f524 commit c78530f

File tree

4 files changed

+98
-67
lines changed

4 files changed

+98
-67
lines changed

.eslintrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "airbnb/base",
3+
"env": {
4+
"mocha": true
5+
},
6+
"rules": {
7+
"strict": [0, "never"]
8+
}
9+
}

index.js

+61-39
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,108 @@
1+
'use strict';
2+
3+
const eachAsync = require('each-async');
4+
15
module.exports.conf = {
26
API_KEY: process.env.NTB_API_KEY,
37
API_ENV: process.env.NTB_API_ENV || 'api',
4-
USER_AGENT: process.env.NTB_USER_AGENT || require('./package.json').version
8+
USER_AGENT: process.env.NTB_USER_AGENT || require('./package.json').version,
59
};
610

7-
module.exports._requestDefaults = function() {
11+
module.exports._requestDefaults = function reqestDefaults() {
812
return require('request').defaults({
913
baseUrl: 'http://' + module.exports.conf.API_ENV + '.nasjonalturbase.no/',
1014
headers: {
11-
'user-agent': module.exports.conf.USER_AGENT
15+
'user-agent': module.exports.conf.USER_AGENT,
1216
},
1317
json: true,
1418
qs: {
15-
api_key: module.exports.conf.API_KEY
16-
}
19+
api_key: module.exports.conf.API_KEY,
20+
},
1721
});
1822
};
1923

20-
var request = module.exports._requestDefaults();
21-
var eachAsync = require('each-async');
24+
let request = module.exports._requestDefaults();
2225

2326
[
2427
'aktiviteter',
2528
'bilder',
2629
'grupper',
2730
'områder',
2831
'steder',
29-
'turer'
30-
].forEach(function(type) {
31-
module.exports[type] = function(params, callback) {
32-
if (!callback) { return request.get({url: encodeURIComponent(type), qs: params}); }
33-
request.get({url: encodeURIComponent(type), qs: params}, callback);
32+
'turer',
33+
].forEach(function typeForEach(type) {
34+
module.exports[type] = function typeAll(params, callback) {
35+
if (!callback) { return request.get({ url: encodeURIComponent(type), qs: params }); }
36+
request.get({ url: encodeURIComponent(type), qs: params }, callback);
3437
};
3538

36-
module.exports[type].each = function(params, callback, done) {
37-
params.skip = params.skip || 0;
38-
params.limit = params.limit || 50;
39+
module.exports[type].each = function typeEach(params, callback, done) {
40+
const query = params;
41+
42+
query.skip = query.skip || 0;
43+
query.limit = query.limit || 50;
3944

40-
module.exports[type](params, function(err, res, body) {
41-
eachAsync(body.documents, callback, function(err) {
42-
if (err) { return done(err); }
45+
module.exports[type](query, function typeCb(typeErr, res, body) {
46+
if (typeErr) { return done(typeErr); }
4347

44-
if (body.documents < params.limit) {
48+
eachAsync(body.documents, callback, function eachAsyncDone(eachErr) {
49+
if (eachErr) { return done(eachErr); }
50+
51+
if (body.documents < query.limit) {
4552
return done(null);
4653
}
4754

48-
params.skip += params.limit;
49-
module.exports[type].each(params, callback, done);
55+
query.skip += query.limit;
56+
module.exports[type].each(query, callback, done);
5057
});
5158
});
5259
};
5360

54-
module.exports[type].post = function(data, callback) {
55-
if (!callback) { return request.post({url: encodeURIComponent(type), body: data}); }
56-
request.post({url: encodeURIComponent(type), body: data}, callback);
61+
module.exports[type].post = function typePost(data, callback) {
62+
if (!callback) {
63+
return request.post({ url: encodeURIComponent(type), body: data });
64+
}
65+
66+
request.post({ url: encodeURIComponent(type), body: data }, callback);
5767
};
5868

59-
module.exports[type].get = function(id, callback) {
60-
if (!callback) { return request.get({url: encodeURIComponent(type) + '/' + id}); }
61-
request.get({url: encodeURIComponent(type) + '/' + id}, callback);
69+
module.exports[type].get = function typeGet(id, callback) {
70+
if (!callback) {
71+
return request.get({ url: encodeURIComponent(type) + '/' + id });
72+
}
73+
74+
request.get({ url: encodeURIComponent(type) + '/' + id }, callback);
6275
};
6376

64-
module.exports[type].delete = function(id, callback) {
65-
if (!callback) { return request.del({url: encodeURIComponent(type) + '/' + id}); }
66-
request.del({url: encodeURIComponent(type) + '/' + id}, callback);
77+
module.exports[type].delete = function typeDelete(id, callback) {
78+
if (!callback) {
79+
return request.del({ url: encodeURIComponent(type) + '/' + id });
80+
}
81+
82+
request.del({ url: encodeURIComponent(type) + '/' + id }, callback);
6783
};
6884

69-
module.exports[type].put = function(id, data, callback) {
70-
if (!callback) { return request.put({url: encodeURIComponent(type) + '/' + id, body: data}); }
71-
request.put({url: encodeURIComponent(type) + '/' + id, body: data}, callback);
85+
module.exports[type].put = function typePut(id, data, callback) {
86+
if (!callback) {
87+
return request.put({ url: encodeURIComponent(type) + '/' + id, body: data });
88+
}
89+
90+
request.put({ url: encodeURIComponent(type) + '/' + id, body: data }, callback);
7291
};
7392

74-
module.exports[type].patch = function(id, data, callback) {
75-
if (!callback) { return request.patch({url: encodeURIComponent(type) + '/' + id, body: data}); }
76-
request.patch({url: encodeURIComponent(type) + '/' + id, body: data}, callback);
93+
module.exports[type].patch = function typePatch(id, data, callback) {
94+
if (!callback) {
95+
return request.patch({ url: encodeURIComponent(type) + '/' + id, body: data });
96+
}
97+
98+
request.patch({ url: encodeURIComponent(type) + '/' + id, body: data }, callback);
7799
};
78100
});
79101

80-
module.exports.configure = function(obj) {
81-
for (var key in obj) {
102+
module.exports.configure = function configure(obj) {
103+
Object.keys(obj).forEach(function objKeysForEach(key) {
82104
module.exports.conf[key] = obj[key];
83-
}
105+
});
84106

85107
// Generate a new request object
86108
request = module.exports._requestDefaults();

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Node.JS-client for Nasjonal Turbase",
55
"main": "index.js",
66
"scripts": {
7-
"lint": "eslint index.js test.js examples/",
7+
"lint": "eslint index.js test.js",
88
"test": "mocha -R tap test.js",
99
"watch": "mocha -w -R progress test.js"
1010
},

test.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
1-
var assert = require('assert');
2-
var turbasen = require('.');
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const turbasen = require('.');
35

46
process.env.NODE_ENV = 'test';
57

6-
describe('conf', function() {
7-
it('sets default config from environment', function() {
8+
describe('conf', function describe() {
9+
it('sets default config from environment', function it() {
810
assert.equal(turbasen.conf.API_KEY, process.env.NTB_API_KEY);
911
assert.equal(turbasen.conf.API_ENV, process.env.NTB_API_ENV);
1012
assert.equal(turbasen.conf.USER_AGENT, process.env.NTB_USER_AGENT);
1113
});
1214
});
1315

14-
describe('configure()', function() {
15-
afterEach(function() {
16+
describe('configure()', function describe() {
17+
afterEach(function afterEach() {
1618
turbasen.configure({
1719
API_KEY: process.env.NTB_API_KEY,
1820
API_ENV: process.env.NTB_API_ENV,
19-
USER_AGENT: process.env.NTB_USER_AGENT
21+
USER_AGENT: process.env.NTB_USER_AGENT,
2022
});
2123
});
2224

23-
it('updates conf variables', function() {
25+
it('updates conf variables', function it() {
2426
turbasen.configure({
2527
API_KEY: 'foo',
2628
API_ENV: 'bar',
27-
USER_AGENT: 'baz'
29+
USER_AGENT: 'baz',
2830
});
2931

3032
assert.equal(turbasen.conf.API_KEY, 'foo');
3133
assert.equal(turbasen.conf.API_ENV, 'bar');
3234
assert.equal(turbasen.conf.USER_AGENT, 'baz');
3335
});
3436

35-
it.skip('regenares request object', function() {
36-
37-
});
37+
it('regenares request object');
3838
});
3939

40-
describe('_requestDefaults()', function() {
41-
it('returns request object', function() {
40+
describe('_requestDefaults()', function describe() {
41+
it('returns request object', function it() {
4242
assert.equal(typeof turbasen._requestDefaults(), 'function');
4343
});
4444
});
4545

46-
describe('api', function() {
47-
it('exposes data types functions', function() {
46+
describe('api', function describe() {
47+
it('exposes data types functions', function it() {
4848
[
4949
'aktiviteter',
5050
'bilder',
5151
'grupper',
5252
'områder',
5353
'steder',
54-
'turer'
55-
].forEach(function(type) {
54+
'turer',
55+
].forEach(function forEach(type) {
5656
assert.equal(typeof turbasen[type], 'function');
5757
assert.equal(typeof turbasen[type].each, 'function');
5858
assert.equal(typeof turbasen[type].post, 'function');
@@ -63,19 +63,19 @@ describe('api', function() {
6363
});
6464
});
6565

66-
it('exposes each document itterator function', function(done) {
66+
it('exposes each document itterator function', function it(done) {
6767
this.timeout(10000);
6868

69-
var counter = 0;
70-
var opts = {
69+
let counter = 0;
70+
const opts = {
7171
tags: 'Bretur',
72-
limit: 10
72+
limit: 10,
7373
};
7474

75-
turbasen.turer.each(opts, function(item, index, next) {
75+
turbasen.turer.each(opts, function eachItem(item, index, next) {
7676
counter++;
7777
process.nextTick(next);
78-
}, function(err) {
78+
}, function eachDone(err) {
7979
assert.ifError(err);
8080
assert.equal(counter, 46);
8181

@@ -84,9 +84,9 @@ describe('api', function() {
8484
});
8585
});
8686

87-
describe('near.js', function() {
88-
it('returns cabins near coornidate', function(done) {
89-
require('./examples/near').on('data', function(data) {
87+
describe('near.js', function describe() {
88+
it('returns cabins near coornidate', function it(done) {
89+
require('./examples/near').on('data', function on(data) {
9090
assert.equal(data.documents[0].navn, 'Selhamar');
9191
assert.equal(data.documents[1].navn, 'Åsedalen');
9292
assert.equal(data.documents[2].navn, 'Torvedalshytta');

0 commit comments

Comments
 (0)