-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
99 lines (85 loc) · 2.82 KB
/
converter.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
(function () {
"use strict";
var moment = require("moment"),
_ = require("lodash");
require("stringformat").extendString("format");
var defaultTransformer = "citygram";
var check = function (result) {
if (!(result && result.rows && Array.isArray(result.rows))) {
throw(new Error("Invalid result object"));
return false;
}
return true;
};
String.prototype.toTitleCase = function () {
return this.replace(/[\w\/\-]\S*/g, function (text) {
return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
});
};
RegExp.prototype.firstMatch = function (text) {
var matches = (text || "").match(this);
return (matches && matches.length) ? matches[0] : undefined;
};
var extrude = {
"date": function (text) {
var match = /(\d{1,2}\/\d{1,2}\/\d{4})/.firstMatch(text);
return (match) ? moment(Date.parse(match)).format("YYYY-MM-DD") : null;
},
"time": function (text) {
var match = /(\d{1,2}\:\d{2}\:\d{2}\s?[AP]M)/i.firstMatch(text);
return (match) ? moment(Date.parse("1/1/1970 " + match)).format("HH:mm:ss.000") : null;
}
};
var transforms = {
"citygram": function (result) {
var entitle = function (record, datetime) {
var template = "A crime incident happened near you on {0} at {1}. " +
"The Cary Police described it as {2}",
when = moment(Date.parse(datetime.replace("T", " ") + " "));
return template.format(when.format("ddd MMM D"),
when.format("h:mm A"),
(record.charge || record.crime).toTitleCase());
};
var transformed = _.map(result.rows, function (row) {
var datetime = extrude.date(row.time) + "T" +
(extrude.time(row.time) || "00:00:00.000"),
location = {
type: "Point",
description: row.location,
coordinates: row.coordinates || []
};
return {
id: "Cary_" + row.id,
datetime: datetime,
type: "Crime Alert",
properties: {
inc_datetime: datetime,
inc_no: row.id,
lcr_desc: row.charge,
location: location,
datasetid: "Cary Police Report",
title: entitle(row, datetime)
},
geometry: location
};
});
return transformed;
}
}
var converter = (function () {
return {
transform: function (result, transformer) {
var transformed = null;
if (check(result)) {
var method = transforms[transformer || defaultTransformer];
if (typeof method !== "function") {
throw(new Error("Invalid transformer: " + String(transformer)));
}
transformed = method(result);
}
return transformed;
}
};
})();
module.exports = converter;
})();