@@ -28,8 +28,8 @@ function cursorFrom(rootData, keyPath, onChange) {
28
28
} else if ( typeof keyPath === 'function' ) {
29
29
onChange = keyPath ;
30
30
keyPath = [ ] ;
31
- } else if ( ! Array . isArray ( keyPath ) ) {
32
- keyPath = [ keyPath ] ;
31
+ } else {
32
+ keyPath = valToKeyPath ( keyPath ) ;
33
33
}
34
34
return makeCursor ( rootData , keyPath , onChange ) ;
35
35
}
@@ -74,20 +74,18 @@ IndexedCursorPrototype.get = function(key, notSetValue) {
74
74
}
75
75
76
76
KeyedCursorPrototype . getIn =
77
- IndexedCursorPrototype . getIn = function ( key , notSetValue ) {
78
- if ( ! Array . isArray ( key ) ) {
79
- key = Immutable . Iterable ( key ) . toArray ( ) ;
80
- }
81
- if ( key . length === 0 ) {
77
+ IndexedCursorPrototype . getIn = function ( keyPath , notSetValue ) {
78
+ keyPath = listToKeyPath ( keyPath ) ;
79
+ if ( keyPath . length === 0 ) {
82
80
return this ;
83
81
}
84
- var value = this . _rootData . getIn ( newKeyPath ( this . _keyPath , key ) , NOT_SET ) ;
85
- return value === NOT_SET ? notSetValue : wrappedValue ( this , key , value ) ;
82
+ var value = this . _rootData . getIn ( newKeyPath ( this . _keyPath , keyPath ) , NOT_SET ) ;
83
+ return value === NOT_SET ? notSetValue : wrappedValue ( this , keyPath , value ) ;
86
84
}
87
85
88
86
IndexedCursorPrototype . set =
89
87
KeyedCursorPrototype . set = function ( key , value ) {
90
- return updateCursor ( this , function ( m ) { return m . set ( key , value ) ; } , key ) ;
88
+ return updateCursor ( this , function ( m ) { return m . set ( key , value ) ; } , [ key ] ) ;
91
89
}
92
90
93
91
IndexedCursorPrototype . setIn =
@@ -97,7 +95,7 @@ KeyedCursorPrototype.remove =
97
95
KeyedCursorPrototype [ 'delete' ] =
98
96
IndexedCursorPrototype . remove =
99
97
IndexedCursorPrototype [ 'delete' ] = function ( key ) {
100
- return updateCursor ( this , function ( m ) { return m . remove ( key ) ; } , key ) ;
98
+ return updateCursor ( this , function ( m ) { return m . remove ( key ) ; } , [ key ] ) ;
101
99
}
102
100
103
101
IndexedCursorPrototype . removeIn =
@@ -170,9 +168,9 @@ IndexedCursorPrototype.withMutations = function(fn) {
170
168
}
171
169
172
170
KeyedCursorPrototype . cursor =
173
- IndexedCursorPrototype . cursor = function ( subKey ) {
174
- return Array . isArray ( subKey ) && subKey . length === 0 ?
175
- this : subCursor ( this , subKey ) ;
171
+ IndexedCursorPrototype . cursor = function ( subKeyPath ) {
172
+ subKeyPath = valToKeyPath ( subKeyPath ) ;
173
+ return subKeyPath . length === 0 ? this : subCursor ( this , subKeyPath ) ;
176
174
}
177
175
178
176
/**
@@ -183,7 +181,7 @@ IndexedCursorPrototype.__iterate = function(fn, reverse) {
183
181
var cursor = this ;
184
182
var deref = cursor . deref ( ) ;
185
183
return deref && deref . __iterate ? deref . __iterate (
186
- function ( v , k ) { return fn ( wrappedValue ( cursor , k , v ) , k , cursor ) ; } ,
184
+ function ( v , k ) { return fn ( wrappedValue ( cursor , [ k ] , v ) , k , cursor ) ; } ,
187
185
reverse
188
186
) : 0 ;
189
187
}
@@ -207,7 +205,7 @@ IndexedCursorPrototype.__iterator = function(type, reverse) {
207
205
}
208
206
var entry = step . value ;
209
207
var k = entry [ 0 ] ;
210
- var v = wrappedValue ( cursor , k , entry [ 1 ] ) ;
208
+ var v = wrappedValue ( cursor , [ k ] , entry [ 1 ] ) ;
211
209
return {
212
210
value : type === Iterator . KEYS ? k : type === Iterator . VALUES ? v : [ k , v ] ,
213
211
done : false
@@ -230,31 +228,32 @@ function makeCursor(rootData, keyPath, onChange, value) {
230
228
return new CursorClass ( rootData , keyPath , onChange , size ) ;
231
229
}
232
230
233
- function wrappedValue ( cursor , key , value ) {
234
- return Iterable . isIterable ( value ) ? subCursor ( cursor , key , value ) : value ;
231
+ function wrappedValue ( cursor , keyPath , value ) {
232
+ return Iterable . isIterable ( value ) ? subCursor ( cursor , keyPath , value ) : value ;
235
233
}
236
234
237
- function subCursor ( cursor , key , value ) {
235
+ function subCursor ( cursor , keyPath , value ) {
238
236
return makeCursor (
239
237
cursor . _rootData ,
240
- newKeyPath ( cursor . _keyPath , key ) ,
238
+ newKeyPath ( cursor . _keyPath , keyPath ) ,
241
239
cursor . _onChange ,
242
240
value
243
241
) ;
244
242
}
245
243
246
- function updateCursor ( cursor , changeFn , changeKey ) {
244
+ function updateCursor ( cursor , changeFn , changeKeyPath ) {
245
+ var deepChange = arguments . length > 2 ;
247
246
var newRootData = cursor . _rootData . updateIn (
248
247
cursor . _keyPath ,
249
- changeKey ? Map ( ) : undefined ,
248
+ deepChange ? Map ( ) : undefined ,
250
249
changeFn
251
250
) ;
252
251
var keyPath = cursor . _keyPath || [ ] ;
253
252
var result = cursor . _onChange && cursor . _onChange . call (
254
253
undefined ,
255
254
newRootData ,
256
255
cursor . _rootData ,
257
- arguments . length > 2 ? newKeyPath ( keyPath , changeKey ) : keyPath
256
+ deepChange ? newKeyPath ( keyPath , changeKeyPath ) : keyPath
258
257
) ;
259
258
if ( result !== undefined ) {
260
259
newRootData = result ;
@@ -263,7 +262,17 @@ function updateCursor(cursor, changeFn, changeKey) {
263
262
}
264
263
265
264
function newKeyPath ( head , tail ) {
266
- return Seq ( head ) . concat ( tail ) . toArray ( ) ;
265
+ return head . concat ( listToKeyPath ( tail ) ) ;
266
+ }
267
+
268
+ function listToKeyPath ( list ) {
269
+ return Array . isArray ( list ) ? list : Immutable . Iterable ( list ) . toArray ( ) ;
270
+ }
271
+
272
+ function valToKeyPath ( val ) {
273
+ return Array . isArray ( val ) ? val :
274
+ Iterable . isIterable ( val ) ? val . toArray ( ) :
275
+ [ val ] ;
267
276
}
268
277
269
278
exports . from = cursorFrom ;
0 commit comments