utils.js 905 字节
Newer Older
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
exports.chunk = function (arr, num) {
  const list = []
  let current = []

  for (const item of arr) {
    current.push(item);
    if (current.length === num) {
      list.push(current)
      current = []
    }
  }

  if (current.length) list.push(current)

  return list
}

exports.checkIsStaticTemplate = function (data = []) {
  let isStatic = data.length <= 0

  for (const template of data) {
    if (template.type === 'static') {
      isStatic = true
      break
    }
  }

  return isStatic
}

exports.parserDynamicField = function (templateData) {
  return templateData.reduce((res, template) => {
    if (/\{.*?\}/.test(template.value)) {
      const [collection, field] = template.value.replace(/\{|\}/g, '').split('.')
      if (!res[collection]) {
        res[collection] = []
      }
      res[collection].push(field)
    }
    return res
  }, {})
}