-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathparser.cc
More file actions
126 lines (95 loc) · 3.81 KB
/
Copy pathparser.cc
File metadata and controls
126 lines (95 loc) · 3.81 KB
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
#include <libpostal/libpostal.h>
#include <nan.h>
#define PARSER_USAGE "Usage: parse_address(address[, options])"
void ParseAddress(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
if (info.Length() < 1) {
Nan::ThrowTypeError(PARSER_USAGE);
return;
}
if (!info[0]->IsString()) {
Nan::ThrowTypeError(PARSER_USAGE);
}
Nan::Utf8String address_utf8(info[0]);
char *address = *address_utf8;
if (address == NULL) {
Nan::ThrowTypeError("Could not convert first arg to string");
return;
}
char *language = NULL;
char *country = NULL;
uint64_t i;
libpostal_address_parser_options_t options = libpostal_get_address_parser_default_options();
if (info.Length() > 1 && info[1]->IsObject()) {
v8::Local<v8::Object> props = info[1]->ToObject(context).ToLocalChecked();
v8::Local<v8::Array> prop_names = Nan::GetPropertyNames(props).ToLocalChecked();
for (i = 0; i < prop_names->Length(); i++) {
v8::Local<v8::Value> key = prop_names->Get(context, i).ToLocalChecked();
if (key->IsString()) {
Nan::Utf8String utf8_key(key);
char *key_string = *utf8_key;
v8::Local<v8::Value> value = Nan::Get(props, key).ToLocalChecked();
if (strcmp(key_string, "language") == 0) {
Nan::Utf8String language_utf8(value);
language = *language_utf8;
if (language != NULL) {
options.language = language;
}
} else if (strcmp(key_string, "country") == 0) {
Nan::Utf8String country_utf8(value);
country = *country_utf8;
if (country != NULL) {
options.country = country;
}
}
}
}
}
libpostal_address_parser_response_t *response = libpostal_parse_address(address, options);
if (response == NULL) {
Nan::ThrowError("Error parsing address");
return;
}
v8::Local<v8::Array> ret = Nan::New<v8::Array>(response->num_components);
v8::Local<v8::String> name_key = Nan::New("value").ToLocalChecked();
v8::Local<v8::String> label_key = Nan::New("component").ToLocalChecked();
for (i = 0; i < response->num_components; i++) {
char *component = response->components[i];
char *label = response->labels[i];
v8::Local<v8::Object> o = Nan::New<v8::Object>();
o->Set(context, name_key, Nan::New(component).ToLocalChecked());
o->Set(context, label_key, Nan::New(label).ToLocalChecked());
ret->Set(context, i, o);
}
libpostal_address_parser_response_destroy(response);
info.GetReturnValue().Set(ret);
}
static void cleanup(void*) {
libpostal_teardown();
libpostal_teardown_parser();
}
void init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();
if (!libpostal_setup() || !libpostal_setup_parser()) {
Nan::ThrowError("Could not load libpostal");
return;
}
exports->Set(
context,
Nan::New("parse_address").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseAddress)->GetFunction(context).ToLocalChecked()
);
exports->Set(
context,
Nan::New("parseAddress").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseAddress)->GetFunction(context).ToLocalChecked()
);
#if NODE_MAJOR_VERSION >= 12
node::Environment* env = node::GetCurrentEnvironment(Nan::GetCurrentContext());
node::AtExit(env, cleanup, NULL);
#else
node::AtExit(cleanup);
#endif
}
NODE_MODULE(parser, init)