File size: 764 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { mergeWith } from 'lodash';

/**
 * Merge handler for lodash.mergeWith
 *
 * Note that a return value of `undefined`
 * indicates to lodash that it should use
 * its normal merge algorithm.
 *
 * In this case, we want to merge keys if
 * they don't exists but when they do, we
 * prefer to concatenate lists instead of
 * overwriting them.
 * @param {?Array<Function>} left existing handlers
 * @param {Array<Function>} right new handlers to add
 * @returns {Array<Function>} combined handlers
 */
const concatHandlers = ( left, right ) =>
	Array.isArray( left ) ? left.concat( right ) : undefined;

export const mergeHandlers = ( ...handlers ) =>
	handlers.length > 1
		? mergeWith( Object.create( null ), ...handlers, concatHandlers )
		: handlers[ 0 ];