Skip to content

Commit

Permalink
chore: minor refactoring to reduce lines and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vraravam committed Jan 26, 2024
1 parent 7111817 commit b7effe6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
9 changes: 3 additions & 6 deletions src/helpers/certs-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { readdirSync, readFileSync, ensureDirSync } from 'fs-extra';
import { join } from 'node:path';
import { userDataCertsPath } from '../environment-remote';
import { removeNewLines } from '../jsUtils';

export function removeNewLines(string: string) {
return string.replaceAll(/\r?\n|\r/g, '');
}

export function readCerts() {
export function checkIfCertIsPresent(certData: string): boolean {
const certsFolder = userDataCertsPath();

ensureDirSync(certsFolder);
Expand All @@ -21,5 +18,5 @@ export function readCerts() {
certs.push(removeNewLines(cert));
}

return certs;
return certs.length > 0 && certs.includes(removeNewLines(certData));
}
2 changes: 1 addition & 1 deletion src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,4 @@
"workspaceDrawer.workspaceFeatureInfo": "<p>Ferdium Workspaces let you focus on what’s important right now. Set up different sets of services and easily switch between them at any time.</p><p>You decide which services you need when and where, so we can help you stay on top of your game - or easily switch off from work whenever you want.</p>",
"workspaceDrawer.workspacesSettingsTooltip": "Edit workspaces settings",
"workspaces.switchingIndicator.switchingTo": "Switching to"
}
}
19 changes: 2 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { openExternalUrl } from './helpers/url-helpers';
import userAgent from './helpers/userAgent-helpers';
import { translateTo } from './helpers/translation-helpers';
import { darkThemeGrayDarkest } from './themes/legacy';
import { readCerts, removeNewLines } from './helpers/certs-helpers';
import { checkIfCertIsPresent } from './helpers/certs-helpers';

const debug = require('./preload-safe-debug')('Ferdium:App');

Expand Down Expand Up @@ -773,21 +773,6 @@ app.on(
return;
}

const trustedCerts = readCerts();
if (!trustedCerts) {
callback(false);
return;
}

const isTrustedCert = trustedCerts.includes(
removeNewLines(certificate.data),
);

if (isTrustedCert) {
callback(true);
return;
}

callback(false);
callback(checkIfCertIsPresent(certificate.data));
},
);
3 changes: 3 additions & 0 deletions src/jsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ export const acceleratorString = (
prefix: string = '(',
suffix: string = ')',
) => (index <= 10 ? `${prefix}${keyCombo}+${index % 10}${suffix}` : '');

export const removeNewLines = (input: string): string =>
input.replaceAll(/\r?\n|\r/g, '');
16 changes: 16 additions & 0 deletions test/jsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,20 @@ describe('jsUtils', () => {
expect(jsUtils.acceleratorString(11, 'abc')).toEqual('');
});
});

describe('removeNewLines', () => {
it('handles unix style new lines', () => {
expect(jsUtils.removeNewLines('abc def\nghi')).toEqual('abc defghi');
});

it('handles windows style new lines', () => {
expect(jsUtils.removeNewLines('abc def\r\nghi')).toEqual('abc defghi');
});

it('handles new lines in the beginning, middle and ending of the string', () => {
expect(jsUtils.removeNewLines('\nabc def\r\nghi\n')).toEqual(
'abc defghi',
);
});
});
});

0 comments on commit b7effe6

Please sign in to comment.