Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds in Babel for es6 features #9

Merged
merged 3 commits into from
Dec 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
/coverage
lib/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- '0.12'
- '4'
branches:
only:
- master
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"description": "A Node.js helper library for swapi.co",
"main": "index.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha test --reporter=nyan",
"prepublish": "./node_modules/nsp/bin/nsp check",
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha",
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register",
"compile": "babel -d lib/ src/",
"prepublish": "./node_modules/nsp/bin/nsp check && npm run compile",
"coverage": "babel-node ./node_modules/.bin/isparta cover _mocha",
"coveralls": "npm run coverage -- --report lcovonly && cat ./coverage/lcov.info | coveralls"
},
"author": {
Expand All @@ -23,8 +24,11 @@
],
"license": "BSD",
"devDependencies": {
"babel-cli": "^6.2.0",
"babel-core": "^6.2.1",
"babel-preset-es2015": "^6.1.18",
"coveralls": "^2.11.4",
"istanbul": "^0.4.1",
"isparta": "^4.0.0",
"mocha": "^2.3.4",
"mocha-lcov-reporter": "^1.0.0",
"nock": "^3.3.2",
Expand Down
40 changes: 20 additions & 20 deletions lib/swapi-node.js → src/swapi-node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var request = require('request'),
util = require('./util');

var baseUrl = 'http://swapi.co/api/';
const BASE_URL = 'http://swapi.co/api/';

function sendRequest(options) {
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if (error || response.statusCode > 399) {
return reject({error: response.statusCode});
}
Expand All @@ -18,9 +18,9 @@ function sendRequest(options) {
return reject({error: 'JSON parse error'});
}

['nextPage', 'previousPage'].forEach(function (value) {
jsonBody[value] = (function () {
return function () {
['nextPage', 'previousPage'].forEach((value) => {
jsonBody[value] = (() => {
return () => {
var url = jsonBody[(value.indexOf('next') > -1) ? 'next' : 'previous'];
if (url) {
return makeRequest(url);
Expand All @@ -31,26 +31,26 @@ function sendRequest(options) {
})();
});

Object.keys(jsonBody).forEach(function (value) {
Object.keys(jsonBody).forEach((value) => {
if (typeof jsonBody[value] !== 'function') {
jsonBody['get' + util.capitaliseFirstLetter(value)] = (function () {
return function () {
jsonBody['get' + util.capitaliseFirstLetter(value)] = (() => {
return () => {
if (!Array.isArray(jsonBody[value])) {
if (jsonBody[value].indexOf(baseUrl) > -1) {
if (jsonBody[value].indexOf(BASE_URL) > -1) {
return makeRequest(jsonBody[value]);
}

return Promise.resolve(jsonBody[value]);
}

var p = jsonBody[value].map(function (val) {
if (val.indexOf(baseUrl) > -1) {
var p = jsonBody[value].map((val) => {
if (val.indexOf(BASE_URL) > -1) {
return makeRequest(val);
}
return Promise.resolve(val);
});

return Promise.all(p).then(function (v) {
return Promise.all(p).then((v) => {
return v;
});
};
Expand All @@ -65,7 +65,7 @@ function sendRequest(options) {

function makeRequest(url) {
var options = {
url: (url.indexOf(baseUrl) > -1) ? url : baseUrl + url,
url: (url.indexOf(BASE_URL) > -1) ? url : BASE_URL + url,
headers: {
'User-Agent': 'swapi-node',
'SWAPI-Node-Version': require('../package.json').version,
Expand All @@ -92,26 +92,26 @@ function parseOptions(options) {
}

module.exports = {
get: function (url) {
get (url) {
return makeRequest(url);
},
getPerson: function (options) {
getPerson (options) {
var opts = parseOptions(options);
return makeRequest('people' + (opts.id ? '/' + opts.id : '/'));
},
getStarship: function (options) {
getStarship (options) {
var opts = parseOptions(options);
return makeRequest('starship' + (opts.id ? '/' + opts.id : '/'));
},
getVehicle: function (options) {
getVehicle (options) {
var opts = parseOptions(options);
return makeRequest('vehicles' + (opts.id ? '/' + opts.id : '/'));
},
getFilm: function (options) {
getFilm (options) {
var opts = parseOptions(options);
return makeRequest('films' + (opts.id ? '/' + opts.id : '/'));
},
getSpecies: function (options) {
getSpecies (options) {
var opts = parseOptions(options);
return makeRequest('species' + (opts.id ? '/' + opts.id : '/'));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util.js → src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
capitaliseFirstLetter: function (string) {
capitaliseFirstLetter (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
};
Loading