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,