Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/api/company-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ test('isSupportedTicker', () => {
expect(new CompanyService('9999', client).isSupportedTicker()).toBe(false)
})

test('convertYearQuarterParamToYearQuarter', () => {
const client = new CachingBuffettCodeApiClientV2('token')
const service = new CompanyService('2371', client)
expect(
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam('LY', 3))
).toEqual(new YearQuarter(2021, 3))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(2016, 'LQ')
)
).toEqual(new YearQuarter(2016, 2))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam('LY', 'LQ')
)
).toEqual(new YearQuarter(2021, 2))
})

test('isOndemandQuarterApiPeriod', () => {
const client = new CachingBuffettCodeApiClientV2('token')
const service = new CompanyService('2371', client)
Expand All @@ -34,6 +52,12 @@ test('isOndemandQuarterApiPeriod', () => {
expect(service.isOndemandQuarterApiPeriod(new YearQuarter(2016, 3))).toBe(
false
)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam('LY', 3))
).toBe(false)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam(2016, 'LQ'))
).toBe(true)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam('LY', 'LQ'))
).toBe(false)
Expand Down
22 changes: 16 additions & 6 deletions src/api/company-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ export class CompanyService {
return !!this.company
}

public convertYearQuarterParamToYearQuarter(
period: YearQuarterParam
): YearQuarter {
if (period.isLatestYear()) {
period.year = this.company['latest_fiscal_year']
}

if (period.isLatestQuarter()) {
period.quarter = this.company['latest_fiscal_quarter']
}

return period.toYearQuarter()
}

