circom/src/streamfromarray_txt.js
src/streamfromarray_txt.jsRaw
1const Readable = require("stream").Readable;
2
3module.exports = function streamFromArrayTxt(ma) {
4 const rs = Readable();
5
6 let curIndex = getFirstIdx(ma);
7
8 rs._read = function() {
9 let res;
10 res = objFromIdx(ma, curIndex);
11 curIndex = nextIdx(curIndex);
12 if (res!==null) {
13 rs.push(res + "\n");
14 } else {
15 rs.push(null);
16 }
17 };
18
19
20 return rs;
21
22
23 function getFirstIdx(ma) {
24 if (typeof ma.push !== "function" ) return [];
25 return [0, ...getFirstIdx(ma[0])];
26 }
27
28 function nextIdx(idx) {
29 if (idx == null) return null;
30 if (idx.length == 0) return null;
31
32 const parentIdx = idx.slice(0,-1);
33
34 const itObj = objFromIdx(ma, parentIdx);
35 const newLastIdx = idx[idx.length-1]+1;
36 if (newLastIdx < itObj.length) {
37 const resIdx = idx.slice();
38 resIdx[resIdx.length-1] = newLastIdx;
39 return [...resIdx, ...getFirstIdx(itObj[newLastIdx])];
40 } else {
41 return nextIdx(parentIdx);
42 }
43 }
44
45 function objFromIdx(ma, idx) {
46 if (idx == null) return null;
47 if (idx.length == 0) return ma;
48 if (ma.length == 0) return "";
49 return objFromIdx(ma[idx[0]], idx.slice(1));
50 }
51};