From 3da15f4baf8b305e4e27eca0be878d7255e8775e Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Tue, 29 Aug 2023 13:35:20 +0200 Subject: [PATCH 1/2] Fixes #242 --- CHANGELOG.md | 6 ++++++ src/util/common.ts | 4 ++-- test/unit/types/election.test.ts | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f1b8b7b..db9d0eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- `dotobject` helper returns null when key is not found. + ## [0.1.1] - 2023-08-14 ### Fixed diff --git a/src/util/common.ts b/src/util/common.ts index 3ed7aa43..a72b52f9 100644 --- a/src/util/common.ts +++ b/src/util/common.ts @@ -60,8 +60,8 @@ export function formatUnits(value: BigNumberish, decimals: number = 18): string */ 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)); + if (dot.length) { + return typeof obj[dot[0]] !== 'undefined' ? rec(obj[dot[0]], dot.slice(1)) : null; } return obj; }; diff --git a/test/unit/types/election.test.ts b/test/unit/types/election.test.ts index 459a8c06..996611e4 100644 --- a/test/unit/types/election.test.ts +++ b/test/unit/types/election.test.ts @@ -60,6 +60,10 @@ describe('Election tests', () => { }, }); expect(election.get('census.type')).toEqual('spreadsheet'); + expect(election.get('census.fields')).toEqual(['firstname', 'lastname', 'email']); + expect(election.get('bad')).toBeNull(); + expect(election.get('bad.bad')).toBeNull(); + expect(election.get('census.bad')).toBeNull(); expect(election.electionType).toEqual({ autoStart: true, interruptible: true, From 23cfbcbcb2209cd9d4771fa4709411f65f658ec9 Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Wed, 30 Aug 2023 14:43:06 +0200 Subject: [PATCH 2/2] Election meta getter returns null when no meta is set --- src/types/election/election.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/election/election.ts b/src/types/election/election.ts index 6e7b1341..362323a7 100644 --- a/src/types/election/election.ts +++ b/src/types/election/election.ts @@ -253,6 +253,6 @@ export abstract class Election { } get(dot: string) { - return dotobject(this.meta, dot); + return this.meta ? dotobject(this.meta, dot) : null; } }