File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 1
1
export const isDate = d => d instanceof Date ;
2
2
export const isEmpty = o => Object . keys ( o ) . length === 0 ;
3
3
export const isObject = o => o != null && typeof o === 'object' ;
4
+ export const properObject = o => isObject ( o ) && ! o . hasOwnProperty ? { ...o } : o ;
Original file line number Diff line number Diff line change 1
1
import forEach from 'jest-each' ;
2
2
3
- import { isDate , isEmpty , isObject } from './' ;
3
+ import { isDate , isEmpty , isObject , properObject } from './' ;
4
4
5
5
describe ( 'utils' , ( ) => {
6
6
@@ -72,4 +72,27 @@ describe('utils', () => {
72
72
expect ( isObject ( value ) ) . toBe ( false ) ;
73
73
} ) ;
74
74
} ) ;
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
+ } ) ;
75
98
} ) ;
You can’t perform that action at this time.
0 commit comments