import { FunctionArguments } from '../types' ; import _ from 'lodash-es' ; import { isFunction } from './assertion' ; export function runIfFn < T , U > ( valueOrFn , ... args ) { return isFunction ( valueOrFn ) ? valueOrFn ( ... args ) : valueOrFn ; } export function callAllHandlers < T extends ( event : any ) => void > ( ... fns ) { return function func ( event ) { fns . some ( ( fn ) => { fn ?. ( event ) ; return event ?. defaultPrevented ; } ) ; } ; } export function callAll ( ... fns ) { return function mergedFn ( ... args ) { fns . forEach ( ( fn ) => { if ( isFunction ( fn ) ) { fn ?. ( ... args ) ; } } ) ; } ; } export function noop ( ... args ) { } interface MessageOptions { condition : boolean ; message : string ; } export const warn = ( options ) => { const { condition , message } = options ; if ( condition ) { console . warn ( message ) ; } } ; export const error = ( options ) => { const { condition , message } = options ; if ( condition ) { console . error ( message ) ; } } ; export function pick < T extends object , U extends keyof T > ( object , props ) { return _ . pick ( object , props ) ; } export function omit < T extends object , U extends keyof T > ( object , props ) { return _ . omit ( object , props ) ; } /**
 * Performs equality by iterating through keys on an object and returning false
 * when any key has values which are not strictly equal between the arguments.
 * Returns true when the values of all keys are strictly equal.
 *
 * 这个函数是从 facebook 某个官方库中复制过来的
 */ export function shallowEqual < T > ( objA , objB ) { const hasOwnProperty = Object . prototype . hasOwnProperty ; if ( Object . is ( objA , objB ) ) { return true ; } if ( typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null ) { return false ; } const keysA = Object . keys ( objA ) ; const keysB = Object . keys ( objB ) ; if ( keysA . length !== keysB . length ) { return false ; } for ( let i = 0 ; i < keysA . length ; i ++ ) { if ( ! hasOwnProperty . call ( objB , keysA [ i ] ) || ! Object . is ( objA [ keysA [ i ] ] , objB [ keysA [ i ] ] ) ) { return false ; } } return true ; }