From e64a7a5cd75db960d13c1ae20dd2a7c523ab9150 Mon Sep 17 00:00:00 2001 From: "Stone C. Lasley" Date: Sat, 4 Dec 2021 19:03:35 -0700 Subject: [PATCH] feat(Gs1Reader): allow ad hoc Ai to be passed on reader configuration Allow consumers to set non-standard AI in reader configuration or just set standard AI that have not been implemented yet re #44 --- src/__tests__/readers/gs-1.reader.spec.ts | 53 +++++++++++++++++++++++ src/models/reader.configuration.ts | 2 + src/readers/gs-1.reader.ts | 10 +++-- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/__tests__/readers/gs-1.reader.spec.ts b/src/__tests__/readers/gs-1.reader.spec.ts index 32a51c6..12e9bd1 100644 --- a/src/__tests__/readers/gs-1.reader.spec.ts +++ b/src/__tests__/readers/gs-1.reader.spec.ts @@ -22,6 +22,7 @@ export class Tester extends GS1Reader { describe('Gs1Reader', () => { let config: IReaderConfiguration; let classUnderTest: Tester; + let adHocAi: ApplicationIdentifier; beforeEach(() => { config = { @@ -29,6 +30,8 @@ describe('Gs1Reader', () => { identifier: ']C1', } as IReaderConfiguration; + // 7009 may be valid somtime in the future + adHocAi = new ApplicationIdentifier('7009', 'My Custom AI', 6, true); classUnderTest = new Tester(config); }); @@ -171,6 +174,16 @@ describe('Gs1Reader', () => { expect(actual.variableLength).toBe(true); }); }); + + test('should get ad hoc Ai', () => { + config.ai = adHocAi; + const { code, description, length, variableLength } = adHocAi; + const actual = classUnderTest.testGetAi(code); + expect(actual.code).toBe(code); + expect(actual.description).toBe(description); + expect(actual.length).toBe(length); + expect(actual.variableLength).toBe(variableLength); + }); }); describe('decode', () => { @@ -296,4 +309,44 @@ describe('Gs1Reader', () => { value: '210101', }); }); + + test('should parse adhoc', () => { + config.ai = adHocAi; + const actual = classUnderTest.decode( + `]C1019628329083134011150523310200059421145143242042 ${adHocAi.code}123456`, + ); + const actual2 = classUnderTest.decode( + `]C10208413556000950${adHocAi.code}1234563703 10ES003472002`, + ); + + expect(actual.values).toContainEqual({ + code: '21', + value: '145143242042', + }); + expect(actual.values).toContainEqual({ code: '310', value: 5.94 }); + expect(actual.values).toContainEqual({ code: '11', value: '150523' }); + expect(actual.values).toContainEqual({ + code: '01', + value: '96283290831340', + }); + expect(actual.values).toContainEqual({ + code: adHocAi.code, + value: '123456', + }); + + expect(actual2.values).toContainEqual({ + code: '02', + value: '08413556000950', + }); + expect(actual2.values).toContainEqual({ code: '37', value: '03' }); + expect(actual2.values).toContainEqual({ + code: '10', + value: 'ES003472002', + }); + expect(actual2.values).toContainEqual({ + code: adHocAi.code, + value: '123456', + }); + }); + }); diff --git a/src/models/reader.configuration.ts b/src/models/reader.configuration.ts index 3abdc07..6ada7e0 100644 --- a/src/models/reader.configuration.ts +++ b/src/models/reader.configuration.ts @@ -1,3 +1,4 @@ +import { ApplicationIdentifier } from '.'; import { IEmbeddedData } from './embedded-data'; export interface IReaderConfiguration { @@ -5,4 +6,5 @@ export interface IReaderConfiguration { delimiter?: string; identifier?: string; values?: IEmbeddedData[]; + ai?: ApplicationIdentifier; } diff --git a/src/readers/gs-1.reader.ts b/src/readers/gs-1.reader.ts index 8c191b1..43ae395 100644 --- a/src/readers/gs-1.reader.ts +++ b/src/readers/gs-1.reader.ts @@ -59,9 +59,7 @@ export class GS1Reader extends BaseReader { let codeLength = 2; while (ai === null && codeLength < 5) { const code = value.substr(0, codeLength); - const ais = APPLICATION_IDENTIFIERS.filter(x => { - return x.code === code; - }); + const ais = this.aiList.filter(x => x.code === code); if (ais.length > 0) { ai = ais[0]; } else { @@ -100,4 +98,10 @@ export class GS1Reader extends BaseReader { return vals; } + + protected get aiList(): ApplicationIdentifier[] { + return (this.configuration?.ai != null) + ? [...APPLICATION_IDENTIFIERS, this.configuration?.ai] + : APPLICATION_IDENTIFIERS; + } }