All 'tokens' are seperated by a single comma ,
.
The dimension identification specifies how many dimensions are present in the file.
Example: 2
(standard x,y)
You can optionally specify titles for each dimension, by including an @
symbol, followed by the display names separated by ;
.
Example: 2@x;y
Data is comma-separated and is split into sections depending on the dimension identification.
Example:
Points in (x,y):
(1, 2)
(5, 3)
(0.3, 0.9)
Full DP file: 2@x;y,1,2,5,3,0.3,0.9
function parseDP(inData) {
if(!inData.includes(','))
return {};
let tokens = inData.trim().split(',');
let labels = [];
if(tokens.length < 2)
return {};
if(tokens[0].includes('@'))
labels = tokens[0].split('@')[1].split(';');
if(isNaN(tokens[0].split('@')[0]))
return {};
let dimSize = parseInt(tokens[0].split('@')[0]),
points = [],
buffer = {},
tempCount = 0;
for(let i = 1; i < tokens.length; i++) {
let val = parseFloat(tokens[i]);
let label = typeof(labels[tempCount]) !== 'undefined' ? labels[tempCount] : tempCount;
buffer[label] = val;
tempCount++;
if((tempCount) >= dimSize || (i+1) === tokens.length) {
points.push(buffer);
buffer = {};
tempCount = 0;
}
}
return points;
}
input: 2@x;y,1,2,5,3,0.3,0.9
output: [ { x: 1, y: 2 }, { x: 5, y: 3 }, { x: 0.3, y: 0.9 } ]