Skip to content

Commit cce1009

Browse files
committed
feat: add stripJsonOfComments to utils
The function removes any comments inside a JSON string.
1 parent 2a01e44 commit cce1009

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

js/src/utils.js

+59-23
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ var game_1_y = 1660.16;
2929
var game_2_x = -3778.16;
3030
var game_2_y = -4549.6;
3131

32-
33-
var game_min_x = -4000.00;
34-
var game_max_x = 6000.00;
35-
var game_min_y = 8000.00;
36-
var game_max_y = -4000.00;
37-
var map_min_x = 0;
38-
var map_min_y = 0;
39-
40-
/*
41-
var rLng = map_1_lng + (x - game_1_x) * (map_1_lng - map_2_lng) / (game_1_x - game_2_x);
42-
var rLat = map_1_lat + (y - game_1_y) * (map_1_lat - map_2_lat) / (game_1_y - game_2_y);
43-
*/
4432
function convertToMap(x, y) {
4533
var h = _MAP_currentLayer.options.tileSize * 3,
4634
w = _MAP_currentLayer.options.tileSize * 2;
@@ -56,20 +44,68 @@ function convertToMap(x, y) {
5644
};
5745
}
5846

59-
function convertToMapOld(x, y) {
60-
var map_max_x = _MAP_currentLayer.options.tileSize * 2,
61-
map_max_y = _MAP_currentLayer.options.tileSize * 3;
47+
const singleComment = 1;
48+
const multiComment = 2;
49+
const stripWithoutWhitespace = () => '';
50+
const stripWithWhitespace = (str, start, end) => str.slice(start, end).replace(/\S/g, ' ');
6251

63-
var xPercent = normalize(x, game_min_x, game_max_x);
64-
var destX = xPercent * (Math.abs(map_max_x - map_min_x)) + map_min_x;
52+
function stripJsonOfComments(str, opts) {
53+
opts = opts || {};
6554

66-
var yPercent = normalize(y, game_min_y, game_max_y);
67-
var destY = yPercent * (Math.abs(map_max_y - map_min_y)) + map_min_y;
55+
const strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
6856

69-
console.log(convertToMap(x,y));
70-
console.log(_MAP_map.unproject([destX, destY], _MAP_map.getMaxZoom()));
71-
return _MAP_map.unproject([destX, destY], _MAP_map.getMaxZoom());
72-
}
57+
let insideString = false;
58+
let insideComment = false;
59+
let offset = 0;
60+
let ret = '';
61+
62+
for (let i = 0; i < str.length; i++) {
63+
const currentChar = str[i];
64+
const nextChar = str[i + 1];
65+
66+
if (!insideComment && currentChar === '"') {
67+
const escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
68+
if (!escaped) {
69+
insideString = !insideString;
70+
}
71+
}
72+
73+
if (insideString) {
74+
continue;
75+
}
76+
77+
if (!insideComment && currentChar + nextChar === '//') {
78+
ret += str.slice(offset, i);
79+
offset = i;
80+
insideComment = singleComment;
81+
i++;
82+
} else if (insideComment === singleComment && currentChar + nextChar === '\r\n') {
83+
i++;
84+
insideComment = false;
85+
ret += strip(str, offset, i);
86+
offset = i;
87+
continue;
88+
} else if (insideComment === singleComment && currentChar === '\n') {
89+
insideComment = false;
90+
ret += strip(str, offset, i);
91+
offset = i;
92+
} else if (!insideComment && currentChar + nextChar === '/*') {
93+
ret += str.slice(offset, i);
94+
offset = i;
95+
insideComment = multiComment;
96+
i++;
97+
continue;
98+
} else if (insideComment === multiComment && currentChar + nextChar === '*/') {
99+
i++;
100+
insideComment = false;
101+
ret += strip(str, offset, i + 1);
102+
offset = i + 1;
103+
continue;
104+
}
105+
}
106+
107+
return ret + (insideComment ? strip(str.substr(offset)) : str.substr(offset));
108+
};
73109

74110
function convertToMapLeaflet(x, y){
75111
var t = convertToMap(x, y);

0 commit comments

Comments
 (0)