Modernise forEach loops throughout

This commit is contained in:
Casper Warden
2020-11-19 16:27:29 +00:00
parent 5c7fcd09bd
commit 7b41239a39
83 changed files with 363 additions and 361 deletions

View File

@@ -81,21 +81,22 @@ fs.readFile('./msg_template.msg', (err, data) =>
//Remove all comments
const lines = msgTemplate.split('\n');
let newLines = [];
lines.forEach((line) => {
let pos = line.indexOf('//');
if (pos !== -1)
{
line = line.substr(0, pos-1);
}
newLines.push(line);
});
for (const line of lines)
{
let pos = line.indexOf('//');
if (pos !== -1)
{
line = line.substr(0, pos-1);
}
newLines.push(line);
}
msgTemplate = newLines.join('\n');
let messages = getBlocks(msgTemplate);
let done = false;
let msgObjects = [];
messages.forEach((message) =>
for (const message of messages)
{
let newMessage = {};
@@ -113,7 +114,7 @@ fs.readFile('./msg_template.msg', (err, data) =>
}
let blocks = getBlocks(message);
blocks.forEach((block) =>
for (const block of blocks)
{
let newBlock = {};
params = getParams(block);
@@ -128,7 +129,7 @@ fs.readFile('./msg_template.msg', (err, data) =>
}
let paramBlocks = getBlocks(block);
paramBlocks.forEach((paramBlock) =>
for (const paramBlock of paramBlocks)
{
let data = getParams(paramBlock);
data = data.split(' ');
@@ -143,12 +144,12 @@ fs.readFile('./msg_template.msg', (err, data) =>
obj['size'] = data[2];
}
newBlock.params.push(obj);
});
}
newMessage.blocks.push(newBlock);
});
}
msgObjects.push(newMessage);
});
}
fs.writeFile('./msg_template.json', JSON.stringify(msgObjects, null, 4), (err) =>
{

View File

@@ -2,7 +2,7 @@ const messages = require('./msg_template.json');
const path = require('path');
const fs = require('fs');
messages.forEach((message) =>
for (const message of messages)
{
let classString = '// This file has been automatically generated by writeMessageClasses.js\n\n';
@@ -13,9 +13,9 @@ messages.forEach((message) =>
let vector4 = false;
let long = false;
let quaternion = false;
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
block.params.forEach((param) =>
for (const param of block.params)
{
switch(param.type)
{
@@ -56,8 +56,8 @@ messages.forEach((message) =>
default:
console.log("UNKNOWN TYPE: "+param.type);
}
});
});
}
}
if (uuid)
{
classString += 'import { UUID } from \'../UUID\';\n'
@@ -92,7 +92,7 @@ messages.forEach((message) =>
let flags = [];
message.flags.forEach((flag) =>
for (const flag of message.flags)
{
switch(flag.toUpperCase())
{
@@ -118,7 +118,7 @@ messages.forEach((message) =>
break;
}
});
}
let id = parseInt(message.id);
switch(message.frequency)
{
@@ -152,15 +152,14 @@ messages.forEach((message) =>
let messageVariableSize = [];
let calcVarVar = false;
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
classString += ' '+block.name+': {\n';
let blockFixedSize = 0;
let blockVariableSize = [];
block.params.forEach((param) =>
for (const param of block.params)
{
classString += ' '+param.name+': ';
let jstype = 'string';
@@ -244,7 +243,7 @@ messages.forEach((message) =>
console.log('Unknown type: '+param.type);
}
classString += jstype + ';\n';
});
}
if (block.type === 'Single')
{
@@ -288,7 +287,7 @@ messages.forEach((message) =>
default:
console.log("Unknown type: "+block.type);
}
});
}
classString+='\n';
classString+=' getSize(): number\n';
@@ -309,13 +308,13 @@ messages.forEach((message) =>
classString+='\n';
if (calcVarVar)
{
classString+=' calculateVarVarSize(block: object[], paramName: string, extraPerVar: number): number\n' +
classString+=' calculateVarVarSize(block: {[key: string]: any}[], paramName: string, extraPerVar: number): number\n' +
' {\n' +
' let size = 0;\n' +
' block.forEach((bl: any) =>\n' +
' for (const bl of block)\n' +
' {\n' +
' size += bl[paramName].length + extraPerVar;\n' +
' });\n' +
' }\n' +
' return size;\n' +
' }\n\n';
}
@@ -330,19 +329,19 @@ messages.forEach((message) =>
let letConst = 'const';
let varBlockCount = 0;
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
if (block.type === 'Variable' || block.type === 'Multiple')
{
varBlockCount++;
}
});
}
if (varBlockCount > 1)
{
letConst = 'let';
}
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
let single = false;
let blockIndex = '';
@@ -379,7 +378,7 @@ messages.forEach((message) =>
classString += ' for (let i = 0; i < count; i++)\n';
classString += ' {\n';
}
block.params.forEach((param) =>
for (const param of block.params)
{
let val = '';
if (!single)
@@ -477,13 +476,13 @@ messages.forEach((message) =>
default:
console.log('Unknown type: ' + param.type);
}
});
}
if (!single)
{
classString += ' }\n';
}
});
}
classString += ' return pos - startPos;\n';
}
else
@@ -503,13 +502,13 @@ messages.forEach((message) =>
let letConst = 'const';
let varBlockCount = 0;
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
if (block.type === 'Variable' || block.type === 'Multiple')
{
varBlockCount++;
}
});
}
if (varBlockCount > 1)
{
letConst = 'let';
@@ -522,7 +521,7 @@ messages.forEach((message) =>
}
let firstBlock = true;
let donelet = false;
message.blocks.forEach((block) =>
for (const block of message.blocks)
{
let single = false;
let blockIndex = '';
@@ -575,7 +574,7 @@ messages.forEach((message) =>
}
const paramTypes = [];
const paramValues = [];
block.params.forEach((param) =>
for (const param of block.params)
{
let jstype = 'string';
let jsvalue = '\'\'';
@@ -648,12 +647,12 @@ messages.forEach((message) =>
}
paramTypes.push(spaces + ' ' + param.name + ': '+jstype);
paramValues.push(spaces + ' ' + param.name + ': '+jsvalue);
});
}
classString += paramTypes.join(',\n')+'\n';
classString += spaces + ' } = {\n';
classString += paramValues.join(',\n')+'\n';
classString += spaces + ' };\n';
block.params.forEach((param) =>
for (const param of block.params)
{
let val = '';
switch (param.type)
@@ -745,7 +744,7 @@ messages.forEach((message) =>
default:
console.log('Unknown type: ' + param.type);
}
});
}
if (!single)
{
classString += ' this.'+block.name+'.push(newObj' + block.name + ');\n';
@@ -757,7 +756,7 @@ messages.forEach((message) =>
}
});
}
classString += ' return pos - startPos;\n';
}
else
@@ -774,22 +773,22 @@ messages.forEach((message) =>
});
});
}
//Now write the Messages class
let classString = '// This file has been automatically generated by writePacketClasses.js\n\n';
messages.forEach((message) =>
for (const message of messages)
{
const name = message.name;
classString += 'export * from \'./messages/'+name+'\';\n';
});
}
classString += 'import { Message } from \'../enums/Message\';\n';
classString += '\n';
classString += 'const messages: {[index: number]: string} = {};\n';
const msgs = [];
messages.forEach((message) =>
for (const message of messages)
{
msgs.push('messages[<number>Message.' + message.name + '] = \'' + message.name + 'Message\'');
});
}
classString += msgs.join(';\n')+';\n';
classString += '\n';
classString += 'export function nameFromID(id: Message): string\n';
@@ -804,7 +803,7 @@ fs.writeFile(p, classString, (err) =>
classString = 'export enum Message {\n';
const msgArr = [];
messages.forEach((message) =>
for (const message of messages)
{
let id = parseInt(message.id);
switch(message.frequency)
@@ -824,7 +823,7 @@ messages.forEach((message) =>
break;
}
msgArr.push(' '+message.name+' = '+id);
});
}
classString += msgArr.join(',\n')+'\n';
classString += '}\n';
const e = path.join(__dirname+'/../lib/enums/Message.ts');