function fromV1(module) {
const {default:{modules:[{variables}]}} = module;
const nodes = [];
function pushToNodes(value, inputs, prefix) {
const valueArray = value.toString().split('\n');
if (valueArray[1] && valueArray[1].match(/^(async +)?function/))
prefix = '';
let cellArray = valueArray[0].match(/return\($/) ?
valueArray.slice(1,-1) : valueArray.slice(1);
if (inputs) {
inputs.filter(inp => inp.startsWith('viewof ')).forEach(
(inp, i) => cellArray = cellArray.map(
line => line.replace(new RegExp(`\\$${i}`,'g'), inp)));
inputs.filter(inp => inp.startsWith('mutable ')).forEach(
(inp, i) => cellArray = cellArray.map(
line => line.replace(new RegExp(`\\$${i}\.value`,'g'), inp)));
}
nodes.push({id:nodes.length, pinned:false, value:prefix+cellArray.join('\n')});
}
for (let j = 0; j < variables.length; j++) {
const {name, inputs, value, from, remote} = variables[j];
if (from === undefined) {
if (value) {
let prefix = '';
if (name) {
if (name.startsWith('viewof ')) {
prefix +=`${name} = `;
pushToNodes(value, inputs, prefix);
j++;
} // check for mutable (assume initial X -> mutable X -> X)
else if (name.startsWith('initial ')) {
const mutName = name.replace('initial', 'mutable');
prefix += `${mutName} = `;
pushToNodes(value, inputs, prefix);
j+=2;
} else {
// ordinary variable -> ordinary cell
prefix += `${name} = `;
pushToNodes(value, inputs, prefix);
}
} else {
pushToNodes(value, inputs, prefix);
}
} else {
// empty cells will be ignored! (these mostly come from comment-only cells...)
}
} else {
// make import cell, combine with previous
// IF THERE ARE INJECTIONS, THE FOLLOWING WON'T BE CORRECT!
let currFrom = from;
let currName = name;
let currRemote = remote;
let imported = [];
while (currFrom === from && ++j < variables.length) {
const {from:nextFrom, name:nextName, remote:nextRemote} = variables[j];
if (currFrom === nextFrom && // if the next "from" is "viewof" or "mutable", change the import
(nextRemote === 'viewof '+currRemote || nextRemote === 'mutable '+currRemote)) {
imported.push(`${nextRemote} as ${currName}`);
++j;
currFrom = variables[j].from;
currName = variables[j].name;
currRemote = variables[j].remote;
} else {
imported.push(currRemote === currName ? currRemote : `${currRemote} as ${currName}`);
currFrom = nextFrom;
currName = nextName;
currRemote = nextRemote;
}
}
nodes.push({id:nodes.length, pinned:false, value:`import {${imported.join(', ')}} from '${from}'`});
j--;
}
}
return nodes;
}