diff --git a/src/sections/sections-pattern.spec.ts b/src/sections/sections-pattern.spec.ts new file mode 100644 index 0000000..86382f9 --- /dev/null +++ b/src/sections/sections-pattern.spec.ts @@ -0,0 +1,28 @@ +import { SECTION_SEGMENT_BREAKDOWN } from './sections-pattern' + +describe('SECTION_SEGMENT_BREAKDOWN', () => { + it.each(['320010003', '320012001', '320291132'])( + 'matches the full abroad section id %s', + (id) => { + const groups = SECTION_SEGMENT_BREAKDOWN.exec(id)?.groups + expect(groups?.abroadRegionCode).toBe('32') + expect(groups?.countryCode).toBe(id.slice(2, 5)) + expect(groups?.sectionCodeAbroad).toBe(id.slice(6)) + }, + ) + + it('still matches abroad region and country segments', () => { + expect(SECTION_SEGMENT_BREAKDOWN.exec('32')?.groups?.abroadRegionCode).toBe( + '32', + ) + expect(SECTION_SEGMENT_BREAKDOWN.exec('32001')?.groups?.countryCode).toBe( + '001', + ) + }) + + it('still matches domestic segments', () => { + const groups = SECTION_SEGMENT_BREAKDOWN.exec('010100001')?.groups + expect(groups?.electionRegionCode).toBe('01') + expect(groups?.sectionCodeDomestic).toBe('001') + }) +}) diff --git a/src/sections/sections-pattern.ts b/src/sections/sections-pattern.ts index fee7891..383b395 100644 --- a/src/sections/sections-pattern.ts +++ b/src/sections/sections-pattern.ts @@ -1,2 +1,2 @@ export const SECTION_SEGMENT_BREAKDOWN = - /^(?0[1-9]|[12][0-9]|3[01])(?:(?[0-9]{2})(?:(?[0-9]{2})(?[0-9]{3})?)?)?$|^(?32)(?:(?[0-9]{3})(?:0(?[0-9]{3})?)?)?$/ + /^(?0[1-9]|[12][0-9]|3[01])(?:(?[0-9]{2})(?:(?[0-9]{2})(?[0-9]{3})?)?)?$|^(?32)(?:(?[0-9]{3})(?:[0-9](?[0-9]{3})?)?)?$/