Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
feat: improve active-calls callerId parser
Browse files Browse the repository at this point in the history
  • Loading branch information
adroste committed Dec 29, 2021
1 parent fc05f51 commit 4af2f0d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 20 deletions.
18 changes: 10 additions & 8 deletions build/src/func/active-calls.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/func/active-calls.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions build/src/func/active-calls.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/func/active-calls.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion src/func/active-calls.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkActiveCalls, offActiveCallsChange, onActiveCallsChange } from './active-calls';
import { checkActiveCalls, createCallerInfoFromCallerId, offActiveCallsChange, onActiveCallsChange } from './active-calls';
import { closeDb, initDb } from '../database';

import { connectTo3cxApi } from '../api/connection';
Expand All @@ -25,6 +25,44 @@ describe('active-calls', () => {
});
});

describe('active-calls callerId parsing', () => {
it('should parse format: +4912341234', () => {
const info = createCallerInfoFromCallerId('+4912341234');
expect(info.phoneNumber).toBe('+4912341234');
expect(info.displayName).toBeUndefined();
});

it('should parse format: Nick Sample (001231234)', () => {
const info = createCallerInfoFromCallerId('Nick Sample (001231234)');
expect(info.phoneNumber).toBe('001231234');
expect(info.displayName).toBe('Nick Sample');
});

it('should parse format: (100)', () => {
const info = createCallerInfoFromCallerId('(100)');
expect(info.phoneNumber).toBe('100');
expect(info.displayName).toBeUndefined();
});

it('should prefer number in parenthesis and parse format: 112 113 (100)', () => {
const info = createCallerInfoFromCallerId('112 113 (100)');
expect(info.phoneNumber).toBe('100');
expect(info.displayName).toBe('112 113');
});

it('should parse format: 10 My Extension', () => {
const info = createCallerInfoFromCallerId('10 My Extension');
expect(info.phoneNumber).toBe('10');
expect(info.displayName).toBe('My Extension');
});

it('should parse format: 10 My Fav Nr Is 55', () => {
const info = createCallerInfoFromCallerId('10 My Fav Nr Is 55');
expect(info.phoneNumber).toBe('10');
expect(info.displayName).toBe('My Fav Nr Is 55');
});
});

afterAll(async () => {
await closeDb();
});
21 changes: 12 additions & 9 deletions src/func/active-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,32 @@ export interface ActiveCall {
}

function getPhoneNumberFromCallerId(callerId: string) {
// format can be for instance: "Name name (+123456789)", "(123495)", "+4359090132"
const test = /(^|\()([+]?\d{2,})($|\)$)/;
const match = test.exec(callerId);
if (match)
return match[2];
return undefined;
// format can be for instance: "Name name (+123456789)", "(123495)", "+4359090132", "1234 Name name"
// minimum 2 digits as 3CX extensions/dns can have 2,3 or 4 numbers
const test1 = /\(([+]?\d{2,})\)$/; // check for number in parenthesis
const test2 = /^([+]?\d{2,})/; // check for number at the beginning
const match1 = test1.exec(callerId);
const match2 = test2.exec(callerId);
// match in parenthesis has priority
return match1?.[1] || match2?.[1] || undefined;
}

function stripPhoneNumberFromCallerId(callerId: string, phoneNumber: string) {
return callerId
.replace(phoneNumber, '')
.replace(' ()', '');
.replace('()', '')
.trim();
}

function createCallerInfoFromCallerId(callerId: string): CallerInfo {
export function createCallerInfoFromCallerId(callerId: string): CallerInfo {
const phoneNumber = getPhoneNumberFromCallerId(callerId);
if (!phoneNumber)
return { displayName: callerId };

const entry = resolveCaller(phoneNumber);
const callerIdWithoutNr = stripPhoneNumberFromCallerId(callerId, phoneNumber);
return {
displayName: entry?.displayName || callerIdWithoutNr,
displayName: entry?.displayName || callerIdWithoutNr || undefined,
phoneNumber,
phoneBookId: entry?.id || undefined,
};
Expand Down

0 comments on commit 4af2f0d

Please sign in to comment.