import _ from 'lodash' ; import { IAggConfig } from '../agg_config' ; import { BaseParamType } from './base' ; export class JsonParamType extends BaseParamType { constructor ( config ) { super ( config ) ; this . name = config . name || 'json' ; if ( ! config . write ) { this . write = ( aggConfig , output ) => { let paramJson ; const param = aggConfig . params [ this . name ] ; if ( ! param ) { return ; } try { paramJson = JSON . parse ( param ) ; } catch ( err ) { return ; } function filteredCombine ( srcA , srcB ) { function mergeObjs ( a , b ) { return _ ( a ) . keys ( ) . union ( _ . keys ( b ) ) . transform ( function ( dest , key ) { const val = compare ( a [ key ] , b [ key ] ) ; if ( val !== undefined ) dest [ key ] = val ; } , { } ) . value ( ) ; } function mergeArrays ( a , b ) { return _ . times ( Math . max ( a . length , b . length ) , function ( i ) { return compare ( a [ i ] , b [ i ] ) ; } ) ; } function compare ( a , b ) { if ( _ . isPlainObject ( a ) && _ . isPlainObject ( b ) ) return mergeObjs ( a , b ) ; if ( Array . isArray ( a ) && Array . isArray ( b ) ) return mergeArrays ( a , b ) ; if ( b === null ) return undefined ; if ( b !== undefined ) return b ; return a ; } return compare ( srcA , srcB ) ; } output . params = filteredCombine ( output . params , paramJson ) ; return ; } ; } } }