All files / lib dispatcher.js

30.61% Statements 15/49
20% Branches 3/15
9.09% Functions 1/11
32.61% Lines 15/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1052x 2x 2x 2x 2x 2x   2x   2x                                                                                                                                                       2x 7x 7x   7x   7x             7x     2x      
const convertShapeToPath = require('./filter/convertShapeToPath').fn
const removeGroups = require('./filter/removeGroups').fn
const viewBoxTransform = require('./filter/viewBoxTransform').fn
const convertTransfromforPath = require('./filter/convertTransfromforPath').fn
const removeGradient = require('./filter/removeGradient').fn
const convertUseToGroup = require('./filter/convertUseToGroup').fn
 
let defaultSize = 1024 // 默认转换 viewbox尺寸
 
const pluginList = {
  convertUseToGroup: (ctx, next) => {
    convertUseToGroup(ctx)
    next()
  },
  convertShapeToPath: (ctx, next) => {
    convertShapeToPath(ctx)
    next()
  },
  removeGroups: (ctx, next) => {
    removeGroups(ctx)
    next()
  },
  viewBoxTransform: (ctx, next) => {
    viewBoxTransform(ctx, defaultSize)
    next()
  },
  convertTransfromforPath: (ctx, next) => {
    convertTransfromforPath(ctx)
    next()
  },
  removeGradient: (ctx, next) => {
    removeGradient(ctx)
    next()
  },
}
 
/** 插件分发器
 * inspire https://github.com/koajs/compose
 * Compose `middleware` returning
 * a fully valid middleware comprised
 * of all those which are passed.
 *
 * @param {Array} middleware 插件中间件队列
 * @return {Function}
 * @api public
 * @todo compose([a, b, c, ...])
 */
function Compose (middleware) {
  if (!Array.isArray(middleware)) {
    throw new TypeError('Middleware stack must be an array!')
  }
  for (const fn of middleware) {
    if (typeof fn !== 'function') {
      throw new TypeError('Middleware must be composed of functions!')
    }
  }
 
  return function (context, next) {
    // last called middleware #
    // eslint-disable-next-line no-unused-vars
    let index = -1
    return dispatch(0)
    function dispatch (i) {
      index = i
      let fn = middleware[i]
      if (i === middleware.length) {
        fn = next
      }
      if (!fn) return
      try {
        return fn(context, dispatch.bind(null, i + 1))
      } catch (err) {
        // log
        console.error(err)
        return dispatch.bind(null, i + 1)()
      }
    }
  }
}
 
/**
 * 插件解析分发机制 Dispatcher
 * convertShapeToPath 基本图形转换为 path
 * removeGroups 基本图形转换为 path
 */
const Dispatcher = (Doc, { plugins = [], size }) => {
  defaultSize = 1024 // 重置默认转换 viewbox尺寸
  Iif (size) defaultSize = size
  // 插件队列
  const middleware = []
 
  plugins.map(item => {
    if (Object.values(item)[0]) {
      middleware.push(pluginList[Object.keys(item)])
    }
  })
 
  // 执行插件分发器
  Iif (middleware.length > 0) Compose(middleware)(Doc)
}
 
module.exports = {
  Dispatcher,
}