csv

webix.csv

webix.csv helper.

Please look into the linked official documentation.

External references

Official documentation page.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
webix.csv = {
    escape:true,
    delimiter:{
        rows: "\n",
        cols: "\t"
    },
    parse:function(text, sep){
        sep = sep||this.delimiter;
        if (!this.escape)
            return this._split_clip_data(text, sep);

        var lines = text.replace(/\n$/,"").split(sep.rows);

        var i = 0;
        while (i < lines.length - 1) {
            if (this._substr_count(lines[i], '"') % 2 === 1) {
                lines[i] += sep.rows + lines[i + 1];
                delete lines[i + 1];
                i++;
            }
            i++;
        }
        var csv = [];
        for (i = 0; i < lines.length; i++) {
            if (typeof(lines[i]) !== 'undefined') {
                var line = lines[i].split(sep.cols);
                for (var j = 0; j < line.length; j++) {
                    if (line[j].indexOf('"') === 0)
                        line[j] = line[j].substr(1, line[j].length - 2);
                    line[j] = line[j].replace('""', '"');
                }
                csv.push(line);
            }
        }
        return csv;
    },
    _split_clip_data: function(text, sep) {
        var lines = text.split(sep.rows);
        for (var i = 0; i < lines.length; i++) {
            lines[i] = lines[i].split(sep.cols);
        }
        return lines;
    },
    /*! counts how many occurances substring in string **/
    _substr_count: function(string, substring) {
        var arr = string.split(substring);
        return arr.length - 1;
    },
    stringify:function(data, sep){
        sep = sep||this.delimiter;

        if (!this.escape){
            for (var i = 0; i < data.length; i++)
                data[i] = data[i].join(sep.cols);
            return data.join(sep.rows);
        }

        var reg = /\n|\"|;|,/;
        for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < data[i].length; j++) {
                if (reg.test(data[i][j])) {
                    data[i][j] = data[i][j].replace(/"/g, '""');
                    data[i][j] = '"' + data[i][j] + '"';
                }
            }
            data[i] = data[i].join(sep.cols);
        }
        data = data.join(sep.rows);
        return data;
    }
};