Skip to content

Commit 641041c

Browse files
committed
Refactoring - WIP
1 parent 6c53136 commit 641041c

26 files changed

+173
-400
lines changed

.eslintrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"plugins": [
3+
"react"
4+
],
5+
"extends": "airbnb",
6+
"ecmaFeatures": {
7+
"jsx": true,
8+
"modules": true
9+
},
10+
"env": {
11+
"es6": true,
12+
"browser": true,
13+
"node": true,
14+
"mocha": true
15+
},
16+
"rules": {
17+
"strict": [0, "global"],
18+
"padded-blocks": [2, "never"],
19+
"arrow-body-style": [0, "as-needed"],
20+
"no-use-before-define": [2, "nofunc"],
21+
"react/prefer-es6-class": [2, "never"]
22+
}
23+
}

.jscs.json

-90
This file was deleted.

.jshintignore

-1
This file was deleted.

.jshintrc

-24
This file was deleted.

circle.yml

-8
This file was deleted.

main.js index.js

+11-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const fs = require('fs');
44
const _ = require('lodash');
55
const Bluebird = require('bluebird');
66

7-
const InvalidArgumentError = require('./lib/errors').InvalidArgumentError;
87
const ApiRequestError = require('./lib/errors').ApiRequestError;
98
const UrlMatchError = require('./lib/errors').UrlMatchError;
109

@@ -19,35 +18,28 @@ const providers = requireProviders();
1918
*/
2019

2120
function get(matchUrls, callback) {
22-
2321
return Bluebird.resolve(matchUrls)
2422
.then(match)
25-
.then((matches) => {
26-
27-
return matches.map((match) => {
28-
23+
.then(matches => {
24+
return matches.map(match => {
2925
const provider = providers[match.providerName];
3026
return provider.fetch(match.embedUrl);
3127
});
3228
})
3329
.settle()
34-
.then((results) => {
35-
30+
.then(results => {
3631
// results is a PromiseInspection array
3732
// this is reached once the operations are all done, regardless if
3833
// they're successful or not.
3934

4035
const oEmbeds = [];
4136

42-
results.forEach((result) => {
43-
37+
results.forEach(result => {
4438
if (result.isFulfilled()) {
45-
4639
// Return the oEmbed information
4740
oEmbeds.push(result.value());
4841

4942
} else if (result.isRejected()) {
50-
5143
// TODO: Do something with 404s etc.
5244
//result.reason()
5345
}
@@ -67,15 +59,9 @@ function get(matchUrls, callback) {
6759
*/
6860

6961
function match(matchUrls, callback) {
70-
7162
return Bluebird.resolve(matchUrls)
7263
.then(sanitizeMatchUrls)
73-
.then((matchUrls) => {
74-
75-
return _.map(matchUrls, function(matchUrl) {
76-
return matchOne(matchUrl);
77-
});
78-
})
64+
.then(matchUrls => matchUrls.map(matchUrl => matchOne(matchUrl)))
7965
.all()
8066
.then(mergeMatchResults)
8167
.then(sanitizeMatchResults)
@@ -90,11 +76,9 @@ function match(matchUrls, callback) {
9076
*/
9177

9278
function matchOne(matchUrl) {
93-
9479
return Bluebird.resolve(matchUrl)
95-
.then((matchUrl) => {
96-
97-
return _.map(_.keys(providers), (providerName) => {
80+
.then(matchUrl => {
81+
return _.map(_.keys(providers), providerName => {
9882
return providers[providerName].match(matchUrl);
9983
});
10084
})
@@ -109,11 +93,9 @@ function matchOne(matchUrl) {
10993
*/
11094

11195
function mergeMatchResults(matchResults) {
112-
11396
let result = [];
11497

115-
matchResults.forEach((item) => {
116-
98+
matchResults.forEach(item => {
11799
if (_.isArray(item)) {
118100
result = _.union(result, item);
119101
}
@@ -130,11 +112,9 @@ function mergeMatchResults(matchResults) {
130112
*/
131113

132114
function sanitizeMatchResults(matchResults) {
133-
134115
const result = [];
135116

136-
matchResults.forEach((matchResult) => {
137-
117+
matchResults.forEach(matchResult => {
138118
if (matchResult !== null &&
139119
matchResult !== undefined &&
140120
_.where(result, matchResult).length === 0) {
@@ -152,7 +132,6 @@ function sanitizeMatchResults(matchResults) {
152132
*/
153133

154134
function requireProviders() {
155-
156135
const providers = {};
157136
const providerDir = __dirname + '/providers';
158137
const providerFiles = fs.readdirSync(providerDir);
@@ -174,19 +153,17 @@ function requireProviders() {
174153
*/
175154

176155
function sanitizeMatchUrls(matchUrls) {
177-
178156
if (_.isString(matchUrls)) {
179157
matchUrls = [matchUrls];
180158
}
181159

182160
if (!_.isArray(matchUrls)) {
183-
throw new InvalidArgumentError('Invalid match URLs - should be string or array');
161+
throw new TypeError('Invalid match URLs - should be string or array');
184162
}
185163

186164
for (let matchUrl of matchUrls) {
187-
188165
if (!_.isString(matchUrl)) {
189-
throw new InvalidArgumentError('Invalid match URL - should be string');
166+
throw new TypeError('Invalid match URL - should be string');
190167
}
191168
}
192169

@@ -207,6 +184,5 @@ function getProviders() {
207184
module.exports.get = get;
208185
module.exports.match = match;
209186
module.exports.providers = getProviders();
210-
module.exports.InvalidArgumentError = InvalidArgumentError;
211187
module.exports.ApiRequestError = ApiRequestError;
212188
module.exports.UrlMatchError = UrlMatchError;

jsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=759670
3+
// for the documentation about the jsconfig.json format
4+
"compilerOptions": {
5+
"target": "es6",
6+
"module": "commonjs",
7+
"allowSyntheticDefaultImports": true
8+
},
9+
"exclude": [
10+
"node_modules",
11+
"bower_components",
12+
"jspm_packages",
13+
"tmp",
14+
"temp"
15+
]
16+
}

lib/errors.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
const createError = require('custom-error-generator');
44

5-
const InvalidArgumentError = createError('InvalidArgumentError');
65
const ApiRequestError = createError('ApiRequestError');
76
const UrlMatchError = createError('UrlMatchError');
87

98
module.exports = {
10-
InvalidArgumentError: InvalidArgumentError,
11-
ApiRequestError: ApiRequestError,
12-
UrlMatchError: UrlMatchError
9+
ApiRequestError,
10+
UrlMatchError,
1311
};

0 commit comments

Comments
 (0)