首页 > 其他 > 详细

合并策略例子

时间:2020-05-24 17:31:21      阅读:47      评论:0      收藏:0      [点我收藏+]
import { AxiosRequestConfig } from ‘../types‘

export default function mergeConfig(
  config1: AxiosRequestConfig,
  config2: AxiosRequestConfig
): AxiosRequestConfig {
  const config = Object.create(null)
  if (!config2) {
    config2 = {}
  }

  for (let key in config2) {
    mergeField(key)
  }

  function mergeField(key: string): void {
    const strat = strats[key] || defaultStatus
    config[key] = strat(config1[key], config2![key])
  }
  return config
}

// 合并策略函数
// 默认
function defaultStatus(val1: any, val2: any) {
  return typeof val2 !== ‘undefined‘ ? val2 : val1
}
// 只接受自定义配置
function fromVal2Strat(val1: any, val2: any) {
  if (typeof val2 !== ‘undefined‘) {
    return val2
  }
}

const strats = Object.create(null)
const stratKeysFromVal2 = [‘url‘, ‘params‘, ‘data‘]
stratKeysFromVal2.forEach(key => {
  strats[key] = fromVal2Strat
})

 

合并策略例子

原文:https://www.cnblogs.com/TTblog5/p/12951638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!