File tree Expand file tree Collapse file tree 2 files changed +66
-1
lines changed
Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
22
33function undefsafe ( obj , path , value ) {
4- var parts = path . split ( '.' ) ;
4+
5+ // I'm not super keen on this private function, but it's because
6+ // it'll also be use in the browser and I wont *one* function exposed
7+ function split ( path ) {
8+ var res = [ ] ;
9+ var level = 0 ;
10+ var key = '' ;
11+
12+ for ( var i = 0 ; i < path . length ; i ++ ) {
13+ var c = path . substr ( i , 1 ) ;
14+
15+ if ( level === 0 && ( c === '.' || c === '[' ) ) {
16+ if ( c === '[' ) {
17+ level ++ ;
18+ i ++ ;
19+ c = path . substr ( i , 1 ) ;
20+ }
21+ res . push ( key ) ;
22+ key = '' ;
23+ continue ;
24+ }
25+
26+ if ( c === ']' ) {
27+ level -- ;
28+ key = key . slice ( 0 , - 1 ) ;
29+ continue ;
30+ }
31+
32+ key += c ;
33+ }
34+
35+ res . push ( key ) ;
36+
37+ return res ;
38+ }
39+
40+ var parts = split ( path ) ;
541 var key = null ;
642 var type = typeof obj ;
743 var root = obj ;
Original file line number Diff line number Diff line change @@ -41,6 +41,35 @@ test('should handle null properties', function (t) {
4141 t . end ( ) ;
4242} ) ;
4343
44+ test ( 'should find properties with periods in them' , function ( t ) {
45+ var value = {
46+ a : { 'one.two' : true }
47+ } ;
48+
49+ var r = undefsafe ( value , 'a["one.two"]' ) ;
50+ t . equal ( r , true , 'a["one.two"]: ' + r ) ;
51+
52+ value = {
53+ a : { 'one.two.and\three' : true }
54+ } ;
55+
56+ r = undefsafe ( value , `a['one.two.and\three']` ) ;
57+ t . equal ( r , true , 'weird: ' + r ) ;
58+
59+ value = {
60+ a : { 'one.two.and\three' : [
61+ false ,
62+ true ,
63+ ] }
64+ } ;
65+
66+ r = undefsafe ( value , `a['one.two.and\three'].1` ) ;
67+ t . equal ( r , true , 'combo: ' + r ) ;
68+
69+ t . end ( ) ;
70+ } ) ;
71+
72+
4473test ( 'should find deep object properties' , function ( t ) {
4574 var value = {
4675 a : {
You can’t perform that action at this time.
0 commit comments