Nitpick: psl types (#12925)
Close https://github.com/twentyhq/twenty/issues/12917
This commit is contained in:
@ -83,6 +83,7 @@
|
||||
"@types/lodash.uniqby": "^4.7.9",
|
||||
"@types/lodash.upperfirst": "^4.3.7",
|
||||
"@types/openid-client": "^3.7.0",
|
||||
"@types/psl": "^1.1.3",
|
||||
"@types/react": "^18.2.39",
|
||||
"@types/unzipper": "^0",
|
||||
"rimraf": "^5.0.5",
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
import psl, { ParsedDomain } from 'psl';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
export const isParsedDomain = (
|
||||
result: ReturnType<typeof psl.parse>,
|
||||
): result is ParsedDomain =>
|
||||
!isDefined(result.error) &&
|
||||
Object.prototype.hasOwnProperty.call(result, 'sld');
|
||||
@ -0,0 +1,60 @@
|
||||
import { EachTestingContext } from 'twenty-shared/testing';
|
||||
|
||||
import { getCompanyNameFromDomainName } from 'src/modules/contact-creation-manager/utils/get-company-name-from-domain-name.util';
|
||||
|
||||
type GetCompanyNameFromDomainNameTestCase = EachTestingContext<{
|
||||
input: string;
|
||||
expected: string;
|
||||
}>;
|
||||
|
||||
describe('getCompanyNameFromDomainName', () => {
|
||||
const testCases: GetCompanyNameFromDomainNameTestCase[] = [
|
||||
{
|
||||
title: 'should extract and capitalize company name from simple domain',
|
||||
context: {
|
||||
input: 'twenty.dev',
|
||||
expected: 'Twenty',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should extract and capitalize company name from subdomain',
|
||||
context: {
|
||||
input: 'app.twenty.dev',
|
||||
expected: 'Twenty',
|
||||
},
|
||||
},
|
||||
{
|
||||
title:
|
||||
'should extract and capitalize company name from multiple subdomains',
|
||||
context: {
|
||||
input: 'test.app.twenty.dev',
|
||||
expected: 'Twenty',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle domain with multiple parts',
|
||||
context: {
|
||||
input: 'twenty.co.uk',
|
||||
expected: 'Twenty',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle empty string',
|
||||
context: {
|
||||
input: '',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle invalid domain',
|
||||
context: {
|
||||
input: 'not-a-valid-domain',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
test.each(testCases)('$title', ({ context: { input, expected } }) => {
|
||||
expect(getCompanyNameFromDomainName(input)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,73 @@
|
||||
import { EachTestingContext } from 'twenty-shared/testing';
|
||||
|
||||
import { getDomainNameFromHandle } from 'src/modules/contact-creation-manager/utils/get-domain-name-from-handle.util';
|
||||
|
||||
type GetDomainNameFromHandleTestCase = EachTestingContext<{
|
||||
input: string;
|
||||
expected: string;
|
||||
}>;
|
||||
|
||||
describe('getDomainNameFromHandle', () => {
|
||||
const testCases: GetDomainNameFromHandleTestCase[] = [
|
||||
{
|
||||
title: 'should extract domain from email handle',
|
||||
context: {
|
||||
input: 'user@twenty.dev',
|
||||
expected: 'twenty.dev',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should extract domain from email handle with subdomain',
|
||||
context: {
|
||||
input: 'user@app.twenty.dev',
|
||||
expected: 'twenty.dev',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should extract domain from email handle with multiple subdomains',
|
||||
context: {
|
||||
input: 'user@test.app.twenty.dev',
|
||||
expected: 'twenty.dev',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle domain with multiple parts',
|
||||
context: {
|
||||
input: 'user@twenty.co.uk',
|
||||
expected: 'twenty.co.uk',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle empty string',
|
||||
context: {
|
||||
input: '',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle string without @ symbol',
|
||||
context: {
|
||||
input: 'not-an-email',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle undefined handle part after @',
|
||||
context: {
|
||||
input: 'user@',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'should handle invalid domain',
|
||||
context: {
|
||||
input: 'user@not-a-valid-domain',
|
||||
expected: '',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
test.each(testCases)('$title', ({ context: { input, expected } }) => {
|
||||
expect(getDomainNameFromHandle(input)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@ -1,9 +1,14 @@
|
||||
// @ts-expect-error legacy noImplicitAny
|
||||
import psl from 'psl';
|
||||
import { capitalize } from 'twenty-shared/utils';
|
||||
|
||||
export const getCompanyNameFromDomainName = (domainName: string) => {
|
||||
const { sld } = psl.parse(domainName);
|
||||
import { isParsedDomain } from 'src/modules/contact-creation-manager/types/is-psl-parsed-domain.type';
|
||||
|
||||
return sld ? capitalize(sld) : '';
|
||||
export const getCompanyNameFromDomainName = (domainName: string) => {
|
||||
const result = psl.parse(domainName);
|
||||
|
||||
if (!isParsedDomain(result)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return result.sld ? capitalize(result.sld) : '';
|
||||
};
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
// @ts-expect-error legacy noImplicitAny
|
||||
import psl from 'psl';
|
||||
|
||||
import { isParsedDomain } from 'src/modules/contact-creation-manager/types/is-psl-parsed-domain.type';
|
||||
|
||||
export const getDomainNameFromHandle = (handle: string): string => {
|
||||
const wholeDomain = handle?.split('@')?.[1] || '';
|
||||
|
||||
const { domain } = psl.parse(wholeDomain);
|
||||
const result = psl.parse(wholeDomain);
|
||||
|
||||
return domain || '';
|
||||
if (!isParsedDomain(result)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return result.domain ?? '';
|
||||
};
|
||||
|
||||
@ -24413,6 +24413,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/psl@npm:^1.1.3":
|
||||
version: 1.1.3
|
||||
resolution: "@types/psl@npm:1.1.3"
|
||||
checksum: 10c0/8719bac618613a2f47d4d2088742c2c01c00fd8d6fe92a3ed474aad83f7199ff53c813d8eee678768d816b02fc94c6e1f0cb4bce2e512efd0bcb3af2524ae5a6
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/qs@npm:*, @types/qs@npm:^6.9.5":
|
||||
version: 6.9.15
|
||||
resolution: "@types/qs@npm:6.9.15"
|
||||
@ -56556,6 +56563,7 @@ __metadata:
|
||||
"@types/lodash.uniqby": "npm:^4.7.9"
|
||||
"@types/lodash.upperfirst": "npm:^4.3.7"
|
||||
"@types/openid-client": "npm:^3.7.0"
|
||||
"@types/psl": "npm:^1.1.3"
|
||||
"@types/react": "npm:^18.2.39"
|
||||
"@types/unzipper": "npm:^0"
|
||||
ai: "npm:^4.3.16"
|
||||
|
||||
Reference in New Issue
Block a user