{"size": 1361, "ext": "js", "max_stars_repo_path": "Xiao/src/util/text-parser.js", "max_stars_repo_name": "xwjie/VueStudyNote", "max_stars_repo_stars_event_min_datetime": "2018-01-03T16:10:30.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-21T03:47:16.000Z", "max_issues_repo_path": "Xiao/src/util/text-parser.js", "max_issues_repo_name": "xwjie/VueStudyNote", "max_issues_repo_issues_event_min_datetime": "2018-01-03T13:43:07.000Z", "max_issues_repo_issues_event_max_datetime": "2018-02-08T02:38:08.000Z", "max_forks_repo_path": "Xiao/src/util/text-parser.js", "max_forks_repo_name": "xwjie/VueStudyNote", "max_forks_repo_forks_event_min_datetime": "2018-02-07T12:42:39.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-26T02:39:49.000Z"}
{"max_stars_count": 75, "max_issues_count": 27, "max_forks_count": 10, "avg_line_length": 20.3134328358, "max_line_length": 96, "alphanum_fraction": 0.6024981631}

//D:\OutPut\VUE\vue\src\compiler\parser\text-parser.js
export function parseText(
  text: string,
  re: string
) {
  if (!re.test(text)) {
    return
  }
  const tokens = []
  let lastIndex = re.lastIndex = 0
  let match, index, tokenValue
  while ((match = re.exec(text))) {
    index = match.index

    // push text token
    if (index > lastIndex) {
      tokenValue = text.slice(lastIndex, index)
      tokens.push(JSON.stringify(tokenValue))
    }

    // tag token
    var exp = match[1].trim()
    tokens.push(createExpressStr(exp))

    lastIndex = index + match[0].length
  }

  if (lastIndex < text.length) {
    tokenValue = text.slice(lastIndex)
    tokens.push(JSON.stringify(tokenValue))
  }

  return {
    expression: tokens.join('+'),
  }
}

/**
 * 处理filter表达式
 * 如：message | capitalize | wrap('===')
 * @param {*} exp
 */
export function createExpressStr(exp: string): string {
  // 数组反转，然后递归生成嵌套函数调用表达式
  return wrapExpressStr(exp.split('|').reverse())
}

function wrapExpressStr(arr: Array<string>): string {
  let str = arr[0].trim()

  if (arr.length == 1) {
    return str
  }

  // 如果是有参数的filter, 如 wrap('===')
  let i = str.indexOf('(')

  if (i == -1) {
    return `_f("${str}")(` + wrapExpressStr(arr.slice(1)) + ')'
  } else {
    return `_f("${str.substr(0, i)}")(` + wrapExpressStr(arr.slice(1)) + ',' + str.substr(i + 1)
  }

}

