Skip to content

Commit

Permalink
feat(MetaService): Add ability to return meta as key value pair inste…
Browse files Browse the repository at this point in the history
…ad of array
  • Loading branch information
Nick Lewis committed Oct 21, 2020
1 parent 5d7ee32 commit e4ba9df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/services/MetaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ export class MetaService {
return this.fields;
}

async getFieldsAsRecord(): Promise<Record<string, FieldMap>> {
return (await this.getFields()).reduce((obj, item) => {
return {
...obj,
[item.name]: item,
};
}, {});
}

async getAllLayouts(): Promise<any[]> {
await this.initialized;
if (this.allFieldsLoaded) {
Expand Down
15 changes: 15 additions & 0 deletions test/MetaService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MetaService } from '../src';
import { FieldMap } from '../lib/types';
import { of } from 'rxjs';

describe('MetaService', () => {
describe('function: hasMemory', () => {
Expand Down Expand Up @@ -44,4 +46,17 @@ describe('MetaService', () => {
expect(actual).toEqual(true);
});
});

describe('getFieldAsRecord', () => {
it('should return as a record', async() => {
const meta: MetaService = new MetaService('Candidate');
const alphaField: FieldMap = {
name: 'alpha',
label: 'Alpha'
};
spyOn(meta, 'getFields').and.returnValue(of([alphaField]).toPromise());
const response = await meta.getFieldsAsRecord();
expect(response.alpha).toBe(alphaField);
});
});
});

0 comments on commit e4ba9df

Please sign in to comment.