Skip to content

Commit

Permalink
Better handle abbreviations and rendering crew names
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwalley committed Nov 13, 2016
1 parent c2ca48b commit 0d547ef
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-bumps-chart",
"version": "0.2.3",
"version": "0.2.5",
"description": "Draw bumps charts.",
"keywords": [
"d3",
Expand Down
4 changes: 2 additions & 2 deletions src/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export default function () {
.datum(d => ({ name: d.name, set: d.set, gender: d.gender, value: d.values[d.values.length === finishLabelIndex ? finishLabelIndex - 1 : finishLabelIndex] }))
.attr('x', 10)
.attr('dy', '.35em')
.text(d => renderName(d.name))
.text(d => renderName(d.name, d.set))
.attr('transform', d =>
`translate(${x(finishLabelPosition + 5 * (numYearsToView - 1))},${y(d.value.pos)})`)
.style('cursor', 'pointer');
Expand Down Expand Up @@ -373,7 +373,7 @@ export default function () {
.attr('x', -10)
.attr('dy', '.35em')
.attr('text-anchor', 'end')
.text(d => renderName(d.name))
.text(d => renderName(d.name, d.set))
.attr('transform', d => `translate(${x(startLabelPosition)},${y(d.value.pos)})`)
.style('cursor', 'pointer');

Expand Down
86 changes: 62 additions & 24 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { findKey, uniq, uniqBy } from 'lodash';
import { csvParse } from 'd3-dsv';
import { min, max } from 'd3-array';

const abbrevCamCollege = {
export const abbrevCamCollege = {
'A': 'Addenbrooke\'s',
'AR': 'Anglia Ruskin',
'Ca': 'Caius',
Expand Down Expand Up @@ -44,7 +44,7 @@ const abbrevCamCollege = {
'W': 'Wolfson'
};

const abbrevOxCollege = {
export const abbrevOxCollege = {
'B': 'Balliol',
'Br': 'Brasenose',
'Ch': 'Christ Church',
Expand Down Expand Up @@ -83,7 +83,7 @@ const abbrevOxCollege = {
};

// eslint-disable-next-line no-unused-vars
const abbrevCamTown = {
export const abbrevCamTown = {
'A': 'Addenbrooke\'s',
'CB': 'Camb Blue',
'CV': 'Camb Veterans',
Expand Down Expand Up @@ -144,66 +144,104 @@ const abbrevCamTown = {
'X': 'X-Press'
};

const abbrev = Object.assign({}, abbrevCamTown, abbrevCamCollege, abbrevOxCollege);

const roman = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX'];

export function abbreviate(event) {
for (let div = 0; div < event.divisions.length; div++) {
for (let pos = 0; pos < event.divisions[div].length; pos++) {
event.divisions[div][pos] = abbreviateCrew(event.divisions[div][pos]);
event.divisions[div][pos] = abbreviateCrew(event.divisions[div][pos], event.set);
}
}

for (let div = 0; div < event.finish.length; div++) {
for (let pos = 0; pos < event.finish[div].length; pos++) {
event.finish[div][pos] = abbreviateCrew(event.finish[div][pos]);
event.finish[div][pos] = abbreviateCrew(event.finish[div][pos], event.set);
}
}

return event;
}

function abbreviateCrew(crew) {
function abbreviateCrew(crew, set) {
const name = crew.replace(/[0-9]+$/, '').trim();
const num = +crew.substring(name.length);

let abbrev;

switch (set) {
case 'Lent Bumps':
case 'May Bumps':
abbrev = abbrevCamCollege;
break;
case 'Torpids':
case 'Summer Eights':
abbrev = abbrevOxCollege;
break;
case 'Town Bumps':
abbrev = abbrevCamTown;
break;
default:
throw 'Unrecognised set: ' + set;
}

if (findKey(abbrev, club => club === name) !== undefined) {
return findKey(abbrev, club => club === name) + (num > 1 ? num : '');
} else {
return crew;
}
}

export function renderName(name) {
export function renderName(name, set) {
// College crews are stored as an abbrevation and we replace the number with Roman numerals
const sh = name.replace(/[0-9]/, '');

if (abbrevCamCollege.hasOwnProperty(sh)) {
const num = name.substring(sh.length);
name = abbrevCamCollege[sh];
let abbrev;
let type;

if (num.length > 0) {
name = name + ' ' + roman[+num - 1];
}

return name;
switch (set) {
case 'Lent Bumps':
case 'May Bumps':
abbrev = abbrevCamCollege;
type = 'college';
break;
case 'Torpids':
case 'Summer Eights':
abbrev = abbrevOxCollege;
type = 'college';
break;
case 'Town Bumps':
abbrev = abbrevCamTown;
type = 'town';
break;
default:
throw 'Unrecognised set: ' + set;
}

if (abbrevOxCollege.hasOwnProperty(sh)) {
if (abbrev.hasOwnProperty(sh)) {
const num = name.substring(sh.length);
name = abbrevOxCollege[sh];
name = abbrev[sh];

if (num.length > 0) {
if (type === 'college' && num.length > 0) {
name = name + ' ' + roman[+num - 1];
} else if (type === 'town' && num.length > 0 && (+num) > 1) {
name = name + ' ' + (+num);
}

return name;
}
} else {
// First boats should not have a number rendered
if (type === 'college') {
const num = name.substring(sh.length);

// Town first boats (ending ' 1') do not have the 1 displayed
if (name.substring(name.length - 2) === ' 1') {
return name.substring(0, name.length - 2);
if (num.length > 0) {
name = sh.trim() + (((+num) > 1) ? ' ' + roman[+num - 1] : '');
}

return name;
} else if (type === 'town' && name.substring(name.length - 2) === ' 1') {
return name.substring(0, name.length - 2);
}
}

return name;
Expand Down
4 changes: 2 additions & 2 deletions test/abbreviate-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var tape = require("tape");
var bumps = require('../');

tape("Transform data correctly.", function (test) {
tape("Abbreviate club names correctly.", function (test) {
var data = {
completed: [[true, true, true], [true, true, true], [true, true, true], [true, true, true]],
days: 4,
Expand Down Expand Up @@ -40,4 +40,4 @@ r rrr rrrrr',

test.deepEqual(actual, expected);
test.end();
})
});
60 changes: 55 additions & 5 deletions test/renderName-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
var tape = require("tape");
var bumps = require('../');

tape("Expand college abbreviation.", function(test) {
tape("Expand Cambridge college abbreviation.", function(test) {
var name = 'Ca';

var expected = 'Caius';
var actual = bumps.renderName(name);
var actual = bumps.renderName(name, 'Lent Bumps');

test.equal(actual, expected);
test.end();
})

tape("Expand Cambridge college abbreviation with number > 1.", function(test) {
var name = 'Ca2';

var expected = 'Caius II';
var actual = bumps.renderName(name, 'Lent Bumps');

test.equal(actual, expected);
test.end();
})

tape("Expand Oxford college abbreviation.", function(test) {
var name = 'O';

var expected = 'Oriel';
var actual = bumps.renderName(name, 'Summer Eights');

test.equal(actual, expected);
test.end();
})

tape("Expand Oxford college abbreviation with number > 1.", function(test) {
var name = 'O7';

var expected = 'Oriel VII';
var actual = bumps.renderName(name, 'Summer Eights');

test.equal(actual, expected);
test.end();
})

tape("Expand town abbreviation.", function(test) {
var name = 'Cy';

var expected = 'City';
var actual = bumps.renderName(name, 'Town Bumps');

test.equal(actual, expected);
test.end();
Expand All @@ -15,7 +55,17 @@ tape("Use Roman numerals for college crews.", function(test) {
var name = 'SS3';

var expected = 'Sidney Sussex III';
var actual = bumps.renderName(name);
var actual = bumps.renderName(name, 'Lent Bumps');

test.equal(actual, expected);
test.end();
})

tape("College first boats should not have a number.", function(test) {
var name = 'Sidney Sussex 1';

var expected = 'Sidney Sussex';
var actual = bumps.renderName(name, 'Lent Bumps');

test.equal(actual, expected);
test.end();
Expand All @@ -25,7 +75,7 @@ tape("Town first boats should not have a number.", function(test) {
var name = 'City 1';

var expected = 'City';
var actual = bumps.renderName(name);
var actual = bumps.renderName(name, 'Town Bumps');

test.equal(actual, expected);
test.end();
Expand All @@ -35,7 +85,7 @@ tape("Town boats > 10 should work.", function(test) {
var name = 'City 11';

var expected = 'City 11';
var actual = bumps.renderName(name);
var actual = bumps.renderName(name, 'Town Bumps');

test.equal(actual, expected);
test.end();
Expand Down

0 comments on commit 0d547ef

Please sign in to comment.