Skip to content

Commit

Permalink
Address hasContent readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jww3 committed Oct 17, 2024
1 parent 7695871 commit 7fd13ec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
23 changes: 23 additions & 0 deletions __test__/url-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import * as urlHelper from '../src/url-helper'

describe('getServerUrl tests', () => {
it('basics', async () => {
// Note that URL::toString will append a trailing / when passed just a domain name ...
expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe(
'http://contoso.com/'
)
expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe(
'https://contoso.com/'
)
expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe(
'https://contoso.com/'
)

// ... but can't make that same assumption when passed an URL that includes some deeper path.
expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe(
'https://contoso.com/a/b'
)
})
})

describe('isGhes tests', () => {
it('basics', async () => {
expect(urlHelper.isGhes()).toBeFalsy()
Expand Down
16 changes: 11 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,13 +2455,13 @@ function getFetchUrl(settings) {
}
function getServerUrl(url) {
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
resolvedUrl = url;
}
return new url_1.URL(resolvedUrl);
}
function getServerApiUrl(url) {
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
let serverUrl = getServerUrl(url);
if (isGhes(url)) {
serverUrl.pathname = 'api/v3';
Expand All @@ -2482,14 +2482,20 @@ function isGhes(url) {
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
}
function pruneSuffix(text, suffix) {
if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
if (hasContent(suffix, WhitespaceMode.AllowPureWhitespace) &&
(text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
return text.substring(0, text.length - suffix.length);
}
return text;
}
function hasContent(text, allowPureWhitespace) {
var WhitespaceMode;
(function (WhitespaceMode) {
WhitespaceMode[WhitespaceMode["IgnorePureWhitespace"] = 0] = "IgnorePureWhitespace";
WhitespaceMode[WhitespaceMode["AllowPureWhitespace"] = 1] = "AllowPureWhitespace";
})(WhitespaceMode || (WhitespaceMode = {}));
function hasContent(text, whitespaceMode) {
let refinedText = text !== null && text !== void 0 ? text : '';
if (!allowPureWhitespace) {
if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) {
refinedText = refinedText.trim();
}
return refinedText.length > 0;
Expand Down
18 changes: 13 additions & 5 deletions src/url-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export function getFetchUrl(settings: IGitSourceSettings): string {

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

return new URL(resolvedUrl)
}

export function getServerApiUrl(url?: string): string {
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
let serverUrl = getServerUrl(url)
if (isGhes(url)) {
serverUrl.pathname = 'api/v3'
Expand Down Expand Up @@ -58,18 +58,26 @@ export function isGhes(url?: string): boolean {
}

function pruneSuffix(text: string, suffix: string) {
if (hasContent(suffix, true) && text?.endsWith(suffix)) {
if (
hasContent(suffix, WhitespaceMode.AllowPureWhitespace) &&
text?.endsWith(suffix)
) {
return text.substring(0, text.length - suffix.length)
}
return text
}

enum WhitespaceMode {
IgnorePureWhitespace,
AllowPureWhitespace
}

function hasContent(
text: string | undefined,
allowPureWhitespace: boolean
whitespaceMode: WhitespaceMode
): boolean {
let refinedText = text ?? ''
if (!allowPureWhitespace) {
if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) {
refinedText = refinedText.trim()
}
return refinedText.length > 0
Expand Down

0 comments on commit 7fd13ec

Please sign in to comment.