-
Notifications
You must be signed in to change notification settings - Fork 2
feat: default APM phone dialing code from browser locale #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roshan-gorasia-cko
wants to merge
3
commits into
master
Choose a base branch
from
feat/apm-locale-default-dialing-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,5 +27,8 @@ jobs: | |
| - name: Lint | ||
| run: yarn lint | ||
|
|
||
| - name: Test | ||
| run: yarn test | ||
|
|
||
| - name: Build | ||
| run: yarn build | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, expect, it } from "vitest" | ||
| import { loadApmUtils, FakeNavigator } from "../support/loadNamespace" | ||
|
|
||
| function getBrowserRegion(navigator: FakeNavigator): string { | ||
| return loadApmUtils(navigator).getBrowserRegion() | ||
| } | ||
|
|
||
| type DialingCode = { region_code: string; value: string } | ||
|
|
||
| function getDefaultDialingCode( | ||
| dialingCodes: DialingCode[], | ||
| navigator: FakeNavigator = {}, | ||
| ): string { | ||
| return loadApmUtils(navigator).getDefaultDialingCode(dialingCodes) | ||
| } | ||
|
|
||
| describe("getBrowserRegion", () => { | ||
| it("extracts the region subtag from a language-region locale", () => { | ||
| expect(getBrowserRegion({ language: "en-GB" })).toBe("GB") | ||
| expect(getBrowserRegion({ language: "fr-CA" })).toBe("CA") | ||
| }) | ||
|
|
||
| it("returns an empty string when there is no region subtag", () => { | ||
| expect(getBrowserRegion({ language: "en" })).toBe("") | ||
| expect(getBrowserRegion({ language: "fr" })).toBe("") | ||
| }) | ||
|
|
||
| it("skips a script subtag and picks the region", () => { | ||
| expect(getBrowserRegion({ language: "zh-Hant-TW" })).toBe("TW") | ||
| }) | ||
|
|
||
| it("ignores non-ISO (numeric UN M49) regions", () => { | ||
| // "es-419" (Latin America) has no 2-letter ISO region to match against. | ||
| expect(getBrowserRegion({ language: "es-419" })).toBe("") | ||
| }) | ||
|
|
||
| it("uppercases the region regardless of source casing", () => { | ||
| expect(getBrowserRegion({ language: "en-gb" })).toBe("GB") | ||
| }) | ||
|
|
||
| it("prefers navigator.languages[0] over navigator.language", () => { | ||
| expect( | ||
| getBrowserRegion({ languages: ["en-GB", "fr-FR"], language: "de-DE" }), | ||
| ).toBe("GB") | ||
| }) | ||
|
|
||
| it("falls back to userLanguage when language is empty", () => { | ||
| expect(getBrowserRegion({ language: "", userLanguage: "en-US" })).toBe("US") | ||
| }) | ||
|
|
||
| it("returns an empty string when no locale is available", () => { | ||
| expect(getBrowserRegion({})).toBe("") | ||
| }) | ||
| }) | ||
|
|
||
| describe("getDefaultDialingCode", () => { | ||
| const codes: DialingCode[] = [ | ||
| { region_code: "AF", value: "+93" }, | ||
| { region_code: "GB", value: "+44" }, | ||
| { region_code: "US", value: "+1" }, | ||
| ] | ||
|
|
||
| it("picks the code whose region matches the browser locale", () => { | ||
| expect(getDefaultDialingCode(codes, { language: "en-GB" })).toBe("+44") | ||
| expect(getDefaultDialingCode(codes, { language: "en-US" })).toBe("+1") | ||
| }) | ||
|
|
||
| it("matches region codes case-insensitively", () => { | ||
| expect( | ||
| getDefaultDialingCode( | ||
| [{ region_code: "gb", value: "+44" }], | ||
| { language: "en-GB" }, | ||
| ), | ||
| ).toBe("+44") | ||
| }) | ||
|
|
||
| it("falls back to the first code when the locale has no region", () => { | ||
| expect(getDefaultDialingCode(codes, { language: "en" })).toBe("+93") | ||
| }) | ||
|
|
||
| it("falls back to the first code when no region matches", () => { | ||
| expect(getDefaultDialingCode(codes, { language: "fr-FR" })).toBe("+93") | ||
| }) | ||
|
|
||
| it("returns an empty string for an empty list", () => { | ||
| expect(getDefaultDialingCode([], { language: "en-GB" })).toBe("") | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { readFileSync } from "node:fs" | ||
| import { resolve } from "node:path" | ||
| import * as ts from "typescript" | ||
|
|
||
| /** | ||
| * The SDK sources use the TypeScript internal-namespace style | ||
| * (`module ProcessOut { export function ... }`) and are shipped as a single | ||
| * global bundle via `tsc --outFile`. That means they expose nothing to ES | ||
| * module `import`, so we cannot import the helpers directly. | ||
| * | ||
| * Instead, we transpile the real source file and evaluate it in an isolated | ||
| * function scope, returning the resulting `ProcessOut` namespace object. A | ||
| * `navigator` implementation is injected so locale-dependent helpers are | ||
| * deterministic regardless of the machine running the tests. | ||
| * | ||
| * This exercises the exact source that ships — no re-implementation, no | ||
| * test-only hooks baked into the production files. | ||
| */ | ||
|
|
||
| const compiledCache = new Map<string, string>() | ||
|
|
||
| function compile(relativePath: string): string { | ||
| const absolute = resolve(process.cwd(), relativePath) | ||
| if (!compiledCache.has(absolute)) { | ||
| const source = readFileSync(absolute, "utf8") | ||
| const { outputText } = ts.transpileModule(source, { | ||
| compilerOptions: { | ||
| module: ts.ModuleKind.CommonJS, | ||
| target: ts.ScriptTarget.ES2020, | ||
| }, | ||
| }) | ||
| compiledCache.set(absolute, outputText) | ||
| } | ||
| return compiledCache.get(absolute)! | ||
| } | ||
|
|
||
| export interface FakeNavigator { | ||
| language?: string | ||
| languages?: string[] | ||
| userLanguage?: string | ||
| } | ||
|
|
||
| /** | ||
| * Load `src/apm/utils.ts` and return its `ProcessOut` namespace, with the | ||
| * given `navigator` shim visible to the module's code. | ||
| */ | ||
| export function loadApmUtils(navigator: FakeNavigator = {}): Record<string, any> { | ||
| const js = compile("src/apm/utils.ts") | ||
| const moduleShim = { exports: {} as Record<string, any> } | ||
| const run = new Function( | ||
| "module", | ||
| "exports", | ||
| "navigator", | ||
| `${js}\nmodule.exports = ProcessOut;`, | ||
| ) | ||
| run(moduleShim, moduleShim.exports, navigator) | ||
| return moduleShim.exports | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { defineConfig } from "vitest/config" | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| include: ["test/**/*.test.ts"], | ||
| environment: "node", | ||
| }, | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.