-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextFixes.js
87 lines (74 loc) · 1.57 KB
/
textFixes.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
// node[~"."~"([\u00A0\u0020][\u00A0\u0020]+|$[\u00A0\u0020]|[\u00A0\u0020]^)"](area.a);
/*
WORK IN PROGRESS
*/
var print = require("josm/util").println;
var layer = josm.layers.get(0);
var ds = layer.data;
function main()
{
var fixes = [
{name: "Double whitespace fix", regex: /[\u00A0\u0020][\u00A0\u0020]+/, replaceWith: " "},
{name: "Whitespace trim", func: function(str){return str.trim();}}
]
initStats(fixes); // set all fixes[i] counters to 0
print("### Running script - textFixes.js");
ds.each(function(p){
for (key in p.tags)
{
for (var i = 0; i < fixes.length; ++i)
{
applyFixToKey(p, fixes[i], key);
}
}
for (key in p.tags)
{
for (var i = 0; i < fixes.length; ++i)
{
applyFixToValue(p, fixes[i], key);
}
}
});
print("==Fix stats==");
for (var i = 0; i < fixes.length; ++i)
{
var fix = fixes[i];
print(fix.name + ": " + fix.stats);
}
print("### Script finished");
}
function applyFixToKey(p, fix, key)
{
var newKey = applyFixToStr(fix, key);
if ((newKey !== null) && (key !== newKey))
{
p.tags[newKey] = p.tags[key];
p.removeTag(key);
fix.stats++;
}
}
function applyFixToValue(p, fix, key)
{
var value = p.tags[key];
var newValue = applyFixToStr(fix, value);
if ((newValue !== null) && (newValue !== value))
{
p.tags[key] = newValue;
fix.stats++;
}
}
function applyFixToStr(fix, str)
{
if (fix.func === undefined)
return str.replace(fix.regex, fix.replaceWith);
else
return fix.func(str);
}
function initStats(fixes)
{
for (var i = 0; i < fixes.length; ++i)
{
fixes.stats = 0;
}
}
main();