Skip to content

Commit 2c92dfc

Browse files
committed
Add proper object creator
1 parent b9313bd commit 2c92dfc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const isDate = d => d instanceof Date;
22
export const isEmpty = o => Object.keys(o).length === 0;
33
export const isObject = o => o != null && typeof o === 'object';
4+
export const properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o;

src/utils/index.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import forEach from 'jest-each';
22

3-
import { isDate, isEmpty, isObject } from './';
3+
import { isDate, isEmpty, isObject, properObject } from './';
44

55
describe('utils', () => {
66

@@ -72,4 +72,27 @@ describe('utils', () => {
7272
expect(isObject(value)).toBe(false);
7373
});
7474
});
75+
76+
describe('.properObject', () => {
77+
it('returns given object when object has keys and hasOwnProperty function', () => {
78+
const o = { a: 1 };
79+
const a = [1];
80+
expect(properObject(o)).toBe(o);
81+
expect(properObject(a)).toBe(a);
82+
});
83+
84+
it('returns given value when value is not an object', () => {
85+
const o = 'hello';
86+
expect(properObject(o)).toBe(o);
87+
});
88+
89+
it('returns object that has given keys and hasOwnProperty function when given object is created from a null', () => {
90+
const o = Object.create(null);
91+
o.a = 1;
92+
const actual = properObject(o);
93+
expect(actual).toEqual({ a: 1 });
94+
expect(typeof actual.hasOwnProperty === 'function').toBe(true);
95+
expect(actual).not.toBe(o);
96+
});
97+
});
7598
});

0 commit comments

Comments
 (0)