Skip to content

Commit

Permalink
Added dotobject in Election
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Aug 1, 2023
1 parent 6a2f1ba commit 6b8347e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/types/election/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IQuestion } from '../metadata';
import { Census } from '../census';
import { MultiLanguage } from '../../util/lang';
import { UnpublishedElection } from './unpublished';
import { dotobject } from '../../util/common';

export interface IVoteType {
/**
Expand Down Expand Up @@ -250,4 +251,8 @@ export abstract class Election {
get addSDKVersion(): boolean {
return this._addSDKVersion;
}

get(dot: string) {
return dotobject(this.meta, dot);
}
}
19 changes: 19 additions & 0 deletions src/util/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ export function areEqualHexStrings(hex1?: string, hex2?: string) {
export function formatUnits(value: BigNumberish, decimals: number = 18): string {
return ethersFormatUnits(value, decimals);
}

/**
* Dot notation to object conversion. Takes any object as first argument and uses the string dot notation from the
* second argument (i.e. 'a.child.node') to access that given object value.
*
* @param {any} obj Object to be accessed by dot notation
* @param {string} dot Dot notation string to extract object data
* @returns
*/
export const dotobject = (obj: any, dot: string) => {
const rec = (obj: any, dot: string[]): any => {
if (dot.length && typeof obj[dot[0]] !== 'undefined') {
return rec(obj[dot[0]], dot.slice(1));
}
return obj;
};

return rec(obj, dot.split('.'));
};
9 changes: 9 additions & 0 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('Election integration tests', () => {
test1: 'test1',
test2: 'test2',
},
census: {
fields: ['firstname', 'lastname', 'email'],
type: 'spreadsheet',
},
};

await client.createAccount();
Expand All @@ -83,10 +87,15 @@ describe('Election integration tests', () => {
test1: 'test1',
test2: 'test2',
},
census: {
fields: ['firstname', 'lastname', 'email'],
type: 'spreadsheet',
},
sdk: {
version: SDK_VERSION,
},
});
expect(publishedElection.get('census.type')).toEqual('spreadsheet');
expect(publishedElection.electionType).toStrictEqual({
autoStart: true,
interruptible: true,
Expand Down
9 changes: 9 additions & 0 deletions test/unit/types/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ beforeEach(() => {
test1: 'test1',
test2: 'test2',
},
census: {
fields: ['firstname', 'lastname', 'email'],
type: 'spreadsheet',
},
},
startDate: new Date().getTime() + 80000,
endDate: new Date().getTime() + 10000000,
Expand All @@ -50,7 +54,12 @@ describe('Election tests', () => {
test1: 'test1',
test2: 'test2',
},
census: {
fields: ['firstname', 'lastname', 'email'],
type: 'spreadsheet',
},
});
expect(election.get('census.type')).toEqual('spreadsheet');
expect(election.electionType).toEqual({
autoStart: true,
interruptible: true,
Expand Down

0 comments on commit 6b8347e

Please sign in to comment.