From b6be163ccb8e4b9de9afe25900850b6b4657cd1c Mon Sep 17 00:00:00 2001 From: David Lechner Date: Mon, 8 Jun 2020 19:36:59 -0500 Subject: [PATCH] refactor lookup function --- src/components/button.test.ts | 11 +---------- src/components/editor.test.ts | 11 +---------- src/components/notification.test.ts | 11 +---------- src/utils/index.test.ts | 8 +++++++- src/utils/index.ts | 15 +++++++++++++++ 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/components/button.test.ts b/src/components/button.test.ts index 1483a68e..8b6ce7ff 100644 --- a/src/components/button.test.ts +++ b/src/components/button.test.ts @@ -1,19 +1,10 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors +import { lookup } from '../utils'; import { TooltipId } from './button'; import en from './button.en.json'; -function lookup(obj: object, id: string): string | undefined { - const value = id - .split('.') - .reduce((pv, cv) => pv && (pv as Record)[cv], obj); - if (typeof value === 'string') { - return value; - } - return undefined; -} - describe('Ensure .json file has matches for TooltipIds', () => { test.each(Object.values(TooltipId))('%s', (id) => { expect(lookup(en, id)).toBeDefined(); diff --git a/src/components/editor.test.ts b/src/components/editor.test.ts index 0db32b08..bc5d3eda 100644 --- a/src/components/editor.test.ts +++ b/src/components/editor.test.ts @@ -1,19 +1,10 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors +import { lookup } from '../utils'; import { EditorStringId } from './editor'; import en from './editor.en.json'; -function lookup(obj: object, id: string): string | undefined { - const value = id - .split('.') - .reduce((pv, cv) => pv && (pv as Record)[cv], obj); - if (typeof value === 'string') { - return value; - } - return undefined; -} - describe('Ensure .json file has matches for EditorStringId', () => { test.each(Object.values(EditorStringId))('%s', (id) => { expect(lookup(en, id)).toBeDefined(); diff --git a/src/components/notification.test.ts b/src/components/notification.test.ts index 12b7c6aa..fe69432d 100644 --- a/src/components/notification.test.ts +++ b/src/components/notification.test.ts @@ -2,18 +2,9 @@ // Copyright (c) 2020 The Pybricks Authors import { MessageId } from '../reducers/notification'; +import { lookup } from '../utils'; import en from './notification.en.json'; -function lookup(obj: object, id: string): string | undefined { - const value = id - .split('.') - .reduce((pv, cv) => pv && (pv as Record)[cv], obj); - if (typeof value === 'string') { - return value; - } - return undefined; -} - describe('Ensure .json file has matches for MessageIds', () => { test.each(Object.values(MessageId))('%s', (id) => { expect(lookup(en, id)).toBeDefined(); diff --git a/src/utils/index.test.ts b/src/utils/index.test.ts index 203ceede..9f69757c 100644 --- a/src/utils/index.test.ts +++ b/src/utils/index.test.ts @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020 The Pybricks Authors -import { assert, hex } from '.'; +import { assert, hex, lookup } from '.'; test('assert', () => { const assertTrue = jest.fn(() => assert(true, 'should not throw')); @@ -16,3 +16,9 @@ test('hex', () => { expect(hex(1, 4)).toBe('0x0001'); expect(hex(2, 8)).toBe('0x00000002'); }); + +test('lookup', () => { + const obj = { a: { b: { c: 'd' } } }; + expect(lookup(obj, 'a.b.c')).toBe('d'); + expect(lookup(obj, 'a.x.y')).toBeUndefined(); +}); diff --git a/src/utils/index.ts b/src/utils/index.ts index ea7af95f..7695b84a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -21,3 +21,18 @@ export function assert(condition: boolean, message: string): void { export function hex(n: number, pad: number): string { return `0x${n.toString(16).padStart(pad, '0')}`; } + +/** + * Looks up a nested property in an object. + * @param obj The object + * @param id The property path + */ +export function lookup(obj: object, id: string): string | undefined { + const value = id + .split('.') + .reduce((pv, cv) => pv && (pv as Record)[cv], obj); + if (typeof value === 'string') { + return value; + } + return undefined; +}