Skip to content

Commit

Permalink
Support sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjcbrown committed Apr 10, 2017
1 parent c1ad4ae commit 9cb5a9c
Show file tree
Hide file tree
Showing 31 changed files with 7,459 additions and 1,774 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ The `options` parameter is optional, but is used to specify the type returned (e
Options support:
- `type` - "series" or "issue". Defaults to "issue".
- Use the `lofcg.types` helper for getting type values, eg. `lofcg.types.ISSUE`
- `publishers` - an array of publisher names to filter on. Default to no filtering.
- `publishers` - an array of publisher names to filter on. Defaults to no filtering.
- `sort` - "asc" (ascending) or "desc" (descending). Defaults to "asc".
- **Note:** Sorting is performed within the module on the response from the server (ie. not by the server itself) due to sorting bugs in the endpoints. However, the server provides additional sorting options depending on the list. Passing a value which is not one of the two supported will be delegated to the server (eg. providing "pulls" for Most Pulled on Pull List will have that order provided by the server).

```js
const lofcg = require('leagueofcomicgeeks');
const options = {
type: lofcg.types.SERIES,
publishers: ['Marvel Comics', 'DC Comics', 'Other']
publishers: ['Marvel Comics', 'DC Comics', 'Other'],
sort: 'desc'
}
lofcg.searchResults.get('detective', options, function (err, results) {
// ...
Expand Down Expand Up @@ -90,15 +93,14 @@ Methods on user lists:
## To do

- Tests
- [ ] Provide sorting to prevent test data changing
- [ ] Unit tests
- [ ] Jenkins integration
- [ ] Periodic run for integration tests
- [ ] PR run for unit tests

- Additional functionality
- [ ] Sorting - A-Z, Z-A
- [ ] Pull lists can have "most pulled"
- [x] Sorting - A-Z, Z-A
- [x] Pull lists can have "most pulled"
- [ ] Filtering - owned, not owned, read, not read
- [ ] New comics can have "only #1s"

Expand Down
17 changes: 12 additions & 5 deletions src/utils/extract-data-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const convertToISO8601Date = function (dateString) {
return date.format('YYYY-MM-DD');
};

const seriesExtractor = function (response) {
const sortList = function (list, sortBy = 'asc') {
if (sortBy === 'asc' || sortBy === 'desc') {
return _.orderBy(list, 'name', sortBy);
}
return list;
};

const seriesExtractor = function (response, options) {
const $ = cheerio.load(response.list);

const extractSeriesData = function () {
Expand All @@ -30,10 +37,10 @@ const seriesExtractor = function (response) {
};
};

return $('li').map(extractSeriesData).get();
return sortList($('li').map(extractSeriesData).get(), options.sort);
};

const issueExtractor = function (response) {
const issueExtractor = function (response, options) {
const $ = cheerio.load(response.list);

const extractIssueData = function () {
Expand Down Expand Up @@ -61,7 +68,7 @@ const issueExtractor = function (response) {
};
};

return $('li').map(extractIssueData).get();
return sortList($('li').map(extractIssueData).get(), options.sort);
};

const extractionHandler = {
Expand All @@ -71,5 +78,5 @@ const extractionHandler = {

module.exports = function (response, options) {
const handler = extractionHandler[options.type] || extractionHandler[config.defaultType];
return handler(response);
return handler(response, options);
};
2 changes: 1 addition & 1 deletion src/utils/list-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getList = function (userId, listId, parameters, options, callback) {
list_option: type,
user_id: userId,
view: viewType[type] || 'thumbs',
order: 'alpha-asc',
order: options.sort || 'alpha-asc',
publisher: getPublisherIds(options.publishers)
}, parameters);
const urlParameterString = queryString.stringify(urlParameters, { arrayFormat: 'bracket' });
Expand Down
13 changes: 13 additions & 0 deletions test/shared/collection/issues-list.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('lodash');
const allIssuesCollection = require('./test-data/all-issues-collection');
const filteredIssuesCollection = require('./test-data/filtered-issues-collection');
const sortedIssuesCollection = require('./test-data/sorted-issues-collection');

module.exports = function (lofcg) {
describe('get issues list', function () {
Expand Down Expand Up @@ -36,5 +37,17 @@ module.exports = function (lofcg) {
done();
});
});

it('should provide a sorted list of comics from a users collection', function (done) {
lofcg.collection.get(readonlyUserId, { sort: 'desc' }, (err, collection) => {
expect(err).toBeNull();
expect(collection.length).toBe(86);
expect(collection).toEqual(sortedIssuesCollection);
_.each(collection, (comic) => {
expect(comic).toBeAComicIssue();
});
done();
});
});
});
};
14 changes: 14 additions & 0 deletions test/shared/collection/series-list.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const _ = require('lodash');
const allSeriesCollection = require('./test-data/all-series-collection');
const filteredSeriesCollection = require('./test-data/filtered-series-collection');
const sortedSeriesCollection = require('./test-data/sorted-series-collection');

module.exports = function (lofcg) {
const options = { type: lofcg.types.SERIES };
const filteredOptions = _.extend({ publishers: ['Image Comics'] }, options);
const sortedOptions = _.extend({ sort: 'desc' }, options);

describe('get series list', function () {
it('should provide no comics in collection with an invalid user id', function (done) {
Expand Down Expand Up @@ -39,5 +41,17 @@ module.exports = function (lofcg) {
done();
});
});

it('should provide a sorted list of comics from a users collection', function (done) {
lofcg.collection.get(readonlyUserId, sortedOptions, (err, collection) => {
expect(err).toBeNull();
expect(collection.length).toBe(3);
expect(collection).toEqual(sortedSeriesCollection);
_.each(collection, (comic) => {
expect(comic).toBeAComicSeries();
});
done();
});
});
});
};
Loading

0 comments on commit 9cb5a9c

Please sign in to comment.