@@ -4,7 +4,6 @@ const fs = require('fs');
4
4
const _ = require ( 'lodash' ) ;
5
5
const Bluebird = require ( 'bluebird' ) ;
6
6
7
- const InvalidArgumentError = require ( './lib/errors' ) . InvalidArgumentError ;
8
7
const ApiRequestError = require ( './lib/errors' ) . ApiRequestError ;
9
8
const UrlMatchError = require ( './lib/errors' ) . UrlMatchError ;
10
9
@@ -19,35 +18,28 @@ const providers = requireProviders();
19
18
*/
20
19
21
20
function get ( matchUrls , callback ) {
22
-
23
21
return Bluebird . resolve ( matchUrls )
24
22
. then ( match )
25
- . then ( ( matches ) => {
26
-
27
- return matches . map ( ( match ) => {
28
-
23
+ . then ( matches => {
24
+ return matches . map ( match => {
29
25
const provider = providers [ match . providerName ] ;
30
26
return provider . fetch ( match . embedUrl ) ;
31
27
} ) ;
32
28
} )
33
29
. settle ( )
34
- . then ( ( results ) => {
35
-
30
+ . then ( results => {
36
31
// results is a PromiseInspection array
37
32
// this is reached once the operations are all done, regardless if
38
33
// they're successful or not.
39
34
40
35
const oEmbeds = [ ] ;
41
36
42
- results . forEach ( ( result ) => {
43
-
37
+ results . forEach ( result => {
44
38
if ( result . isFulfilled ( ) ) {
45
-
46
39
// Return the oEmbed information
47
40
oEmbeds . push ( result . value ( ) ) ;
48
41
49
42
} else if ( result . isRejected ( ) ) {
50
-
51
43
// TODO: Do something with 404s etc.
52
44
//result.reason()
53
45
}
@@ -67,15 +59,9 @@ function get(matchUrls, callback) {
67
59
*/
68
60
69
61
function match ( matchUrls , callback ) {
70
-
71
62
return Bluebird . resolve ( matchUrls )
72
63
. 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 ) ) )
79
65
. all ( )
80
66
. then ( mergeMatchResults )
81
67
. then ( sanitizeMatchResults )
@@ -90,11 +76,9 @@ function match(matchUrls, callback) {
90
76
*/
91
77
92
78
function matchOne ( matchUrl ) {
93
-
94
79
return Bluebird . resolve ( matchUrl )
95
- . then ( ( matchUrl ) => {
96
-
97
- return _ . map ( _ . keys ( providers ) , ( providerName ) => {
80
+ . then ( matchUrl => {
81
+ return _ . map ( _ . keys ( providers ) , providerName => {
98
82
return providers [ providerName ] . match ( matchUrl ) ;
99
83
} ) ;
100
84
} )
@@ -109,11 +93,9 @@ function matchOne(matchUrl) {
109
93
*/
110
94
111
95
function mergeMatchResults ( matchResults ) {
112
-
113
96
let result = [ ] ;
114
97
115
- matchResults . forEach ( ( item ) => {
116
-
98
+ matchResults . forEach ( item => {
117
99
if ( _ . isArray ( item ) ) {
118
100
result = _ . union ( result , item ) ;
119
101
}
@@ -130,11 +112,9 @@ function mergeMatchResults(matchResults) {
130
112
*/
131
113
132
114
function sanitizeMatchResults ( matchResults ) {
133
-
134
115
const result = [ ] ;
135
116
136
- matchResults . forEach ( ( matchResult ) => {
137
-
117
+ matchResults . forEach ( matchResult => {
138
118
if ( matchResult !== null &&
139
119
matchResult !== undefined &&
140
120
_ . where ( result , matchResult ) . length === 0 ) {
@@ -152,7 +132,6 @@ function sanitizeMatchResults(matchResults) {
152
132
*/
153
133
154
134
function requireProviders ( ) {
155
-
156
135
const providers = { } ;
157
136
const providerDir = __dirname + '/providers' ;
158
137
const providerFiles = fs . readdirSync ( providerDir ) ;
@@ -174,19 +153,17 @@ function requireProviders() {
174
153
*/
175
154
176
155
function sanitizeMatchUrls ( matchUrls ) {
177
-
178
156
if ( _ . isString ( matchUrls ) ) {
179
157
matchUrls = [ matchUrls ] ;
180
158
}
181
159
182
160
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' ) ;
184
162
}
185
163
186
164
for ( let matchUrl of matchUrls ) {
187
-
188
165
if ( ! _ . isString ( matchUrl ) ) {
189
- throw new InvalidArgumentError ( 'Invalid match URL - should be string' ) ;
166
+ throw new TypeError ( 'Invalid match URL - should be string' ) ;
190
167
}
191
168
}
192
169
@@ -207,6 +184,5 @@ function getProviders() {
207
184
module . exports . get = get ;
208
185
module . exports . match = match ;
209
186
module . exports . providers = getProviders ( ) ;
210
- module . exports . InvalidArgumentError = InvalidArgumentError ;
211
187
module . exports . ApiRequestError = ApiRequestError ;
212
188
module . exports . UrlMatchError = UrlMatchError ;
0 commit comments