@@ -6,13 +6,14 @@ export interface BeautifierConfig {
66}
77
88export default class Beautifier {
9- private beautificationMap : Record < string , Record < string , any > > ;
9+ private beautificationMap : Record < string , Record < string , any > | string > =
10+ BEAUTIFIER_EVENT_MAP ;
1011
1112 private floatKeys : string [ ] ;
1213
1314 private floatKeysHashMap : Record < string , boolean > ;
1415
15- private config : BeautifierConfig | undefined ;
16+ private config : BeautifierConfig ;
1617
1718 constructor ( config : BeautifierConfig ) {
1819 this . config = config ;
@@ -158,11 +159,14 @@ export default class Beautifier {
158159 this . floatKeys . forEach ( ( keyName ) => {
159160 this . floatKeysHashMap [ keyName ] = true ;
160161 } ) ;
162+ }
161163
162- this . beautificationMap = BEAUTIFIER_EVENT_MAP ;
164+ setWarnIfMissing ( value : boolean ) {
165+ this . config . warnKeyMissingInMap = value ;
163166 }
164167
165168 beautifyValueWithKey ( key : string | number , val : unknown ) {
169+ // console.log('beautifier.beautifyValueWithKey()', { key, val });
166170 if ( typeof val === 'string' && this . floatKeysHashMap [ key ] && val !== '' ) {
167171 const result = parseFloat ( val ) ;
168172 if ( isNaN ( result ) ) {
@@ -177,6 +181,7 @@ export default class Beautifier {
177181 * Beautify array or object, recurisvely
178182 */
179183 beautifyObjectValues ( data : any | any [ ] ) {
184+ // console.log('beautifier.beautifyObjectValues()', { data });
180185 if ( Array . isArray ( data ) ) {
181186 return this . beautifyArrayValues ( data ) ;
182187 }
@@ -197,6 +202,7 @@ export default class Beautifier {
197202 }
198203
199204 beautifyArrayValues ( data : any [ ] , parentKey ?: string | number ) {
205+ // console.log('beautifier.beautifyArrayValues()', { data, parentKey });
200206 const beautifedArray : any [ ] = [ ] ;
201207 for ( const [ key , val ] of data . entries ( ) ) {
202208 const type = typeof val ;
@@ -212,7 +218,6 @@ export default class Beautifier {
212218 }
213219
214220 beautify ( data : any , key ?: string | number ) {
215- // console.log('beautify()', { key });
216221 if ( typeof key !== 'string' && typeof key !== 'number' ) {
217222 console . warn (
218223 `beautify(object, ${ key } ) is not valid key - beautification failed ` ,
@@ -223,6 +228,7 @@ export default class Beautifier {
223228 }
224229
225230 const knownBeautification = this . beautificationMap [ key ] ;
231+ // console.log('beautify: ', { key, knownBeautification }, data);
226232 if ( ! knownBeautification ) {
227233 const valueType = typeof data ;
228234 const isPrimitive =
@@ -251,18 +257,21 @@ export default class Beautifier {
251257 }
252258
253259 const newItem = { } ;
254- for ( const key in data ) {
255- const value = data [ key ] ;
260+ for ( const propertyKey in data ) {
261+ const value = data [ propertyKey ] ;
256262 const valueType = typeof value ;
257263
258- let newKey = knownBeautification [ key ] || key ;
264+ let newKey = knownBeautification [ propertyKey ] || propertyKey ;
259265 if ( Array . isArray ( newKey ) ) {
260266 newKey = newKey [ 0 ] ;
261267 }
262268
263269 if ( ! Array . isArray ( value ) ) {
264270 if ( valueType === 'object' && value !== null ) {
265- newItem [ newKey ] = this . beautify ( value , knownBeautification [ key ] ) ;
271+ newItem [ newKey ] = this . beautify (
272+ value ,
273+ knownBeautification [ propertyKey ] ,
274+ ) ;
266275 } else {
267276 newItem [ newKey ] = this . beautifyValueWithKey ( newKey , value ) ;
268277 }
@@ -271,10 +280,43 @@ export default class Beautifier {
271280
272281 const newArray : any [ ] = [ ] ;
273282 if ( Array . isArray ( this . beautificationMap [ newKey ] ) ) {
283+ // console.log('beautify().isArray(): ', {
284+ // newKey,
285+ // arrayFromMap: this.beautificationMap[newKey],
286+ // });
274287 for ( const elementValue of value ) {
275288 const mappedBeautification =
276- this . beautificationMap [ knownBeautification [ key ] ] ;
289+ this . beautificationMap [ knownBeautification [ propertyKey ] ] ;
290+
291+ // console.log('mapped meautification: ', {
292+ // knownBeautification: knownBeautification[propertyKey],
293+ // mappedBeautification,
294+ // key,
295+ // subKey: propertyKey,
296+ // newKey,
297+ // });
298+
299+ if ( ! mappedBeautification ) {
300+ // console.warn(
301+ // `Beautifier(): found map for "${key}" but property with array ("${propertyKey}") is missing in map: `,
302+ // {
303+ // eventMapKey: key,
304+ // propertyKey: propertyKey,
305+ // elementValue,
306+ // knownBeautification,
307+ // value,
308+ // // beautfTest1: this.beautify(value, propertyKey),
309+ // },
310+ // );
311+ newArray . push ( elementValue ) ;
312+
313+ continue ;
314+ }
277315 const childMapping = mappedBeautification [ 0 ] ;
316+ // const childMapping =
317+ // typeof mappedBeautification === 'string' // Pointer to another key
318+ // ? this.beautificationMap[mappedBeautification]
319+ // : mappedBeautification[0];
278320
279321 if ( typeof childMapping === 'object' && childMapping !== null ) {
280322 const mappedResult = { } ;
0 commit comments