Skip to content

Instantly share code, notes, and snippets.

@erineland
Created July 5, 2019 10:50
Show Gist options
  • Save erineland/c027b4016cceeb31233cb980e92bb76c to your computer and use it in GitHub Desktop.
Save erineland/c027b4016cceeb31233cb980e92bb76c to your computer and use it in GitHub Desktop.

A simple utility (node module) that can be used to order a list of objects that have both name and rank, as well as finding the average rank.

See tests for examples like the one below:

const rankingTool = require('../src/object-ranking-tool');

const rankObjects = [
    {
        name: 'The Battle of The Bastards',
        rank: 4
    },
    {
        name: 'The Battle of Winterfell',
        rank: 5
    },
    {
        name: 'The Battle of Blackwater Bay',
        rank: 3
    },
];

const orderedRanks = rankingTool.orderByRank(rankObjects); // Now in order!
const averageRank = rankingTool.findAverageRank(rankObjects); // It's 4
const rankingTool = require('../src/object-ranking-tool');
const rankObjects = [
{
name: 'The Battle of The Bastards',
rank: 4
},
{
name: 'The Battle of Winterfell',
rank: 5
},
{
name: 'The Battle of Blackwater Bay',
rank: 3
},
];
describe('Object Ranking Tool', () => {
describe('Ordering the object ranks', () => {
describe('When an array of objects is not passed in', () => {
it('Should throw an error with a meaningful error message', () => {
try {
const orderedRanks = rankingTool.orderByRank();
} catch (parameterError) {
expect(parameterError.message).toBe('Please supply a valid array of objects with names and ranks');
}
});
});
describe('When a valid array of objects is passed in', () => {
describe('When no sort order is defined, it sorts by ascending by default', () => {
it('Should return that array in ranked order', () => {
const orderedRanks = rankingTool.orderByRank(rankObjects);
expect(orderedRanks[0].name).toBe('The Battle of Blackwater Bay');
});
})
describe('When the sort order is passed in', () => {
it('Should return that array in ranked order', () => {
const orderedDescRanks = rankingTool.orderByRank(rankObjects, 'desc');
expect(orderedDescRanks[0].name).toBe('The Battle of Winterfell');
});
});
});
});
describe('Finding the average rank', () => {
describe('When an array of objects is not passed in', () => {
it('Should throw an error with a meaningful error message', () => {
try {
const averageRank = rankingTool.findAverageRank();
} catch (parameterError) {
expect(parameterError.message).toBe('Please supply a valid array of objects with names and ranks');
}
});
});
describe('When a valid array of objects is passed in', () => {
it('Returns the average rank of the array of ranked objects', () => {
const averageRank = rankingTool.findAverageRank(rankObjects);
expect(averageRank).toBe(4);
});
});
});
});
module.exports.orderByRank = (objectsWithRanks, sortOrder) => {
if (!objectsWithRanks) {
throw new Error('Please supply a valid array of objects with names and ranks');
}
if (sortOrder === 'desc') {
objectsWithRanks.sort((firstElement, nextElement) => {
return nextElement.rank - firstElement.rank;
});
} else {
objectsWithRanks.sort((firstElement, nextElement) => {
return firstElement.rank - nextElement.rank;
});
}
return objectsWithRanks;
}
module.exports.findAverageRank = objectsWithRanks => {
if (!objectsWithRanks) {
throw new Error('Please supply a valid array of objects with names and ranks');
}
let sumRank = 0;
objectsWithRanks.forEach(currentObject => sumRank += currentObject.rank);
const average = sumRank / objectsWithRanks.length;
return average;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment