Skip to content

Commit a9749a5

Browse files
committed
src: simplify URLPatternInit::ToJsObject by reducing boilerplate
1 parent 2c24d77 commit a9749a5

File tree

1 file changed

+18
-62
lines changed

1 file changed

+18
-62
lines changed

src/node_url_pattern.cc

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -242,68 +242,24 @@ MaybeLocal<Value> URLPattern::URLPatternInit::ToJsObject(
242242
auto isolate = env->isolate();
243243
auto context = env->context();
244244
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-
}
245+
246+
const auto trySet = [&](auto name, const std::optional<std::string>& val) {
247+
if (!val.has_value()) return true;
248+
Local<Value> temp;
249+
return ToV8Value(context, *val).ToLocal(&temp) &&
250+
result->Set(context, name, temp).IsJust();
251+
};
252+
253+
if (!trySet(env->protocol_string(), init.protocol) ||
254+
!trySet(env->username_string(), init.username) ||
255+
!trySet(env->password_string(), init.password) ||
256+
!trySet(env->hostname_string(), init.hostname) ||
257+
!trySet(env->port_string(), init.port) ||
258+
!trySet(env->pathname_string(), init.pathname) ||
259+
!trySet(env->search_string(), init.search) ||
260+
!trySet(env->hash_string(), init.hash) ||
261+
!trySet(env->base_url_string(), init.base_url)) {
262+
return {};
307263
}
308264
return result;
309265
}

0 commit comments

Comments
 (0)