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:
parent
cafc2130af
commit
67c7f82de5
21
client/src/utils/hasOwn.test.js
Normal file
21
client/src/utils/hasOwn.test.js
Normal 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);
|
||||
});
|
||||
});
|
8
client/src/utils/hasOwn.ts
Normal file
8
client/src/utils/hasOwn.ts
Normal 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 };
|
Loading…
Reference in New Issue
Block a user