Lodash:在字符串末尾按模式匹配分块

Lodash: chunk by pattern matching at end of string

我的文件格式如下所示。

' this is a comment
#method1
XQ#1234
END
#method2
XR#1234
HR#9620
END

我的目标是分块,但展望 END 的每个实例。

var instructions = _(fs.readdirSync(filepath))
  .filter((file) => _.endsWith(file, '.assembly'))
  .map((filename) => fs.readFileSync(path.join(filepath, filename)).toString('ascii').split('\n'))
  .flatten()
  .reject((str) => /^\s*$|^[\\'\"].*/g.test(str))
  // ^ the contents of the files with the comments and blank lines removed.
  // here is where I get lost
  .chunk( /** ??? */ )

最终目标是拥有这样的东西:

[ ['XQ#1234'], ['XR#1234', 'HR#9620'] ]

我遇到的主要问题是根据 "END" 的每个实例对数组进行分块,而不是使用两个分隔符(例如,BEGINEND)。我认为必须使用 _.findIndex 来帮助解决这个问题。

如何根据 lodash 中的结束定界符对出现的字符串进行分块?

您正在寻找拆分函数:

_.mixin({"split": function(arr, f) {
    return _.reduce(arr, function(r, x) {
       if(f(x)) {
           r.push([]);
       } else {
           r[r.length - 1].push(x)
       }
       return r;
    }, [[]]);
}});

并像这样使用它:

var instructions = _(fs.readdirSync(filepath))
  .filter((file) => _.endsWith(file, '.assembly'))
  .map((filename) => fs.readFileSync(path.join(filepath, filename)).toString('ascii').split('\n'))
  .flatten()
  .reject((str) => /^\s*$|^[\\'\"].*/g.test(str))
  .split((str) => "END" == str)

或者,如果您不想向 lodash 添加新的 mixin:

...
.reject((str) => /^\s*$|^[\\'\"].*/g.test(str))
.reduce(function(r, x) {
   if("END" == x) {
       r.push([]);
   } else {
       r[r.length - 1].push(x)
   }
   return r;
}, [[]]);