1
1
import setIn from 'lodash/fp/set' ;
2
2
import get from 'lodash/get' ;
3
- import { InternalNamePath , NamePath , Store , StoreValue , EventArgs } from '../interface' ;
3
+ import {
4
+ InternalNamePath ,
5
+ NamePath ,
6
+ Store ,
7
+ StoreValue ,
8
+ EventArgs ,
9
+ } from '../interface' ;
4
10
import { toArray } from './typeUtil' ;
5
11
6
12
/**
@@ -19,12 +25,19 @@ export function getValue(store: Store, namePath: InternalNamePath) {
19
25
return value ;
20
26
}
21
27
22
- export function setValue ( store : Store , namePath : InternalNamePath , value : StoreValue ) : Store {
28
+ export function setValue (
29
+ store : Store ,
30
+ namePath : InternalNamePath ,
31
+ value : StoreValue ,
32
+ ) : Store {
23
33
const newStore = setIn ( namePath , value , store ) ;
24
34
return newStore ;
25
35
}
26
36
27
- export function cloneByNamePathList ( store : Store , namePathList : InternalNamePath [ ] ) : Store {
37
+ export function cloneByNamePathList (
38
+ store : Store ,
39
+ namePathList : InternalNamePath [ ] ,
40
+ ) : Store {
28
41
let newStore = { } ;
29
42
namePathList . forEach ( namePath => {
30
43
const value = getValue ( store , namePath ) ;
@@ -34,12 +47,21 @@ export function cloneByNamePathList(store: Store, namePathList: InternalNamePath
34
47
return newStore ;
35
48
}
36
49
37
- export function containsNamePath ( namePathList : InternalNamePath [ ] , namePath : InternalNamePath ) {
38
- return namePathList && namePathList . some ( path => matchNamePath ( path , namePath ) ) ;
50
+ export function containsNamePath (
51
+ namePathList : InternalNamePath [ ] ,
52
+ namePath : InternalNamePath ,
53
+ ) {
54
+ return (
55
+ namePathList && namePathList . some ( path => matchNamePath ( path , namePath ) )
56
+ ) ;
39
57
}
40
58
41
59
function isObject ( obj : StoreValue ) {
42
- return typeof obj === 'object' && obj !== null ;
60
+ return (
61
+ typeof obj === 'object' &&
62
+ obj !== null &&
63
+ Object . getPrototypeOf ( obj ) === Object . prototype
64
+ ) ;
43
65
}
44
66
45
67
/**
@@ -58,8 +80,10 @@ function internalSetValues<T>(store: T, values: T): T {
58
80
const value = values [ key ] ;
59
81
60
82
// If both are object (but target is not array), we use recursion to set deep value
61
- const recursive = isObject ( prevValue ) && isObject ( value ) && ! Array . isArray ( value ) ;
62
- newStore [ key ] = recursive ? internalSetValues ( prevValue , value || { } ) : value ;
83
+ const recursive = isObject ( prevValue ) && isObject ( value ) ;
84
+ newStore [ key ] = recursive
85
+ ? internalSetValues ( prevValue , value || { } )
86
+ : value ;
63
87
} ) ;
64
88
65
89
return newStore ;
@@ -76,7 +100,11 @@ export function matchNamePath(
76
100
namePath : InternalNamePath ,
77
101
changedNamePath : InternalNamePath | null ,
78
102
) {
79
- if ( ! namePath || ! changedNamePath || namePath . length !== changedNamePath . length ) {
103
+ if (
104
+ ! namePath ||
105
+ ! changedNamePath ||
106
+ namePath . length !== changedNamePath . length
107
+ ) {
80
108
return false ;
81
109
}
82
110
return namePath . every ( ( nameUnit , i ) => changedNamePath [ i ] === nameUnit ) ;
@@ -93,7 +121,12 @@ export function isSimilar(source: SimilarObject, target: SimilarObject) {
93
121
return false ;
94
122
}
95
123
96
- if ( ! source || ! target || typeof source !== 'object' || typeof target !== 'object' ) {
124
+ if (
125
+ ! source ||
126
+ ! target ||
127
+ typeof source !== 'object' ||
128
+ typeof target !== 'object'
129
+ ) {
97
130
return false ;
98
131
}
99
132
@@ -105,14 +138,20 @@ export function isSimilar(source: SimilarObject, target: SimilarObject) {
105
138
const sourceValue = source [ key ] ;
106
139
const targetValue = target [ key ] ;
107
140
108
- if ( typeof sourceValue === 'function' && typeof targetValue === 'function' ) {
141
+ if (
142
+ typeof sourceValue === 'function' &&
143
+ typeof targetValue === 'function'
144
+ ) {
109
145
return true ;
110
146
}
111
147
return sourceValue === targetValue ;
112
148
} ) ;
113
149
}
114
150
115
- export function defaultGetValueFromEvent ( valuePropName : string , ...args : EventArgs ) {
151
+ export function defaultGetValueFromEvent (
152
+ valuePropName : string ,
153
+ ...args : EventArgs
154
+ ) {
116
155
const event = args [ 0 ] ;
117
156
if ( event && event . target && valuePropName in event . target ) {
118
157
return ( event . target as HTMLInputElement ) [ valuePropName ] ;
@@ -133,7 +172,12 @@ export function defaultGetValueFromEvent(valuePropName: string, ...args: EventAr
133
172
*/
134
173
export function move < T > ( array : T [ ] , moveIndex : number , toIndex : number ) {
135
174
const { length } = array ;
136
- if ( moveIndex < 0 || moveIndex >= length || toIndex < 0 || toIndex >= length ) {
175
+ if (
176
+ moveIndex < 0 ||
177
+ moveIndex >= length ||
178
+ toIndex < 0 ||
179
+ toIndex >= length
180
+ ) {
137
181
return array ;
138
182
}
139
183
const item = array [ moveIndex ] ;
0 commit comments