forked from prettydiff/prettydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvmin.js
104 lines (99 loc) · 3.63 KB
/
csvmin.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*global charDecoder*/
/*
This code may be used internally to Travelocity without limitation,
exclusion, or restriction. If this code is used externally the
following comment must be included everywhere this code is used.
*/
/***********************************************************************
This is written by Austin Cheney on 21 Apr 2010. Anybody may use this
code without permission so long as this comment exists verbatim in each
instance of its use.
http://www.travelocity.com/
http://mailmarkup.org/
http://prettydiff.com/
**********************************************************************/
/*
This code merely returns code transformed by csvbeauty back into its
former state.
*/
/*global charDecoder*/
/*
This code may be used internally to Travelocity without limitation,
exclusion, or restriction. If this code is used externally the
following comment must be included everywhere this code is used.
*/
/*
This code merely returns code transformed by csvbeauty back into its
former state.
*/
var csvmin = function (source, ch) {
"use strict";
if (ch === "") {
ch = ",";
} else {
ch = charDecoder(ch);
}
(function () {
var a = 0,
b = source.length,
c = [],
d = "",
e = 0,
f = [],
multiline = function (x) {
var w = [],
y,
z = x.length - 2;
if (x.length === 2) {
return "{ }";
} else {
for (y = 0; y < z; y += 1) {
w.push(ch);
}
return w.join("") + "{ }";
}
},
g = source.replace(/\n\n\{\-\}\n\n/g, "{-}").replace(/\n{2,}/g, multiline).split("\n"),
err = "",
error = "Error: Unterminated String begging at character number ";
for (a = 0; a < b; a += 1) {
c = [];
if (g[a].indexOf("\"") !== -1) {
f = g[a].split("");
e = f.length;
for (b = 0; b < e; b += 1) {
if (f[b] === "\"") {
c.push(b);
}
}
if (c.length === 1) {
d = error;
g[a] = f.join("");
err = g[a].slice(c[0], c[0] + 9);
return;
} else if (c.length > 2) {
e = c.length - 1;
for (d = 1; d < e; d += 1) {
f[c[d]] = "\"\"";
}
}
g[a] = f.join("");
}
}
if (d === error) {
return error + (g.join(ch).indexOf(g[a]) + c[0]) + " or value number " + (a + 1) + ", '" + err + "'.";
}
if (g[g.length - 1] === "{|}") {
g[g.length - 1] = "";
}
source = g.join(ch).replace(/\n/g, ch);
}());
do {
source = source.replace("{ }", "\n");
} while (source.indexOf("{ }") !== -1);
source = source.replace(/\n{2}/g, "\n");
if (source.indexOf("{|}") === source.length - 3) {
source = source.slice(0, source.length - 3) + ch;
}
return source.replace(/\{\-\}/g, "\n");
};