@@ -81,28 +81,29 @@ export function saveValue(value) {
81
81
return value ;
82
82
}
83
83
84
- export function cleanData ( data , deletionKeys = true ) {
85
- data = foundry . utils . flattenObject ( data ) ;
84
+ export function cleanData ( data , { inplace = false , deletionKeys = false , keepOthers = true , partial = false } ) {
85
+ const flatData = foundry . utils . flattenObject ( data ) ;
86
+ let newData = { } ;
86
87
87
- const newData = { } ;
88
-
89
- if ( deletionKeys ) {
90
- for ( const key of Object . keys ( DEFAULT_FLAGS ) . concat ( Object . keys ( data ) ) ) {
91
- if ( ! key . startsWith ( `flags.${ MODULE_ID } .` ) ) {
88
+ if ( deletionKeys || inplace ) {
89
+ for ( const key of ( partial ? [ ] : Object . keys ( DEFAULT_FLAGS ) ) . concat ( Object . keys ( flatData ) ) ) {
90
+ if ( ! ( key . startsWith ( `flags.${ MODULE_ID } .` ) && ! key . includes ( ".-=" ) ) ) {
92
91
continue ;
93
92
}
94
93
95
94
const split = key . split ( "." ) ;
96
95
97
- for ( let i = 1 ; i < split . length ; i ++ ) {
96
+ for ( let i = partial ? split . length - 1 : 1 ; i < split . length ; i ++ ) {
98
97
newData [ `${ split . slice ( 0 , i ) . join ( "." ) } .-=${ split [ i ] } ` ] = null ;
99
98
}
100
99
}
101
100
}
102
101
103
- for ( let [ key , value ] of Object . entries ( data ) ) {
104
- if ( ! key . startsWith ( `flags.${ MODULE_ID } .` ) ) {
105
- newData [ key ] = value ;
102
+ for ( let [ key , value ] of Object . entries ( flatData ) ) {
103
+ if ( ! ( key . startsWith ( `flags.${ MODULE_ID } .` ) && ! key . includes ( ".-=" ) ) ) {
104
+ if ( keepOthers && ! inplace ) {
105
+ newData [ key ] = value ;
106
+ }
106
107
107
108
continue ;
108
109
}
@@ -112,33 +113,44 @@ export function cleanData(data, deletionKeys = true) {
112
113
}
113
114
114
115
const defaultValue = DEFAULT_FLAGS [ key ] ;
116
+ const normalizeValue = value => {
117
+ value = value ?? null ;
118
+
119
+ if ( parseValue ( defaultValue ) ) {
120
+ value = saveValue ( value ) ;
121
+ } else if ( typeof value === "string" ) {
122
+ if ( ! value ) {
123
+ value = null ;
124
+ } else {
125
+ value = value . trim ( ) . toLowerCase ( ) ;
126
+ }
127
+ }
115
128
116
- value = value ?? null ;
129
+ return value ;
130
+ } ;
117
131
118
- if ( parseValue ( defaultValue ) ) {
119
- value = saveValue ( value ) ;
120
- } else if ( typeof value === "string" ) {
121
- if ( ! value ) {
122
- value = null ;
123
- } else {
124
- value = value . trim ( ) . toLowerCase ( ) ;
125
- }
132
+ if ( value instanceof Array ) {
133
+ value = value . map ( normalizeValue ) ;
134
+ } else {
135
+ value = normalizeValue ( value ) ;
126
136
}
127
137
128
- if ( value !== defaultValue && value !== null ) {
138
+ if ( value != null && value !== defaultValue && ! value . equals ?. ( defaultValue ) ) {
129
139
newData [ key ] = value ;
130
140
131
- if ( deletionKeys ) {
141
+ if ( deletionKeys || inplace ) {
132
142
const split = key . split ( "." ) ;
133
143
134
144
for ( let i = 1 ; i < split . length ; i ++ ) {
135
145
delete newData [ `${ split . slice ( 0 , i ) . join ( "." ) } .-=${ split [ i ] } ` ] ;
136
146
}
137
147
}
148
+ } else if ( ! deletionKeys ) {
149
+ newData [ key ] = foundry . utils . deepClone ( defaultValue ) ;
138
150
}
139
151
}
140
152
141
- if ( deletionKeys ) {
153
+ if ( deletionKeys || inplace ) {
142
154
for ( const key in newData ) {
143
155
if ( ! key . startsWith ( `flags.${ MODULE_ID } .` ) && ! key . startsWith ( `flags.-=${ MODULE_ID } ` ) ) {
144
156
continue ;
@@ -164,5 +176,21 @@ export function cleanData(data, deletionKeys = true) {
164
176
}
165
177
}
166
178
167
- return foundry . utils . expandObject ( Object . fromEntries ( Object . entries ( newData ) . sort ( ( a , b ) => b [ 0 ] . length - a [ 0 ] . length ) ) ) ;
179
+ newData = foundry . utils . expandObject ( Object . fromEntries ( Object . entries ( newData ) . sort ( ( a , b ) => b [ 0 ] . length - a [ 0 ] . length ) ) ) ;
180
+
181
+ if ( ! inplace ) {
182
+ return newData ;
183
+ }
184
+
185
+ foundry . utils . mergeObject ( data , newData , { performDeletions : true } ) ;
186
+
187
+ if ( deletionKeys ) {
188
+ foundry . utils . mergeObject ( data , newData ) ;
189
+ }
190
+
191
+ if ( ! keepOthers ) {
192
+ foundry . utils . filterObject ( data , foundry . utils . expandObject ( DEFAULT_FLAGS ) ) ;
193
+ }
194
+
195
+ return data ;
168
196
}
0 commit comments