Skip to content

Commit

Permalink
fix(Sort Method): Fix possibility to get errors on sorted object
Browse files Browse the repository at this point in the history
Replace the words saved in an object by saving them in an array

BREAKING CHANGE: getSorted method now returns an array, not an object. Please read  the
documentation.

#4
  • Loading branch information
proustibat committed Oct 13, 2017
1 parent 4019e9b commit 3c77748
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ options | Object | Settings of the instance (read-only)

Property | Parameters | Default | Description
-------- | ---------- | ------- | -----------
getSorted | String: '*desc*', '*asc*' | 'desc' | Returns stats sorted by order descendant or ascendant
getSorted | String: '*desc*', '*asc*' | 'desc' | Returns an array with objects sorted by order descendant or ascendant, each index of the array is an object as following : `{word:'three', number: '3'}`

## Examples

Expand Down
30 changes: 14 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,9 @@ Occurences.prototype = {
/**
* Returns words occurrences sorted by ascendant/descendant order
* @param String: 'asc', 'desc'
* @returns {{}}
* @returns {null|Array}
*/
getSorted: function(order) {

// If no argument, orders by descendant by default
order = typeof order === 'undefined' ? 'desc' : order;
getSorted: function(order = 'desc') {

// Be sure the argument is in lowercase
order = order.toLowerCase();
Expand All @@ -191,29 +188,30 @@ Occurences.prototype = {
return this._sortedDesc;
}

// Sort sorted objects
this._sortedAsc = this._sort(order, this._stats);
this._sortedDesc = this._sort(order, this._stats);
// Save sorted object for both orders
this._sortedAsc = this._sort('asc', this._stats);
this._sortedDesc = this._sort('desc', this._stats);

// Returns the requested sorted object
return (order === 'asc') ? this._sortedAsc : this._sortedDesc;
},


_sort:function(order, stats) {
// TODO: REFACTO
let keysSorted = Object.keys(stats).sort((a,b)=>{
// Create array object for each word with its value and its occurrences
let statsArray = Object.keys(stats).map( key => {
return { value: key, number: stats[key] };
});

// Sort array by number value of its each object, depending on order in parameter
let result = statsArray.sort((a,b)=>{
if(order === 'asc') {
return stats[a]-stats[b];
return a.number - b.number;
}
else {
return stats[b] - stats[a];
return b.number - a.number;
}
});
let result = {};
keysSorted.forEach((key)=>{
result[key] = stats[key];
});
return result;
}
};
Expand Down
26 changes: 21 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,39 @@ describe('getSorted', () => {
it('works with no argument', () => {
let instance = new Occurrences("Not connected to power. Power is it good or bad. What is power? Dunno what power is but I know what it's not.");
let sorted = instance.getSorted();
chai.expect(sorted).to.be.an('object');
chai.expect(sorted).to.be.an('array');
});

it('works with asc argument', () => {
let instance = new Occurrences("Not connected to power. Power is it good or bad. What is power? Dunno what power is but I know what it's not.");
let sorted = instance.getSorted('asc');
let sorted2 = instance.getSorted('asc');
chai.expect(sorted).to.be.an('object');
chai.expect(sorted2).to.be.an('object');
chai.expect(sorted).to.be.an('array');
chai.expect(sorted2).to.be.an('array');
});

it('works with desc argument', () => {
let instance = new Occurrences("Not connected to power. Power is it good or bad. What is power? Dunno what power is but I know what it's not.");
let sorted = instance.getSorted('desc');
let sorted2 = instance.getSorted('desc');
chai.expect(sorted).to.be.an('object');
chai.expect(sorted2).to.be.an('object');
chai.expect(sorted).to.be.an('array');
chai.expect(sorted2).to.be.an('array');
});

it('returns an array correctly sorted by ascending', () => {
let instance = new Occurrences("three three four four one two two three four four");
let sorted = instance.getSorted('asc');
let sorted2 = instance.getSorted('asc');
chai.expect(sorted).to.eql([{value:'one', number:1}, {value:'two', number:2}, {value:'three', number:3}, {value:'four', number:4}]);
chai.expect(sorted2).to.eql([{value:'one', number:1}, {value:'two', number:2}, {value:'three', number:3}, {value:'four', number:4}]);
});

it('returns an array correctly sorted by descending', () => {
let instance = new Occurrences("three three four four one two two three four four");
let sorted = instance.getSorted('desc');
let sorted2 = instance.getSorted('desc');
chai.expect(sorted).to.eql([{value:'four', number:4}, {value:'three', number:3}, {value:'two', number:2}, {value:'one', number:1}]);
chai.expect(sorted2).to.eql([{value:'four', number:4}, {value:'three', number:3}, {value:'two', number:2}, {value:'one', number:1}]);
});
});

0 comments on commit 3c77748

Please sign in to comment.