Skip to content

Commit

Permalink
Adds splitOwnerAndRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbs committed Jul 25, 2024
1 parent 7d2c385 commit 00910cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
26 changes: 26 additions & 0 deletions __test__/utils/splitOwnerAndRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { splitOwnerAndRepository } from "@/common"

test("It returns undefined when string includes no slash", async () => {
const result = splitOwnerAndRepository("foo")
expect(result).toBeUndefined()
})

test("It returns undefined when repository is empty", async () => {
const result = splitOwnerAndRepository("foo/")
expect(result).toBeUndefined()
})

test("It returns undefined when owner is empty", async () => {
const result = splitOwnerAndRepository("/foo")
expect(result).toBeUndefined()
})

test("It splits owner and repository", async () => {
const result = splitOwnerAndRepository("acme/foo")
expect(result).toEqual({ owner: "acme", repository: "foo" })
})

test("It splits owner and repository for repository name containing a slash", async () => {
const result = splitOwnerAndRepository("acme/foo/bar")
expect(result).toEqual({ owner: "acme", repository: "foo/bar" })
})
1 change: 1 addition & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { default as env } from "./env"
export { default as getBaseFilename } from "./getBaseFilename"
export { default as isMac } from "./isMac"
export { default as useKeyboardShortcut } from "./useKeyboardShortcut"
export { default as splitOwnerAndRepository } from "./splitOwnerAndRepository"
14 changes: 14 additions & 0 deletions src/common/utils/splitOwnerAndRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Split full repository names into owner and repository.
// shapehq/foo becomes { owner: "shapehq", "repository": "foo" }
export default (str: string) => {
const index = str.indexOf("/")
if (index === -1) {
return undefined
}
const owner = str.substring(0, index)
const repository = str.substring(index + 1)
if (owner.length == 0 || repository.length == 0) {
return undefined
}
return { owner, repository }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import IGitHubRepositoryDataSource, {
GitHubRepository
} from "./IGitHubRepositoryDataSource"
import { splitOwnerAndRepository } from "@/common"

export default class FilteringGitHubRepositoryDataSource implements IGitHubRepositoryDataSource {
private readonly dataSource: IGitHubRepositoryDataSource
Expand All @@ -16,18 +17,9 @@ export default class FilteringGitHubRepositoryDataSource implements IGitHubRepos

async getRepositories(): Promise<GitHubRepository[]> {
const repositories = await this.dataSource.getRepositories()
// Split full repository names into owner and repository.
// shapehq/foo becomes { owner: "shapehq", "repository": "foo" }
const hiddenOwnerAndRepoNameList = this.hiddenRepositories.map(str => {
const index = str.indexOf("/")
if (index === -1) {
return { owner: str, repository: "" }
}
const owner = str.substring(0, index)
const repository = str.substring(index + 1)
return { owner, repository }
})
// Only return repositories that are not on the hidden list.
const hiddenOwnerAndRepoNameList = this.hiddenRepositories
.map(splitOwnerAndRepository)
.filter(e => e !== undefined)
return repositories.filter(repository => {
const hiddenMatch = hiddenOwnerAndRepoNameList.find(e =>
e.owner == repository.owner && e.repository == repository.name
Expand Down

0 comments on commit 00910cc

Please sign in to comment.