diff --git a/client/src/utils/hasOwn.test.js b/client/src/utils/hasOwn.test.js new file mode 100644 index 0000000000..9350c9d6bd --- /dev/null +++ b/client/src/utils/hasOwn.test.js @@ -0,0 +1,21 @@ +import { hasOwn } from './hasOwn'; + +describe('hasOwn', () => { + it('should return false when not provided an object', () => { + expect(hasOwn()).toEqual(false); + expect(hasOwn(null)).toEqual(false); + expect(hasOwn([])).toEqual(false); + expect(hasOwn(undefined)).toEqual(false); + }); + + it('should return false if the object does not have the key', () => { + expect(hasOwn({}, 'a')).toEqual(false); + expect(hasOwn({ bb: true }, 'a')).toEqual(false); + expect(hasOwn({ AA: 'something' }, 'aa')).toEqual(false); + }); + + it('should return true if the object does have the key', () => { + expect(hasOwn({ bb: true }, 'bb')).toEqual(true); + expect(hasOwn({ AA: 'something' }, 'AA')).toEqual(true); + }); +}); diff --git a/client/src/utils/hasOwn.ts b/client/src/utils/hasOwn.ts new file mode 100644 index 0000000000..00759dd7f8 --- /dev/null +++ b/client/src/utils/hasOwn.ts @@ -0,0 +1,8 @@ +/** + * Returns true if the specified object has the + * indicated property as its own property. + */ +const hasOwn = (object: Record, key: string) => + object ? Object.prototype.hasOwnProperty.call(object, key) : false; + +export { hasOwn };