Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import { getCountries, getCountryCallingCode } from 'libphonenumber-js';
import {
type CountryCode,
getCountries,
getCountryCallingCode,
} from 'libphonenumber-js';

const ALL_COUNTRIES_CODE = getCountries();
// Precompute a map from calling code to country codes for O(1) lookups
const CALLING_CODE_TO_COUNTRIES = new Map<string, CountryCode[]>();

export const getCountryCodesForCallingCode = (callingCode: string) => {
for (const country of getCountries()) {
const callingCode = getCountryCallingCode(country);

const existing = CALLING_CODE_TO_COUNTRIES.get(callingCode);

if (existing) {
existing.push(country);
} else {
CALLING_CODE_TO_COUNTRIES.set(callingCode, [country]);
}
}

export const getCountryCodesForCallingCode = (
callingCode: string,
): CountryCode[] => {
const cleanCallingCode = callingCode.startsWith('+')
? callingCode.slice(1)
: callingCode;

return ALL_COUNTRIES_CODE.filter((country) => {
const countryCallingCode = getCountryCallingCode(country);

return countryCallingCode === cleanCallingCode;
});
return CALLING_CODE_TO_COUNTRIES.get(cleanCallingCode) ?? [];
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Returning the cached array exposes internal mutable state; callers can mutate it and corrupt future lookups. Return a copy to preserve previous behavior of returning a fresh array.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/twenty-shared/src/utils/validation/phones-value/getCountryCodesForCallingCode.ts, line 29:

<comment>Returning the cached array exposes internal mutable state; callers can mutate it and corrupt future lookups. Return a copy to preserve previous behavior of returning a fresh array.</comment>

<file context>
@@ -1,15 +1,30 @@
-
-    return countryCallingCode === cleanCallingCode;
-  });
+  return CALLING_CODE_TO_COUNTRIES.get(cleanCallingCode) ?? [];
 };
</file context>
Suggested change
return CALLING_CODE_TO_COUNTRIES.get(cleanCallingCode) ?? [];
return [...(CALLING_CODE_TO_COUNTRIES.get(cleanCallingCode) ?? [])];
Fix with Cubic

};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type CountryCode, getCountries } from 'libphonenumber-js';

const ALL_COUNTRIES_CODE = getCountries();
const ALL_COUNTRIES_CODE_SET = new Set<string>(getCountries());

export const isValidCountryCode = (input: string): input is CountryCode => {
return ALL_COUNTRIES_CODE.includes(input as unknown as CountryCode);
return ALL_COUNTRIES_CODE_SET.has(input);
};