utils.js 864 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. exports.chunk = function (arr, num) {
  2. const list = []
  3. let current = []
  4. for (const item of arr) {
  5. current.push(item);
  6. if (current.length === num) {
  7. list.push(current)
  8. current = []
  9. }
  10. }
  11. if (current.length) list.push(current)
  12. return list
  13. }
  14. exports.checkIsStaticTemplate = function (data = []) {
  15. let isStatic = data.length <= 0
  16. for (const template of data) {
  17. if (template.type === 'static') {
  18. isStatic = true
  19. break
  20. }
  21. }
  22. return isStatic
  23. }
  24. exports.parserDynamicField = function (templateData) {
  25. return templateData.reduce((res, template) => {
  26. if (/\{.*?\}/.test(template.value)) {
  27. const [collection, field] = template.value.replace(/\{|\}/g, '').split('.')
  28. if (!res[collection]) {
  29. res[collection] = []
  30. }
  31. res[collection].push(field)
  32. }
  33. return res
  34. }, {})
  35. }