Skip to content

Commit ae199ad

Browse files
committed
fix: refactored code
1 parent 4a5f7c5 commit ae199ad

File tree

2 files changed

+3
-65
lines changed

2 files changed

+3
-65
lines changed

src/shared/experience/expSite.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class ExperienceSite {
3737
this.org = org;
3838
this.siteDisplayName = siteName.trim();
3939
this.siteName = this.siteDisplayName.replace(' ', '_');
40+
// Replace any special characters in site name with underscore
41+
this.siteName = this.siteName.replace(/[^a-zA-Z0-9]/g, '_');
4042
}
4143

4244
/**
@@ -179,13 +181,6 @@ export class ExperienceSite {
179181

180182
public async getRemoteMetadata(): Promise<SiteMetadata | undefined> {
181183
if (this.metadataCache.remoteMetadata) return this.metadataCache.remoteMetadata;
182-
// Check if there are special characters in the site name
183-
const isInvalidSiteName = hasSpacesOrSpecialChars(this.siteName);
184-
// const originalSiteName = this.siteName;
185-
if (isInvalidSiteName) {
186-
const updatedSiteName = replaceSpacesAndSpecialChars(this.siteName);
187-
this.siteName = updatedSiteName;
188-
}
189184
const result = await this.org
190185
.getConnection()
191186
.query<{ Name: string; LastModifiedDate: string }>(
@@ -363,27 +358,3 @@ function getSiteNameFromStaticResource(staticResourceName: string): string {
363358
}
364359
return parts.slice(4).join(' ');
365360
}
366-
367-
export function replaceSpacesAndSpecialChars(value: string): string {
368-
// Replace any special characters with underscore
369-
value = value.replace(/[^a-zA-Z0-9]/g, '_');
370-
371-
// Replace spaces with underscore
372-
value = value.replace(/ /g, '_');
373-
374-
return value;
375-
}
376-
377-
export function hasSpacesOrSpecialChars(value: string): boolean {
378-
// Check for spaces
379-
if (value.includes(' ')) {
380-
return true;
381-
}
382-
383-
// Check for special characters
384-
if (/[^\w]/.test(value)) {
385-
return true;
386-
}
387-
388-
return false;
389-
}

test/spec/expSite.spec.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,7 @@
77
import { expect } from 'chai';
88
import { Connection, Org } from '@salesforce/core';
99
import sinon from 'sinon';
10-
import {
11-
replaceSpacesAndSpecialChars,
12-
hasSpacesOrSpecialChars,
13-
ExperienceSite,
14-
} from '../../src/shared/experience/expSite.js';
15-
16-
describe('replaceSpacesAndSpecialChars', () => {
17-
it('should replace spaces and special characters with underscores', () => {
18-
const input = 'site#name@with-special%chars with spaces';
19-
const expectedOutput = 'site_name_with_special_chars_with_spaces';
20-
const output = replaceSpacesAndSpecialChars(input);
21-
expect(output).to.equal(expectedOutput);
22-
});
23-
});
24-
25-
describe('hasSpacesOrSpecialChars', () => {
26-
it('should return true if the input string has spaces', () => {
27-
const input = 'Hello World';
28-
const output = hasSpacesOrSpecialChars(input);
29-
expect(output).to.be.true;
30-
});
31-
32-
it('should return true if the input string has special characters', () => {
33-
const input = 'Hello, @#';
34-
const output = hasSpacesOrSpecialChars(input);
35-
expect(output).to.be.true;
36-
});
37-
38-
it('should return false if the input string has neither spaces nor special characters', () => {
39-
const input = 'HelloWorld';
40-
const output = hasSpacesOrSpecialChars(input);
41-
expect(output).to.be.false;
42-
});
43-
});
10+
import { ExperienceSite } from '../../src/shared/experience/expSite.js';
4411

4512
describe('getRemoteMetadata', () => {
4613
it('should return remote metadata when it exists', async () => {

0 commit comments

Comments
 (0)