Add `getOwn` utility function

This commit is contained in:
Evan Hahn 2020-11-04 13:56:49 -06:00 committed by GitHub
parent d1c63609a7
commit ca83281986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

View File

@ -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) {

View File

@ -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<string, number> = { bar: 123 };
assert.isUndefined(getOwn(obj, 'foo'));
});
it('returns undefined when asking for a non-own property', () => {
const obj: Record<string, number> = { bar: 123 };
assert.isUndefined(getOwn(obj, 'hasOwnProperty'));
const person = new Person(1880);
assert.isUndefined(getOwn(person, 'getAge'));
});
it('returns own properties', () => {
const obj: Record<string, number> = { 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<string, unknown> = {
foo: 123,
hasOwnProperty: () => true,
};
assert.strictEqual(getOwn(obj, 'foo'), 123);
assert.isUndefined(getOwn(obj, 'bar'));
});
});

13
ts/util/getOwn.ts Normal file
View File

@ -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<TObject extends object, TKey extends keyof TObject>(
obj: TObject,
key: TKey
): TObject[TKey] | undefined {
return has(obj, key) ? obj[key] : undefined;
}