-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { immerable } from 'immer' | ||
import { Fragment } from 'fragmentarium/domain/fragment' | ||
|
||
interface Record { | ||
readonly afoNumber: string | ||
readonly page: string | ||
readonly text: string | ||
readonly textNumber: string | ||
readonly linesDiscussed?: string | ||
readonly discussedBy?: string | ||
readonly discussedByNotes?: string | ||
} | ||
|
||
export default class AfoRegisterRecord { | ||
[immerable] = true | ||
|
||
readonly afoNumber: string | ||
readonly page: string | ||
readonly text: string | ||
readonly textNumber: string | ||
readonly linesDiscussed?: string | ||
readonly discussedBy?: string | ||
readonly discussedByNotes?: string | ||
|
||
constructor({ | ||
afoNumber, | ||
page, | ||
text, | ||
textNumber, | ||
linesDiscussed, | ||
discussedBy, | ||
discussedByNotes, | ||
}: Record) { | ||
this.afoNumber = afoNumber | ||
this.page = page | ||
this.text = text | ||
this.textNumber = textNumber | ||
this.linesDiscussed = linesDiscussed | ||
this.discussedBy = discussedBy | ||
this.discussedByNotes = discussedByNotes | ||
} | ||
|
||
toMarkdownString(fragments: Fragment[]): string { | ||
let result = this.text + (this.textNumber ? ' ' + this.textNumber : '') | ||
const linkToFragment = this.findLinkToFragment(fragments) | ||
if (linkToFragment) result += `(${linkToFragment})` | ||
if (this.linesDiscussed) result += ', ' + this.linesDiscussed | ||
if (this.discussedBy) result += ': ' + this.discussedBy | ||
if (this.discussedByNotes) result += ' ' + this.discussedByNotes | ||
|
||
result += `<small class="text-black-50 ml-3">${ | ||
this.afoNumber + this.page | ||
}</small>` | ||
|
||
result = result.replace(/\^([^^]+)\^/g, '<sup>$1</sup>') | ||
return result | ||
} | ||
|
||
findLinkToFragment(fragments: Fragment[]): string { | ||
const reference = this.text + ' ' + this.textNumber | ||
const matchingFragments = fragments.filter((fragment) => | ||
fragment.traditionalReferences.includes(reference) | ||
) | ||
if (matchingFragments.length === 0) return '' | ||
return matchingFragments.map((fragment) => fragment.number).join(', ') | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/afo-register/infrastructure/AfoRegisterRepository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import AfoRegisterRecord from 'afo-register/domain/Record' | ||
import ApiClient from 'http/ApiClient' | ||
import AfoRegisterRepository from './AfoRegisterRepository' | ||
import Promise from 'bluebird' | ||
import { testDelegation, TestData } from 'test-support/utils' | ||
|
||
jest.mock('afo-register/domain/Record') | ||
jest.mock('http/ApiClient') | ||
|
||
const apiClient = new (ApiClient as jest.Mock<jest.Mocked<ApiClient>>)() | ||
|
||
const afoRegisterRepository = new AfoRegisterRepository(apiClient) | ||
const query = '{"afoNumber": "AfO 12", "page": "321"}' | ||
const resultStub = { | ||
afoNumber: 'AfO 12', | ||
page: '321', | ||
text: 'text', | ||
textNumber: 'text number', | ||
} as AfoRegisterRecord | ||
const entry = new AfoRegisterRecord(resultStub) | ||
|
||
const testData: TestData<AfoRegisterRepository>[] = [ | ||
new TestData( | ||
'search', | ||
[query], | ||
apiClient.fetchJson, | ||
[entry], | ||
[`/afo-register?${encodeURIComponent(query)}`, false], | ||
Promise.resolve(entry) | ||
), | ||
] | ||
|
||
describe('AfoRegisterRepository', () => | ||
testDelegation(afoRegisterRepository, testData)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import AfoRegisterRecord from 'afo-register/domain/Record' | ||
import Promise from 'bluebird' | ||
import ApiClient from 'http/ApiClient' | ||
|
||
export default class BibliographyRepository { | ||
private readonly apiClient: ApiClient | ||
|
||
constructor(apiClient: ApiClient) { | ||
this.apiClient = apiClient | ||
} | ||
|
||
search(query: string): Promise<AfoRegisterRecord> { | ||
return this.apiClient | ||
.fetchJson(`/afo-register?${encodeURIComponent(query)}`, false) | ||
.then((result) => new AfoRegisterRecord(result)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters