@@ -32,37 +32,54 @@ import FactoryMaker from '../../core/FactoryMaker.js';
3232
3333function ObjectIron ( mappers ) {
3434
35- function mergeValues ( parentItem , childItem ) {
36- for ( let name in parentItem ) {
37- if ( ! childItem . hasOwnProperty ( name ) ) {
38- childItem [ name ] = parentItem [ name ] ;
35+ function _conditionallyMapProperty ( exception , parentName , parentIsArray , parentEl , child , mergeFlag ) {
36+ let allowMapping = true ;
37+
38+ // check, if element matches an exception
39+ if ( exception ) {
40+ for ( const [ key , values ] of Object . entries ( exception ) ) {
41+ let attr = parentEl [ key ] ;
42+ if ( values . some ( v => attr . match ( v ) ) ) {
43+ allowMapping = false ;
44+ }
45+ }
46+ }
47+
48+ // apply mapping
49+ if ( allowMapping ) {
50+ if ( child [ parentName ] ) {
51+ // property already exists
52+ // check to see if we should merge
53+ if ( mergeFlag ) {
54+ if ( parentIsArray ) {
55+ child [ parentName ] . push ( parentEl ) ;
56+ }
57+ }
58+ } else {
59+ // just add the property
60+ if ( parentIsArray ) {
61+ child [ parentName ] = [ parentEl ] ;
62+ } else {
63+ child [ parentName ] = parentEl ;
64+ }
3965 }
4066 }
67+
4168 }
4269
43- function mapProperties ( properties , parent , child ) {
70+ function mapProperties ( properties , exceptions , parent , child ) {
4471 for ( let i = 0 , len = properties . length ; i < len ; ++ i ) {
4572 const property = properties [ i ] ;
4673
4774 if ( parent [ property . name ] ) {
48- if ( child [ property . name ] ) {
49- // check to see if we should merge
50- if ( property . merge ) {
51- const parentValue = parent [ property . name ] ;
52- const childValue = child [ property . name ] ;
53-
54- // complex objects; merge properties
55- if ( typeof parentValue === 'object' && typeof childValue === 'object' ) {
56- mergeValues ( parentValue , childValue ) ;
57- }
58- // simple objects; merge them together
59- else {
60- child [ property . name ] = parentValue + childValue ;
61- }
62- }
75+ const propertyParentElementArray = parent [ property . name ] ;
76+
77+ if ( Array . isArray ( propertyParentElementArray ) ) {
78+ propertyParentElementArray . forEach ( propParentEl => {
79+ _conditionallyMapProperty ( exceptions [ property . name ] , property . name , true , propParentEl , child , property . merge ) ;
80+ } ) ;
6381 } else {
64- // just add the property
65- child [ property . name ] = parent [ property . name ] ;
82+ _conditionallyMapProperty ( exceptions [ property . name ] , property . name , false , propertyParentElementArray , child , property . merge ) ;
6683 }
6784 }
6885 }
@@ -76,7 +93,7 @@ function ObjectIron(mappers) {
7693 if ( array ) {
7794 for ( let v = 0 , len2 = array . length ; v < len2 ; ++ v ) {
7895 const childNode = array [ v ] ;
79- mapProperties ( item . properties , node , childNode ) ;
96+ mapProperties ( item . properties , item . exceptions , node , childNode ) ;
8097 mapItem ( childItem , childNode ) ;
8198 }
8299 }
0 commit comments