Skip to content

Commit

Permalink
Merge teamStringToId and getTeam into IITC.utils.getTeamId
Browse files Browse the repository at this point in the history
  • Loading branch information
modos189 committed Nov 14, 2024
1 parent 74afad8 commit 6d50bad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion core/code/_deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ window.androidCopy = function () {
* @returns {number} The team ID the entity belongs to.
*/
window.getTeam = function (details) {
return IITC.utils.teamStringToId(details.team);
return IITC.utils.getTeamId(details.team);
};
16 changes: 10 additions & 6 deletions core/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,18 @@ const isPointInPolygon = (polygon, point) => {
};

/**
* Converts a team string to a team ID.
* Converts a team string or object to a team ID.
* Accepts either team string directly (e.g. "RESISTANCE", "R") or an object with team property.
* Returns TEAM_NONE if no match found.
*
* @memberof IITC.utils
* @function teamStringToId
* @param {string} teamStr - The team string to convert.
* @function getTeamId
* @param {(Object|string)} input - Input to convert to team ID
* @param {string} [input.team] - Team string when input is an object
* @returns {number} The team ID corresponding to the team string.
*/
const teamStringToId = (teamStr) => {
const getTeamId = (input) => {
const teamStr = typeof input === 'string' ? input : input?.team;
if (window.TEAM_CODENAMES.includes(teamStr)) {
return window.TEAM_CODENAMES.indexOf(teamStr);
}
Expand Down Expand Up @@ -515,7 +519,7 @@ IITC.utils = {
clampLatLng,
clampLatLngBounds,
isPointInPolygon,
teamStringToId,
getTeamId,
};

// Map of legacy function names to their new names (or the same name if not renamed)
Expand Down Expand Up @@ -543,7 +547,7 @@ const legacyFunctionMappings = {
clampLatLng: 'clampLatLng',
clampLatLngBounds: 'clampLatLngBounds',
pnpoly: 'isPointInPolygon',
teamStringToId: 'teamStringToId',
teamStringToId: 'getTeamId',
};

// Set up synchronization between `window` and `IITC.utils` with new names
Expand Down
86 changes: 69 additions & 17 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,31 +668,83 @@ describe('IITC.utils.clamp', () => {
});
});

describe('IITC.utils.teamStringToId', () => {
describe('IITC.utils.getTeamId', () => {
before(() => {
window.TEAM_CODENAMES = ['NEUTRAL', 'RESISTANCE', 'ENLIGHTENED', 'MACHINA'];
window.TEAM_CODES = ['N', 'R', 'E', 'M'];
window.TEAM_NONE = -1;
window.TEAM_NONE = 0;
});

it('should return the correct ID for a valid team name in TEAM_CODENAMES', () => {
expect(IITC.utils.teamStringToId('NEUTRAL')).to.equal(0);
expect(IITC.utils.teamStringToId('RESISTANCE')).to.equal(1);
expect(IITC.utils.teamStringToId('ENLIGHTENED')).to.equal(2);
expect(IITC.utils.teamStringToId('MACHINA')).to.equal(3);
describe('string input', () => {
it('should return correct ID for valid team names from TEAM_CODENAMES', () => {
expect(IITC.utils.getTeamId('NEUTRAL')).to.equal(0);
expect(IITC.utils.getTeamId('RESISTANCE')).to.equal(1);
expect(IITC.utils.getTeamId('ENLIGHTENED')).to.equal(2);
expect(IITC.utils.getTeamId('MACHINA')).to.equal(3);
});

it('should return correct ID for valid team codes from TEAM_CODES', () => {
expect(IITC.utils.getTeamId('N')).to.equal(0);
expect(IITC.utils.getTeamId('R')).to.equal(1);
expect(IITC.utils.getTeamId('E')).to.equal(2);
expect(IITC.utils.getTeamId('M')).to.equal(3);
});

it('should be case sensitive', () => {
expect(IITC.utils.getTeamId('resistance')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('Resistance')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('r')).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for invalid team names or codes', () => {
expect(IITC.utils.getTeamId('ALIENS')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('X')).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId('')).to.equal(window.TEAM_NONE);
});
});

it('should return the correct ID for a valid team code in TEAM_CODES', () => {
expect(IITC.utils.teamStringToId('N')).to.equal(0);
expect(IITC.utils.teamStringToId('R')).to.equal(1);
expect(IITC.utils.teamStringToId('E')).to.equal(2);
expect(IITC.utils.teamStringToId('M')).to.equal(3);
describe('object input', () => {
it('should return correct ID for objects with valid team names', () => {
expect(IITC.utils.getTeamId({ team: 'NEUTRAL' })).to.equal(0);
expect(IITC.utils.getTeamId({ team: 'RESISTANCE' })).to.equal(1);
expect(IITC.utils.getTeamId({ team: 'ENLIGHTENED' })).to.equal(2);
expect(IITC.utils.getTeamId({ team: 'MACHINA' })).to.equal(3);
});

it('should return correct ID for objects with valid team codes', () => {
expect(IITC.utils.getTeamId({ team: 'N' })).to.equal(0);
expect(IITC.utils.getTeamId({ team: 'R' })).to.equal(1);
expect(IITC.utils.getTeamId({ team: 'E' })).to.equal(2);
expect(IITC.utils.getTeamId({ team: 'M' })).to.equal(3);
});

it('should return TEAM_NONE for objects with invalid team property values', () => {
expect(IITC.utils.getTeamId({ team: 'ALIENS' })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: 'X' })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: '' })).to.equal(window.TEAM_NONE);
});
});

it('should return TEAM_NONE for an invalid team name or code', () => {
expect(IITC.utils.teamStringToId('ALIENS')).to.equal(window.TEAM_NONE);
expect(IITC.utils.teamStringToId('X')).to.equal(window.TEAM_NONE);
expect(IITC.utils.teamStringToId('')).to.equal(window.TEAM_NONE);
expect(IITC.utils.teamStringToId(null)).to.equal(window.TEAM_NONE);
describe('error handling', () => {
it('should return TEAM_NONE for null/undefined input', () => {
expect(IITC.utils.getTeamId(null)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId(undefined)).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for objects without team property', () => {
expect(IITC.utils.getTeamId({})).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ notTeam: 'RESISTANCE' })).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for objects with null/undefined team property', () => {
expect(IITC.utils.getTeamId({ team: null })).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId({ team: undefined })).to.equal(window.TEAM_NONE);
});

it('should return TEAM_NONE for non-string/non-object inputs', () => {
expect(IITC.utils.getTeamId(123)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId(true)).to.equal(window.TEAM_NONE);
expect(IITC.utils.getTeamId([])).to.equal(window.TEAM_NONE);
});
});
});

0 comments on commit 6d50bad

Please sign in to comment.