@@ -79,4 +79,68 @@ describe('get', () => {
7979 expect ( get ( obj , 'a.b.c.d.e.f' ) ) . toBe ( 'deep' ) ;
8080 expect ( get ( obj , [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] ) ) . toBe ( 'deep' ) ;
8181 } ) ;
82+
83+ it ( 'returns value at number path' , ( ) => {
84+ const arr = [ 'zero' , 'one' , 'two' ] ;
85+ expect ( get ( arr , 0 ) ) . toBe ( 'zero' ) ;
86+ expect ( get ( arr , 1 ) ) . toBe ( 'one' ) ;
87+ expect ( get ( arr , 2 ) ) . toBe ( 'two' ) ;
88+ } ) ;
89+
90+ it ( 'returns defaultValue when number path does not exist' , ( ) => {
91+ const arr = [ 'zero' , 'one' ] ;
92+ expect ( get ( arr , 5 , 'default' ) ) . toBe ( 'default' ) ;
93+ } ) ;
94+
95+ it ( 'handles bracket notation with numeric indices' , ( ) => {
96+ const obj = { a : [ { b : 1 } , { b : 2 } ] } ;
97+ expect ( get ( obj , 'a[0].b' ) ) . toBe ( 1 ) ;
98+ expect ( get ( obj , 'a[1].b' ) ) . toBe ( 2 ) ;
99+ } ) ;
100+
101+ it ( 'handles bracket notation with double-quoted keys' , ( ) => {
102+ const obj = { a : { 'special-key' : 'value1' , 'another.key' : 'value2' } } ;
103+ expect ( get ( obj , 'a["special-key"]' ) ) . toBe ( 'value1' ) ;
104+ expect ( get ( obj , 'a["another.key"]' ) ) . toBe ( 'value2' ) ;
105+ } ) ;
106+
107+ it ( 'handles bracket notation with single-quoted keys' , ( ) => {
108+ const obj = { a : { 'special-key' : 'value1' , 'another.key' : 'value2' } } ;
109+ expect ( get ( obj , "a['special-key']" ) ) . toBe ( 'value1' ) ;
110+ expect ( get ( obj , "a['another.key']" ) ) . toBe ( 'value2' ) ;
111+ } ) ;
112+
113+ it ( 'handles mixed dot and bracket notation' , ( ) => {
114+ const obj = { a : [ { b : { 'c-d' : [ 1 , 2 , 3 ] } } ] } ;
115+ expect ( get ( obj , 'a[0].b["c-d"][2]' ) ) . toBe ( 3 ) ;
116+ } ) ;
117+
118+ it ( 'handles bracket notation at the start of path' , ( ) => {
119+ const obj = { 0 : 'zero' , 'special-key' : 'special' } ;
120+ expect ( get ( obj , '[0]' ) ) . toBe ( 'zero' ) ;
121+ expect ( get ( obj , '["special-key"]' ) ) . toBe ( 'special' ) ;
122+ } ) ;
123+
124+ it ( 'handles consecutive bracket notations' , ( ) => {
125+ const obj = {
126+ a : [
127+ [ 1 , 2 ] ,
128+ [ 3 , 4 ] ,
129+ ] ,
130+ } ;
131+ expect ( get ( obj , 'a[0][1]' ) ) . toBe ( 2 ) ;
132+ expect ( get ( obj , 'a[1][0]' ) ) . toBe ( 3 ) ;
133+ } ) ;
134+
135+ it ( 'returns defaultValue for invalid bracket notation paths' , ( ) => {
136+ const obj = { a : { b : 1 } } ;
137+ expect ( get ( obj , 'a[0]' , 'default' ) ) . toBe ( 'default' ) ;
138+ expect ( get ( obj , 'a["nonexistent"]' , 'default' ) ) . toBe ( 'default' ) ;
139+ } ) ;
140+
141+ it ( 'handles keys with special characters via bracket notation' , ( ) => {
142+ const obj = { 'key.with.dots' : 'dots' , 'key[with]brackets' : 'brackets' } ;
143+ expect ( get ( obj , '["key.with.dots"]' ) ) . toBe ( 'dots' ) ;
144+ expect ( get ( obj , '["key[with]brackets"]' ) ) . toBe ( 'brackets' ) ;
145+ } ) ;
82146} ) ;
0 commit comments