/**
 * Copyright (c) 2016-2018 Dmitry Panyushkin
 * Available under MIT license
 */ import { EXTENDERS_KEY , extendObject , hasOwnProperty , objectForEach } from "./common-functions" ; type Extender = Object | Function ; interface ExtendersDictionary { [ propName ] : Extender [ ] ; } export function applyExtenders ( instance , key , target ) { const dictionary = instance [ EXTENDERS_KEY ] as ExtendersDictionary ; const extenders = dictionary && dictionary [ key as any ] ; if ( extenders ) { extenders . forEach ( ( extender ) => { const koExtender = extender instanceof Function ? extender . call ( instance ) : extender ; target = target . extend ( koExtender ) ; } ) ; } return target ; } export function defineExtenders ( prototype , key , extendersOrFactory ) { let dictionary = prototype [ EXTENDERS_KEY ] as ExtendersDictionary ; if ( ! hasOwnProperty ( prototype , EXTENDERS_KEY ) ) { prototype [ EXTENDERS_KEY ] = dictionary = extendObject ( { } , dictionary ) as ExtendersDictionary ; objectForEach ( dictionary , ( existingKey , extenders ) => { dictionary [ existingKey ] = [ ... extenders ] ; } ) ; } const currentExtenders = dictionary [ key as any ] || ( dictionary [ key as any ] = [ ] ) ; currentExtenders . push ( extendersOrFactory ) ; }