public isOndemandQuarterApiPeriod(
_period: YearQuarter | YearQuarterParam
): boolean {
if (!this.isSupportedTicker()) {
throw new Error('unsupported ticker')
}

let period
let period: YearQuarter
if (_period instanceof YearQuarterParam) {
if (_period.isLatestYear() && _period.isLatestQuarter()) {
return false
}

period = _period.toYearQuarter()
period = this.convertYearQuarterParamToYearQuarter(_period)
} else {
period = _period
}
Expand Down
4 changes: 2 additions & 2 deletions src/fiscal-periods/period-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'

test('parse', () => {
expect(PeriodParser.parse('2020Q3')).toEqual(new YearQuarterParam(2020, 3))
expect(PeriodParser.parse('2020LQ')).toEqual(new YearQuarterParam(2020, 'LQ'))
expect(PeriodParser.parse('LYQ3')).toEqual(new YearQuarterParam('LY', 3))
expect(PeriodParser.parse('LYLQ')).toEqual(new YearQuarterParam('LY', 'LQ'))
expect(PeriodParser.parse('2020-09-06')).toEqual(
new DateParam(new Date(2020, 9, 6))
)
expect(() => PeriodParser.parse('foo')).toThrow(ParseError)
expect(() => PeriodParser.parse('2020LQ')).toThrow(ParseError)
expect(() => PeriodParser.parse('LYQ3')).toThrow(ParseError)
expect(() => PeriodParser.parse('2020/09/06')).toThrow(ParseError)
expect(() => PeriodParser.parse('latest')).toThrow(ParseError)
})
29 changes: 18 additions & 11 deletions src/fiscal-periods/year-quarter-param.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
InvalidLYLQError,
InvalidYearError,
InvalidQuarterError,
ParseError
Expand All @@ -10,22 +9,27 @@ import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
test('constructor', () => {
expect(() => new YearQuarterParam(0, 3)).toThrow(InvalidYearError)
expect(() => new YearQuarterParam(1, 3)).not.toThrow(Error)
expect(() => new YearQuarterParam('LY', 3)).toThrow(InvalidLYLQError)
expect(() => new YearQuarterParam(2018, 0)).toThrow(InvalidQuarterError)
expect(() => new YearQuarterParam(2018, 1)).not.toThrow(Error)
expect(() => new YearQuarterParam(2018, 4)).not.toThrow(Error)
expect(() => new YearQuarterParam(2018, 5)).toThrow(InvalidQuarterError)
expect(() => new YearQuarterParam('LY', 0)).toThrow(InvalidLYLQError)
expect(() => new YearQuarterParam('LY', 1)).toThrow(InvalidLYLQError)
expect(() => new YearQuarterParam('LY', 4)).toThrow(InvalidLYLQError)
expect(() => new YearQuarterParam('LY', 5)).toThrow(InvalidLYLQError)
expect(() => new YearQuarterParam('LY', 0)).toThrow(InvalidQuarterError)
expect(() => new YearQuarterParam('LY', 1)).not.toThrow(Error)
expect(() => new YearQuarterParam('LY', 4)).not.toThrow(Error)
expect(() => new YearQuarterParam('LY', 5)).toThrow(InvalidQuarterError)
expect(() => new YearQuarterParam(0, 'LQ')).toThrow(InvalidYearError)
expect(() => new YearQuarterParam(2018, 'LQ')).not.toThrow(Error)
expect(() => new YearQuarterParam('LY', 'LQ')).not.toThrow(Error)
})

test('convertibleToYearQuarter', () => {
expect(
new YearQuarterParam('LY', 'LQ').convertibleToYearQuarter()
).toBeFalsy()
expect(
new YearQuarterParam(2020, 'LQ').convertibleToYearQuarter()
).toBeFalsy()
expect(new YearQuarterParam('LY', 3).convertibleToYearQuarter()).toBeFalsy()
expect(new YearQuarterParam(2020, 3).convertibleToYearQuarter()).toBeTruthy()
})

Expand All @@ -37,13 +41,13 @@ test('toYearQuarter', () => {
})

test('isLatestYear', () => {
expect(new YearQuarterParam('LY', 'LQ').isLatestYear()).toBeTruthy()
expect(new YearQuarterParam('LY', 3).isLatestYear()).toBeTruthy()
expect(new YearQuarterParam(2020, 3).isLatestYear()).toBeFalsy()
})

test('isLatestQuarter', () => {
expect(new YearQuarterParam('LY', 'LQ').isLatestYear()).toBeTruthy()
expect(new YearQuarterParam(2020, 3).isLatestYear()).toBeFalsy()
expect(new YearQuarterParam(2020, 'LQ').isLatestQuarter()).toBeTruthy()
expect(new YearQuarterParam(2020, 3).isLatestQuarter()).toBeFalsy()
})

test('fromYearQuarter', () => {
Expand All @@ -59,14 +63,17 @@ test('parse', () => {
expect(YearQuarterParam.parse('2020q3')).toEqual(
new YearQuarterParam(2020, 3)
)
expect(YearQuarterParam.parse('2020LQ')).toEqual(
new YearQuarterParam(2020, 'LQ')
)
expect(YearQuarterParam.parse('LYQ3')).toEqual(new YearQuarterParam('LY', 3))
expect(YearQuarterParam.parse('LYLQ')).toEqual(
new YearQuarterParam('LY', 'LQ')
)
expect(YearQuarterParam.parse('lylq')).toEqual(
new YearQuarterParam('LY', 'LQ')
)

expect(() => YearQuarterParam.parse('20Q3')).toThrow(ParseError)
expect(() => YearQuarterParam.parse('LYQ3')).toThrow(InvalidLYLQError)
expect(() => YearQuarterParam.parse('2020LQ')).toThrow(InvalidLYLQError)
expect(() => YearQuarterParam.parse('foo')).toThrow(ParseError)
})
10 changes: 0 additions & 10 deletions src/fiscal-periods/year-quarter-param.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
InvalidLYLQError,
InvalidYearError,
InvalidQuarterError,
ParseError
Expand All @@ -8,14 +7,6 @@ import { YearQuarter } from '~/fiscal-periods/year-quarter'

export class YearQuarterParam {
constructor(public year: number | 'LY', public quarter: number | 'LQ') {
if (
(!this.isLatestYear() && this.isLatestQuarter()) ||
(this.isLatestYear() && !this.isLatestQuarter())
) {
// NOTE: 現状ではLY/LQの同時指定しかサポートしない
throw new InvalidLYLQError()
}

if (!this.isLatestYear() && year < 1) {
throw new InvalidYearError()
}
Expand Down Expand Up @@ -45,7 +36,6 @@ export class YearQuarterParam {
return this.quarter === 'LQ'
}

// XXX: LYLQの同時指定や-1などの相対値指定をどうするか確認する
static parse(str: string): YearQuarterParam {
str = str.toUpperCase()
const matches = str.match(/^(\d{4}|LY)(Q\d|LQ)$/)
Expand Down