Fix potential bug in key array params expanding

This commit is contained in:
Alexander Zobnin
2017-05-17 22:04:26 +03:00
parent 253ed4fb72
commit b0344dc817
7 changed files with 119 additions and 10 deletions

View File

@@ -16,6 +16,11 @@ describe('Utils', () => {
name: `CPU $2 time - $3`,
key: `system.cpu.util[,system,avg1]`,
expected: "CPU system time - avg1"
},
{
name: `CPU - $1 - $2 - $3`,
key: `system.cpu.util[,system,avg1]`,
expected: "CPU - - system - avg1"
}
];
@@ -56,5 +61,31 @@ describe('Utils', () => {
});
done();
});
it('should properly expand array params', (done) => {
let test_cases = [
{
name: `CPU $2 - $3 time`,
key: `system.cpu.util[,[user,system],avg1]`,
expected: "CPU user,system - avg1 time"
},
{
name: `CPU $2 - $3 time`,
key: `system.cpu.util[,["user,system",iowait],avg1]`,
expected: `CPU "user,system",iowait - avg1 time`
},
{
name: `CPU - $2 - $3 - $4`,
key: `system.cpu.util[,[],["user,system",iowait],avg1]`,
expected: `CPU - - "user,system",iowait - avg1`
}
];
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
});
done();
});
});
});

View File

@@ -25,15 +25,22 @@ export function expandItemName(name, key) {
function splitKeyParams(paramStr) {
let params = [];
let quoted = false;
let in_array = false;
let split_symbol = ',';
let param = '';
_.forEach(paramStr, symbol => {
if (symbol === '"' && !quoted) {
quoted = true;
if (symbol === '"' && in_array) {
param += symbol;
} else if (symbol === '"' && quoted) {
quoted = false;
} else if (symbol === split_symbol && !quoted) {
} else if (symbol === '"' && !quoted) {
quoted = true;
} else if (symbol === '[' && !quoted) {
in_array = true;
} else if (symbol === ']' && !quoted) {
in_array = false;
} else if (symbol === split_symbol && !quoted && !in_array) {
params.push(param);
param = '';
} else {