PageSourceSearch

https://convertir.github.io/js/tools-impl/csv-prepend-column.js

js convertir.github.io collected 2026-10-03 08:49:49 UTC 2,316 bytes, 56 lines download raw bytes

1$(function () {
2    mkTool(
3        'csv-prepend-column',
4        function (text, asyncResultFn) {
5            var newCol = $('#csv-prepend-column-new-column').val();
6
7            text = text.replace("\r\n", "\n");
8            var lines = text.split("\n");
9            lines.splice(0,0,lines[0]);
10            text = lines.join("\n");
11
12            var Converter = window.csvtojson.Converter;
13            var converter = new Converter({});
14            converter.transform = function (json, row, index) {
15                json['rowIndex_xyzzy'] = index;
16                json['row_xyzzy'] = row;
17            }
18            converter.fromString(text, function(err, result){
19                if (err) throw new Error(err);
20                asyncResultFn(result, text, newCol);
21            });
22        },
23        {
24            asyncResultFn : function (json, text, newCol) {
25                var newColLines = newCol.split('\n');
26                if (newColLines.length < json.length) {
27                    $('#action-error').show();
28                    $('#action-error').text('The new column has less rows than the existing CSV. Prepending anyway.');
29                    for (var i = 0; i < json.length-newColLines.length; i++) {
30                        newColLines.push('');
31                    }
32                }
33                if (newColLines.length > json.length) {
34                    $('#action-error').show();
35                    $('#action-error').text('The new column has more rows than the existing CSV. Prepending anyway.');
36                    newColLines.splice(json.length,newColLines.length);
37                }
38                var prependedJson = [];
39                for (var i = 0; i < json.length; i++) {
40                    json[i]['row_xyzzy'].splice(0,0,newColLines[i]);
41                    prependedJson.push(json[i]['row_xyzzy']);
42                }
43                var prepended = json2csv({
44                    data : prependedJson
45                });
46                var lines = prepended.split('\n');
47                prepended = lines.slice(1).join('\n');
48                $('#csv-prepend-column-text').val(prepended);
49            },
50            exceptionFn : function (err) {
51                $('#action-error').show();
52                $('#action-error').text(err);
53            }
54        }
55    );
56});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.