Finally finished giant ES6 refactor

This commit is contained in:
Jonathan Cremin 2015-08-20 23:22:57 +01:00
parent c6d48cc424
commit 03e2666958
39 changed files with 553 additions and 635 deletions

View file

@ -1,18 +1,19 @@
import {parse} from 'url';
import { parse } from 'url';
import querystring from 'querystring';
import request from 'superagent';
import 'superagent-bluebird-promise';
import { match as urlMatch } from './url';
module.exports.id = 'itunes';
export let id = 'itunes';
const apiRoot = 'https://itunes.apple.com';
module.exports.match = require('./url').match;
export const match = urlMatch;
module.exports.parseUrl = function* (url) {
let parsed = parse(url);
let matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
let query = querystring.parse(parsed.query);
export function* parseUrl(url) {
const parsed = parse(url);
const matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
const query = querystring.parse(parsed.query);
if (matches) {
let type = 'album';
@ -23,11 +24,11 @@ module.exports.parseUrl = function* (url) {
}
return yield module.exports.lookupId(id, type, matches[1] || 'us');
} else {
return Promise.reject(new Error());
throw new Error();
}
};
module.exports.lookupId = function* (id, type, cc) {
export function* lookupId(id, type, cc) {
if (String(id).match(/^[a-z]{2}/)) {
cc = id.substr(0, 2);
id = id.substr(2);
@ -38,13 +39,13 @@ module.exports.lookupId = function* (id, type, cc) {
path = '/' + cc + path;
}
let response = yield request.get(apiRoot + path);
const response = yield request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
let error = new Error('Not Found');
const error = new Error('Not Found');
error.status = 404;
return Promise.reject(error);
throw error;
} else {
result = result.results[0];
@ -70,13 +71,13 @@ module.exports.lookupId = function* (id, type, cc) {
};
}
return Promise.resolve(item);
return item;
}
};
module.exports.search = function* (data) {
export function* search(data) {
let query, album, entity;
let type = data.type;
const type = data.type;
if (type === 'album') {
query = data.artist.name + ' ' + data.name;
@ -88,27 +89,27 @@ module.exports.search = function* (data) {
entity = 'musicTrack';
}
let path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
let response = yield request.get(apiRoot + path);
const path = '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
const response = yield request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results[0]) {
let matches = album.match(/^[^\(\[]+/);
const matches = album.match(/^[^\(\[]+/);
if (matches && matches[0] && matches[0] !== album) {
let cleanedData = JSON.parse(JSON.stringify(data));
const cleanedData = JSON.parse(JSON.stringify(data));
if (type === 'album') {
cleanedData.name = matches[0].trim();
} else if (type === 'track') {
cleanedData.album.name = matches[0].trim();
}
return yield module.exports.search(cleanedData);
return yield search(cleanedData);
} else {
return Promise.resolve({service: 'itunes'});
return {service: 'itunes'};
}
} else {
result = result.results[0];
let item = {
const item = {
service: 'itunes',
type: type,
id: 'us' + result.collectionId,
@ -129,6 +130,6 @@ module.exports.search = function* (data) {
name: result.collectionName
};
}
return Promise.resolve(item);
return item;
}
};

View file

@ -1,16 +1,15 @@
"use strict";
var parse = require('url').parse;
var querystring = require('querystring');
import { parse } from 'url';
import querystring from 'querystring';
module.exports.match = function(url, type) {
var parsed = parse(url);
export function* match(url, type) {
const parsed = parse(url);
if (!parsed.host.match(/itunes.apple\.com$/)) {
return false;
}
var matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
var query = querystring.parse(parsed.query);
const matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
const query = querystring.parse(parsed.query);
return !!matches[3];
};