Skip to content

Commit 120bc28

Browse files
committed
src: simplify URLPatternInit::ToJsObject by reducing boilerplate
1 parent 00f904f commit 120bc28

File tree

1 file changed

+20
-62
lines changed

1 file changed

+20
-62
lines changed

src/node_url_pattern.cc

+20-62
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ void URLPattern::MemoryInfo(MemoryTracker* tracker) const {
130130
tracker->TrackField("pathname", url_pattern_.get_pathname());
131131
tracker->TrackField("search", url_pattern_.get_search());
132132
tracker->TrackField("hash", url_pattern_.get_hash());
133+
tracker->TrackFieldWithSize(
134+
"pattern", sizeof(url_pattern_), "ada:url_pattern");
133135
}
134136

135137
void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
@@ -242,68 +244,24 @@ MaybeLocal<Value> URLPattern::URLPatternInit::ToJsObject(
242244
auto isolate = env->isolate();
243245
auto context = env->context();
244246
auto result = Object::New(isolate);
245-
if (init.protocol) {
246-
Local<Value> protocol;
247-
if (!ToV8Value(context, *init.protocol).ToLocal(&protocol) ||
248-
result->Set(context, env->protocol_string(), protocol).IsNothing()) {
249-
return {};
250-
}
251-
}
252-
if (init.username) {
253-
Local<Value> username;
254-
if (!ToV8Value(context, *init.username).ToLocal(&username) ||
255-
result->Set(context, env->username_string(), username).IsNothing()) {
256-
return {};
257-
}
258-
}
259-
if (init.password) {
260-
Local<Value> password;
261-
if (!ToV8Value(context, *init.password).ToLocal(&password) ||
262-
result->Set(context, env->password_string(), password).IsNothing()) {
263-
return {};
264-
}
265-
}
266-
if (init.hostname) {
267-
Local<Value> hostname;
268-
if (!ToV8Value(context, *init.hostname).ToLocal(&hostname) ||
269-
result->Set(context, env->hostname_string(), hostname).IsNothing()) {
270-
return {};
271-
}
272-
}
273-
if (init.port) {
274-
Local<Value> port;
275-
if (!ToV8Value(context, *init.port).ToLocal(&port) ||
276-
result->Set(context, env->port_string(), port).IsNothing()) {
277-
return {};
278-
}
279-
}
280-
if (init.pathname) {
281-
Local<Value> pathname;
282-
if (!ToV8Value(context, *init.pathname).ToLocal(&pathname) ||
283-
result->Set(context, env->pathname_string(), pathname).IsNothing()) {
284-
return {};
285-
}
286-
}
287-
if (init.search) {
288-
Local<Value> search;
289-
if (!ToV8Value(context, *init.search).ToLocal(&search) ||
290-
result->Set(context, env->search_string(), search).IsNothing()) {
291-
return {};
292-
}
293-
}
294-
if (init.hash) {
295-
Local<Value> hash;
296-
if (!ToV8Value(context, *init.hash).ToLocal(&hash) ||
297-
result->Set(context, env->hash_string(), hash).IsNothing()) {
298-
return {};
299-
}
300-
}
301-
if (init.base_url) {
302-
Local<Value> base_url;
303-
if (!ToV8Value(context, *init.base_url).ToLocal(&base_url) ||
304-
result->Set(context, env->base_url_string(), base_url).IsNothing()) {
305-
return {};
306-
}
247+
248+
const auto trySet = [&](auto name, const std::optional<std::string>& val) {
249+
if (!val.has_value()) return true;
250+
Local<Value> temp;
251+
return ToV8Value(context, *val).ToLocal(&temp) &&
252+
result->Set(context, name, temp).IsJust();
253+
};
254+
255+
if (!trySet(env->protocol_string(), init.protocol) ||
256+
!trySet(env->username_string(), init.username) ||
257+
!trySet(env->password_string(), init.password) ||
258+
!trySet(env->hostname_string(), init.hostname) ||
259+
!trySet(env->port_string(), init.port) ||
260+
!trySet(env->pathname_string(), init.pathname) ||
261+
!trySet(env->search_string(), init.search) ||
262+
!trySet(env->hash_string(), init.hash) ||
263+
!trySet(env->base_url_string(), init.base_url)) {
264+
return {};
307265
}
308266
return result;
309267
}

0 commit comments

Comments
 (0)