-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
normalizers.js
140 lines (109 loc) · 2.65 KB
/
normalizers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var _ = require('lodash');
module.exports = {};
// don't remove this, it's used for composing functions in eval_test
module.exports.identity = function(val) {
return val;
};
module.exports.stripPunctuation = function(val) {
if (!_.isString(val)) {
return val;
}
// remove anything that's not a letter, number, underscore, or whitespace
return val.replace(/[^\w\s]/g, '');
};
module.exports.toLowerCase = function(val) {
if (!_.isString(val)) {
return val;
}
return val.toLowerCase();
};
module.exports.toUpperCase = function(val) {
if (!_.isString(val)) {
return val;
}
return val.toUpperCase();
};
module.exports.trim = function(val) {
if (!_.isString(val)) {
return val;
}
return val.trim();
};
module.exports.abbreviateDirectionals = function(val) {
if (!_.isString(val)) {
return val;
}
var directionals = {
'NORTH': 'N',
'SOUTH': 'S',
'EAST': 'E',
'WEST': 'W',
'NORTHEAST': 'NE',
'NORTHWEST': 'NW',
'SOUTHEAST': 'SE',
'SOUTHWEST': 'SW'
};
return replaceTerms(val, directionals, 'gi');
};
module.exports.abbreviateStreetSuffixes = function(val) {
if (!_.isString(val)) {
return val;
}
var terms = {
'AVE': 'AV',
'AVENUE': 'AV',
'BOULEVARD': 'BLVD',
'CIRCLE': 'CIR',
'COURT': 'CT',
'COVE': 'CV',
'DRIVE': 'DR',
'HIGHWAY': 'HWY',
'JUNIOR': 'JR',
'LANE': 'LN',
'LOOP': 'LP',
'PARKWAY': 'PKWY',
'PIKE': 'PKE',
'PLACE': 'PL',
'ROAD': 'RD',
'STREET': 'ST',
'TERR': 'TER',
'TERRACE': 'TER',
'TRACE': 'TRCE',
'WAY': 'WY'
};
return replaceTerms(val, terms, 'gi');
};
module.exports.removeOrdinals = function(val) {
if (!_.isString(val)) {
return val;
}
return val.replace(/((\d+)(st|nd|th|rd))/ig, '$2');
};
// Sometimes we know a string (poi name etc.) should not contain any numbers
// this normalizer erases the numbers and trims the result
module.exports.removeNumbers = function(val) {
if (!_.isString(val)) {
return val;
}
return val.replace(/[0-9]/g, '').trim();
};
module.exports.abbreviateMountSaintFort = function(val) {
if (!_.isString(val)) {
return val;
}
var terms = {
'MOUNT': 'Mt',
'SAINT': 'St',
'FORT': 'Ft'
};
return replaceTerms(val, terms, 'gi');
};
// starting with the input value, case-insensitively replace all original
// originals with replacments
function replaceTerms(val, terms, flags) {
return Object.keys(terms).reduce( function(previousValue, original) {
var regexp = new RegExp('\\b' + original + '\\b', flags);
var replacment = terms[original];
return previousValue.replace(regexp, replacment);
}, val);
}