Skip to content

Commit 7fd13ec

Browse files
committed
Address hasContent readability
1 parent 7695871 commit 7fd13ec

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

__test__/url-helper.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import * as urlHelper from '../src/url-helper'
22

3+
describe('getServerUrl tests', () => {
4+
it('basics', async () => {
5+
// Note that URL::toString will append a trailing / when passed just a domain name ...
6+
expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/')
7+
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
8+
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
9+
expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe(
10+
'http://contoso.com/'
11+
)
12+
expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe(
13+
'https://contoso.com/'
14+
)
15+
expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe(
16+
'https://contoso.com/'
17+
)
18+
19+
// ... but can't make that same assumption when passed an URL that includes some deeper path.
20+
expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe(
21+
'https://contoso.com/a/b'
22+
)
23+
})
24+
})
25+
326
describe('isGhes tests', () => {
427
it('basics', async () => {
528
expect(urlHelper.isGhes()).toBeFalsy()

dist/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -2455,13 +2455,13 @@ function getFetchUrl(settings) {
24552455
}
24562456
function getServerUrl(url) {
24572457
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
2458-
if (hasContent(url, false)) {
2458+
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
24592459
resolvedUrl = url;
24602460
}
24612461
return new url_1.URL(resolvedUrl);
24622462
}
24632463
function getServerApiUrl(url) {
2464-
if (hasContent(url, false)) {
2464+
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
24652465
let serverUrl = getServerUrl(url);
24662466
if (isGhes(url)) {
24672467
serverUrl.pathname = 'api/v3';
@@ -2482,14 +2482,20 @@ function isGhes(url) {
24822482
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
24832483
}
24842484
function pruneSuffix(text, suffix) {
2485-
if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
2485+
if (hasContent(suffix, WhitespaceMode.AllowPureWhitespace) &&
2486+
(text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
24862487
return text.substring(0, text.length - suffix.length);
24872488
}
24882489
return text;
24892490
}
2490-
function hasContent(text, allowPureWhitespace) {
2491+
var WhitespaceMode;
2492+
(function (WhitespaceMode) {
2493+
WhitespaceMode[WhitespaceMode["IgnorePureWhitespace"] = 0] = "IgnorePureWhitespace";
2494+
WhitespaceMode[WhitespaceMode["AllowPureWhitespace"] = 1] = "AllowPureWhitespace";
2495+
})(WhitespaceMode || (WhitespaceMode = {}));
2496+
function hasContent(text, whitespaceMode) {
24912497
let refinedText = text !== null && text !== void 0 ? text : '';
2492-
if (!allowPureWhitespace) {
2498+
if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) {
24932499
refinedText = refinedText.trim();
24942500
}
24952501
return refinedText.length > 0;

src/url-helper.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
2222

2323
export function getServerUrl(url?: string): URL {
2424
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
25-
if (hasContent(url, false)) {
25+
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
2626
resolvedUrl = url!
2727
}
2828

2929
return new URL(resolvedUrl)
3030
}
3131

3232
export function getServerApiUrl(url?: string): string {
33-
if (hasContent(url, false)) {
33+
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
3434
let serverUrl = getServerUrl(url)
3535
if (isGhes(url)) {
3636
serverUrl.pathname = 'api/v3'
@@ -58,18 +58,26 @@ export function isGhes(url?: string): boolean {
5858
}
5959

6060
function pruneSuffix(text: string, suffix: string) {
61-
if (hasContent(suffix, true) && text?.endsWith(suffix)) {
61+
if (
62+
hasContent(suffix, WhitespaceMode.AllowPureWhitespace) &&
63+
text?.endsWith(suffix)
64+
) {
6265
return text.substring(0, text.length - suffix.length)
6366
}
6467
return text
6568
}
6669

70+
enum WhitespaceMode {
71+
IgnorePureWhitespace,
72+
AllowPureWhitespace
73+
}
74+
6775
function hasContent(
6876
text: string | undefined,
69-
allowPureWhitespace: boolean
77+
whitespaceMode: WhitespaceMode
7078
): boolean {
7179
let refinedText = text ?? ''
72-
if (!allowPureWhitespace) {
80+
if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) {
7381
refinedText = refinedText.trim()
7482
}
7583
return refinedText.length > 0

0 commit comments

Comments
 (0)