这段代码可以更短更容易实现吗?
Can this code be shorter and easier to implement?
我正在为节点 red 制作一个脚本,我想让它易于实现。我怎样才能进一步压缩这段代码?
var notDetected = "NOT DETECTED";
var models = [
context.get('model1')||notDetected,
context.get('model2')||notDetected,
context.get('model3')||notDetected,
context.get('model4')||notDetected
];
switch(msg.topic)
{
case "core_1":
models[0] = msg.model + "";
context.set('model1', models[0]);
break;
case "core_2":
models[1] = msg.model + "";
context.set('model2', models[1]);
break;
case "core_3":
models[2] = msg.model + "";
context.set('model3', models[2]);
break;
case "core_4":
models[3] = msg.model + "";
context.set('model4', models[3]);
break;
}
var msgs = [
{payload: models[0]},
{payload: models[1]},
{payload: models[2]},
{payload: models[3]}
];
return msgs;
模型还能再压缩吗?短信呢?我可以删除模型中的那个 ID 以匹配消息 ID,例如
msg[x] = {payload: models[x]};
可能吗?
根据readability/extensibility压缩代码(不一定space):
- 可以使用循环填充
models
和msgs
数组,
- 您可以在第一个“_”处拆分
core_X
字符串来获取索引,但如果您不验证输入,这可能会导致安全问题;或者您可以使用从四个字符串 core_1
到实际数字的映射。
在这里查看 for 循环:https://www.w3schools.com/js/js_loop_for.asp
它可能看起来像这样:
var msgs = [];
for (x = 0; x < 4; x++) {
msgs[x] = {payload: models[x]};
}
所以在稍微考虑之后,我制作了一个脚本,它将仅将 4 条消息重定向到它们需要去的地方,然后在另一个脚本中使用所需的参数,或者只是从整个消息中获取 msg.model。这是拆分器代码:
var notDetected = {};
var msgs = [
context.get('msg1')||notDetected,
context.get('msg2')||notDetected,
context.get('msg3')||notDetected,
context.get('msg4')||notDetected
];
switch(msg.topic)
{
case "core_1":
context.set('msg1', msg);
break;
case "core_2":
context.set('msg2', msg);
break;
case "core_3":
context.set('msg3', msg);
break;
case "core_4":
context.set('msg4', msg);
break;
}
return msgs;
在文本节点中,我只是设置为使用 msg.model 值。现在我得到了更有用的分离器
您可以将 context.get() 包装在一个函数中,然后在您的 models 数组中重复使用它,这样您就不必更改每个函数更改数组中的索引。
示例:
var notDetected = "NOT DETECTED";
var getContext = function(str) {
return context.get(str) || "NOT DETECTED";
}
var models = [
getContext('model1'),
getContext('model2'),
getContext('model3'),
getContext('model4'),
];
如果您需要对 context.get() 进行更改,您只需在函数中更改一次即可,如下所示:differentContext.get(str) || "NOT DETECTED"
否则您将不得不更改每个索引:
var models = [
differentContext.get('model1')||notDetected,
differentContext.get('model2')||notDetected,
differentContext.get('model3')||notDetected,
differentContext.get('model4')||notDetected
];
我正在为节点 red 制作一个脚本,我想让它易于实现。我怎样才能进一步压缩这段代码?
var notDetected = "NOT DETECTED";
var models = [
context.get('model1')||notDetected,
context.get('model2')||notDetected,
context.get('model3')||notDetected,
context.get('model4')||notDetected
];
switch(msg.topic)
{
case "core_1":
models[0] = msg.model + "";
context.set('model1', models[0]);
break;
case "core_2":
models[1] = msg.model + "";
context.set('model2', models[1]);
break;
case "core_3":
models[2] = msg.model + "";
context.set('model3', models[2]);
break;
case "core_4":
models[3] = msg.model + "";
context.set('model4', models[3]);
break;
}
var msgs = [
{payload: models[0]},
{payload: models[1]},
{payload: models[2]},
{payload: models[3]}
];
return msgs;
模型还能再压缩吗?短信呢?我可以删除模型中的那个 ID 以匹配消息 ID,例如
msg[x] = {payload: models[x]};
可能吗?
根据readability/extensibility压缩代码(不一定space):
- 可以使用循环填充
models
和msgs
数组, - 您可以在第一个“_”处拆分
core_X
字符串来获取索引,但如果您不验证输入,这可能会导致安全问题;或者您可以使用从四个字符串core_1
到实际数字的映射。
在这里查看 for 循环:https://www.w3schools.com/js/js_loop_for.asp
它可能看起来像这样:
var msgs = [];
for (x = 0; x < 4; x++) {
msgs[x] = {payload: models[x]};
}
所以在稍微考虑之后,我制作了一个脚本,它将仅将 4 条消息重定向到它们需要去的地方,然后在另一个脚本中使用所需的参数,或者只是从整个消息中获取 msg.model。这是拆分器代码:
var notDetected = {};
var msgs = [
context.get('msg1')||notDetected,
context.get('msg2')||notDetected,
context.get('msg3')||notDetected,
context.get('msg4')||notDetected
];
switch(msg.topic)
{
case "core_1":
context.set('msg1', msg);
break;
case "core_2":
context.set('msg2', msg);
break;
case "core_3":
context.set('msg3', msg);
break;
case "core_4":
context.set('msg4', msg);
break;
}
return msgs;
在文本节点中,我只是设置为使用 msg.model 值。现在我得到了更有用的分离器
您可以将 context.get() 包装在一个函数中,然后在您的 models 数组中重复使用它,这样您就不必更改每个函数更改数组中的索引。
示例:
var notDetected = "NOT DETECTED";
var getContext = function(str) {
return context.get(str) || "NOT DETECTED";
}
var models = [
getContext('model1'),
getContext('model2'),
getContext('model3'),
getContext('model4'),
];
如果您需要对 context.get() 进行更改,您只需在函数中更改一次即可,如下所示:differentContext.get(str) || "NOT DETECTED"
否则您将不得不更改每个索引:
var models = [
differentContext.get('model1')||notDetected,
differentContext.get('model2')||notDetected,
differentContext.get('model3')||notDetected,
differentContext.get('model4')||notDetected
];