Skip to content

Commit

Permalink
feat(Gs1Reader): allow ad hoc Ai to be passed on reader configuration
Browse files Browse the repository at this point in the history
Allow consumers to set non-standard AI in reader configuration or just set standard AI that have not
been implemented yet

re #44
  • Loading branch information
stonelasley committed Dec 5, 2021
1 parent d841d0e commit e64a7a5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/__tests__/readers/gs-1.reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export class Tester extends GS1Reader {
describe('Gs1Reader', () => {
let config: IReaderConfiguration;
let classUnderTest: Tester;
let adHocAi: ApplicationIdentifier;

beforeEach(() => {
config = {
delimiter: ' ',
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);
});

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
});
});

});
2 changes: 2 additions & 0 deletions src/models/reader.configuration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ApplicationIdentifier } from '.';
import { IEmbeddedData } from './embedded-data';

export interface IReaderConfiguration {
symbology: string;
delimiter?: string;
identifier?: string;
values?: IEmbeddedData[];
ai?: ApplicationIdentifier;
}
10 changes: 7 additions & 3 deletions src/readers/gs-1.reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e64a7a5

Please sign in to comment.