tree-merge written in javascript