diff --git a/ts/components/emoji/lib.ts b/ts/components/emoji/lib.ts index 3485ac361..4cae1d9c9 100644 --- a/ts/components/emoji/lib.ts +++ b/ts/components/emoji/lib.ts @@ -20,6 +20,7 @@ import { import Fuse from 'fuse.js'; import PQueue from 'p-queue'; import is from '@sindresorhus/is'; +import { getOwn } from '../../util/getOwn'; export const skinTones = ['1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF']; @@ -282,19 +283,11 @@ export function convertShortName( } export function emojiToImage(emoji: string): string | undefined { - if (!Object.prototype.hasOwnProperty.call(imageByEmoji, emoji)) { - return undefined; - } - - return imageByEmoji[emoji]; + return getOwn(imageByEmoji, emoji); } export function emojiToData(emoji: string): EmojiData | undefined { - if (!Object.prototype.hasOwnProperty.call(dataByEmoji, emoji)) { - return undefined; - } - - return dataByEmoji[emoji]; + return getOwn(dataByEmoji, emoji); } function getCountOfAllMatches(str: string, regex: RegExp) { diff --git a/ts/test/util/getOwn_test.ts b/ts/test/util/getOwn_test.ts new file mode 100644 index 000000000..ff789bace --- /dev/null +++ b/ts/test/util/getOwn_test.ts @@ -0,0 +1,50 @@ +// Copyright 2020 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only + +import { assert } from 'chai'; + +import { getOwn } from '../../util/getOwn'; + +describe('getOwn', () => { + class Person { + public birthYear: number; + + constructor(birthYear: number) { + this.birthYear = birthYear; + } + + getAge() { + return new Date().getFullYear() - this.birthYear; + } + } + + it('returns undefined when asking for a non-existent property', () => { + const obj: Record = { bar: 123 }; + assert.isUndefined(getOwn(obj, 'foo')); + }); + + it('returns undefined when asking for a non-own property', () => { + const obj: Record = { bar: 123 }; + assert.isUndefined(getOwn(obj, 'hasOwnProperty')); + + const person = new Person(1880); + assert.isUndefined(getOwn(person, 'getAge')); + }); + + it('returns own properties', () => { + const obj: Record = { foo: 123 }; + assert.strictEqual(getOwn(obj, 'foo'), 123); + + const person = new Person(1880); + assert.strictEqual(getOwn(person, 'birthYear'), 1880); + }); + + it('works even if `hasOwnProperty` has been overridden for the object', () => { + const obj: Record = { + foo: 123, + hasOwnProperty: () => true, + }; + assert.strictEqual(getOwn(obj, 'foo'), 123); + assert.isUndefined(getOwn(obj, 'bar')); + }); +}); diff --git a/ts/util/getOwn.ts b/ts/util/getOwn.ts new file mode 100644 index 000000000..b6989e176 --- /dev/null +++ b/ts/util/getOwn.ts @@ -0,0 +1,13 @@ +// Copyright 2020 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only + +import { has } from 'lodash'; + +// We want this to work with any object, so we allow `object` here. +// eslint-disable-next-line @typescript-eslint/ban-types +export function getOwn( + obj: TObject, + key: TKey +): TObject[TKey] | undefined { + return has(obj, key) ? obj[key] : undefined; +}