From 8cbfbfd6c9c8407fec16084a81e66abf771d50db Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Mon, 11 Nov 2024 22:03:10 +0500 Subject: [PATCH 1/4] Move `window.teamStringToId` to `IITC.utils.teamStringToId` The `window.getTeam` function is marked as deprecated --- core/code/_deprecated.js | 15 ++++++++++++++- core/code/entity_info.js | 34 ---------------------------------- core/code/utils.js | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 35 deletions(-) delete mode 100644 core/code/entity_info.js diff --git a/core/code/_deprecated.js b/core/code/_deprecated.js index 2304ef78e..4f85f0c50 100644 --- a/core/code/_deprecated.js +++ b/core/code/_deprecated.js @@ -1,4 +1,4 @@ -/* global L -- eslint */ +/* global IITC, L -- eslint */ /** * @file This file contains functions that are not use by IITC itself @@ -174,3 +174,16 @@ window.findPortalLatLng = function (guid) { window.androidCopy = function () { return true; // i.e. execute other actions }; + +/** + * Given the entity detail data, returns the team the entity belongs to. + * Uses TEAM_* enum values. + * + * @deprecated + * @function getTeam + * @param {Object} details - The details hash of an entity. + * @returns {number} The team ID the entity belongs to. + */ +window.getTeam = function (details) { + return IITC.utils.teamStringToId(details.team); +}; diff --git a/core/code/entity_info.js b/core/code/entity_info.js deleted file mode 100644 index 60023798f..000000000 --- a/core/code/entity_info.js +++ /dev/null @@ -1,34 +0,0 @@ -/* exported setup --eslint */ - -/** - * Entity Details Tools - * Functions to extract useful data from entity details, such as portals, links, and fields. - * @module entity_info - */ - -/** - * Given the entity detail data, returns the team the entity belongs to. - * Uses TEAM_* enum values. - * - * @function getTeam - * @param {Object} details - The details hash of an entity. - * @returns {number} The team ID the entity belongs to. - */ -window.getTeam = function (details) { - return window.teamStringToId(details.team); -}; - -/** - * Converts a team string to a team ID. - * - * @function teamStringToId - * @param {string} teamStr - The team string to convert. - * @returns {number} The team ID corresponding to the team string. - */ -window.teamStringToId = function (teamStr) { - var teamIndex = window.TEAM_CODENAMES.indexOf(teamStr); - if (teamIndex >= 0) return teamIndex; - teamIndex = window.TEAM_CODES.indexOf(teamStr); - if (teamIndex >= 0) return teamIndex; - return window.TEAM_NONE; -}; diff --git a/core/code/utils.js b/core/code/utils.js index 311a065a3..d2f10ad44 100644 --- a/core/code/utils.js +++ b/core/code/utils.js @@ -472,6 +472,22 @@ const isPointInPolygon = (polygon, point) => { return !!inside; }; +/** + * Converts a team string to a team ID. + * + * @memberof IITC.utils + * @function teamStringToId + * @param {string} teamStr - The team string to convert. + * @returns {number} The team ID corresponding to the team string. + */ +const teamStringToId = function (teamStr) { + var teamIndex = window.TEAM_CODENAMES.indexOf(teamStr); + if (teamIndex >= 0) return teamIndex; + teamIndex = window.TEAM_CODES.indexOf(teamStr); + if (teamIndex >= 0) return teamIndex; + return window.TEAM_NONE; +}; + IITC.utils = { getURLParam, getCookie, @@ -497,6 +513,7 @@ IITC.utils = { clampLatLng, clampLatLngBounds, isPointInPolygon, + teamStringToId, }; // Map of legacy function names to their new names (or the same name if not renamed) @@ -524,6 +541,7 @@ const legacyFunctionMappings = { clampLatLng: 'clampLatLng', clampLatLngBounds: 'clampLatLngBounds', pnpoly: 'isPointInPolygon', + teamStringToId: 'teamStringToId', }; // Set up synchronization between `window` and `IITC.utils` with new names From 82fddfb7305947336839b1d64c1b9f8c037610ca Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Mon, 11 Nov 2024 22:07:42 +0500 Subject: [PATCH 2/4] Refactoring of `IITC.utils.teamStringToId` --- core/code/utils.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/code/utils.js b/core/code/utils.js index d2f10ad44..0ad0cef05 100644 --- a/core/code/utils.js +++ b/core/code/utils.js @@ -480,11 +480,13 @@ const isPointInPolygon = (polygon, point) => { * @param {string} teamStr - The team string to convert. * @returns {number} The team ID corresponding to the team string. */ -const teamStringToId = function (teamStr) { - var teamIndex = window.TEAM_CODENAMES.indexOf(teamStr); - if (teamIndex >= 0) return teamIndex; - teamIndex = window.TEAM_CODES.indexOf(teamStr); - if (teamIndex >= 0) return teamIndex; +const teamStringToId = (teamStr) => { + if (window.TEAM_CODENAMES.includes(teamStr)) { + return window.TEAM_CODENAMES.indexOf(teamStr); + } + if (window.TEAM_CODES.includes(teamStr)) { + return window.TEAM_CODES.indexOf(teamStr); + } return window.TEAM_NONE; }; From 74afad885482b9feaf14f47bd8a51b920bb58949 Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Mon, 11 Nov 2024 22:20:00 +0500 Subject: [PATCH 3/4] Tests for `IITC.utils.teamStringToId` --- test/utils.spec.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/utils.spec.js b/test/utils.spec.js index 278462ec0..31b31b14d 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -1,4 +1,4 @@ -import { describe, it, beforeEach } from 'mocha'; +import { describe, it, before, beforeEach } from 'mocha'; import { expect } from 'chai'; /* global IITC */ @@ -667,3 +667,32 @@ describe('IITC.utils.clamp', () => { expect(result).to.equal(-5); }); }); + +describe('IITC.utils.teamStringToId', () => { + before(() => { + window.TEAM_CODENAMES = ['NEUTRAL', 'RESISTANCE', 'ENLIGHTENED', 'MACHINA']; + window.TEAM_CODES = ['N', 'R', 'E', 'M']; + window.TEAM_NONE = -1; + }); + + 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); + }); + + 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); + }); + + 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); + }); +}); From 6d50badbfa3a4c76f9aec46a2f0ced105cdcc3b1 Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Thu, 14 Nov 2024 16:44:54 +0500 Subject: [PATCH 4/4] Merge teamStringToId and getTeam into IITC.utils.getTeamId --- core/code/_deprecated.js | 2 +- core/code/utils.js | 16 +++++--- test/utils.spec.js | 86 ++++++++++++++++++++++++++++++++-------- 3 files changed, 80 insertions(+), 24 deletions(-) diff --git a/core/code/_deprecated.js b/core/code/_deprecated.js index 4f85f0c50..218e9c196 100644 --- a/core/code/_deprecated.js +++ b/core/code/_deprecated.js @@ -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); }; diff --git a/core/code/utils.js b/core/code/utils.js index 0ad0cef05..4e61d76dc 100644 --- a/core/code/utils.js +++ b/core/code/utils.js @@ -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); } @@ -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) @@ -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 diff --git a/test/utils.spec.js b/test/utils.spec.js index 31b31b14d..f42962169 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -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); + }); }); });