1+ import create from '@core-js/pure/es/object/create' ;
2+ import defineProperty from '@core-js/pure/es/object/define-property' ;
13import keysLength from '@core-js/pure/full/object/keys-length' ;
4+ import Symbol from '@core-js/pure/es/symbol' ;
25
36QUnit . test ( 'Object.keysLength' , assert => {
47 assert . isFunction ( keysLength ) ;
@@ -9,4 +12,48 @@ QUnit.test('Object.keysLength', assert => {
912 assert . same ( keysLength ( { } ) , 0 , 'Empty object' ) ;
1013 assert . throws ( ( ) => keysLength ( null ) , TypeError ) ;
1114 assert . throws ( ( ) => keysLength ( ) , TypeError ) ;
15+
16+ assert . same ( keysLength ( { a : 1 , [ Symbol ( 's' ) ] : 2 } ) , 1 , 'Ignores symbol keys' ) ;
17+
18+ const nonEnum = { a : 1 } ;
19+ defineProperty ( nonEnum , 'hidden' , {
20+ value : true ,
21+ enumerable : false ,
22+ } ) ;
23+ assert . same ( keysLength ( nonEnum ) , 1 , 'Ignores non-enumerable own properties' ) ;
24+
25+ const withAccessor = { } ;
26+ defineProperty ( withAccessor , 'foo' , {
27+ get ( ) { return 1 ; } ,
28+ enumerable : true ,
29+ } ) ;
30+ assert . same ( keysLength ( withAccessor ) , 1 , 'Counts enumerable accessors' ) ;
31+
32+ const withNonEnumAccessor = { } ;
33+ defineProperty ( withNonEnumAccessor , 'bar' , {
34+ get ( ) { return 1 ; } ,
35+ enumerable : false ,
36+ } ) ;
37+ assert . same ( keysLength ( withNonEnumAccessor ) , 0 , 'Ignores non-enumerable accessors' ) ;
38+
39+ const proto = { a : 1 , b : 2 } ;
40+ const obj = create ( proto ) ;
41+ obj . c = 3 ;
42+ defineProperty ( proto , 'd' , {
43+ value : 4 ,
44+ enumerable : true ,
45+ } ) ;
46+ assert . same ( keysLength ( obj ) , 1 , 'Does not count inherited properties' ) ;
47+
48+ const dict = { __proto__ : null } ;
49+ dict . x = 1 ;
50+ assert . same ( keysLength ( dict ) , 1 , 'Works with null-prototype objects' ) ;
51+
52+ assert . same ( keysLength ( 'abc' ) , 3 , 'String primitive indices are own enumerable keys' ) ;
53+ assert . same ( keysLength ( 42 ) , 0 , 'Number primitive has no enumerable string keys' ) ;
54+ assert . same ( keysLength ( true ) , 0 , 'Boolean primitive has no enumerable string keys' ) ;
55+
56+ const arr = [ 'a' ] ;
57+ arr [ 2 ] = 'b' ;
58+ assert . same ( keysLength ( arr ) , 2 , 'Array counts only present indices' ) ;
1259} ) ;
0 commit comments