0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 09:33:54 +01:00

Create a util hasOwn to simplify object property checks

This commit is contained in:
Lovelyfin00 2022-11-09 16:07:35 +01:00 committed by LB (Ben Johnston)
parent cafc2130af
commit 67c7f82de5
2 changed files with 29 additions and 0 deletions

View File

@ -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);
});
});

View File

@ -0,0 +1,8 @@
/**
* Returns true if the specified object has the
* indicated property as its own property.
*/
const hasOwn = (object: Record<string, unknown>, key: string) =>
object ? Object.prototype.hasOwnProperty.call(object, key) : false;
export { hasOwn };