Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/internal/legacy/processbinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ObjectFromEntries,
ObjectEntries,
SafeArrayIterator,
} = primordials;
const { types } = require('util');
const types = require('internal/util/types');

module.exports = {
const factories = {
util() {
return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter(
ObjectEntries(types),
Expand All @@ -34,3 +35,17 @@ module.exports = {
})));
}
};

module.exports = ObjectFromEntries(
new SafeArrayIterator(
ArrayPrototypeMap(ObjectEntries(factories), ({ 0: key, 1: factory }) => {
let result;
return [key, () => {
if (!result) {
result = factory();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!result) {
result = factory();
}
result ||= factory();

but do we want falsy or nullish?

Suggested change
if (!result) {
result = factory();
}
result ??= factory();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, it can just be return result ??= factory().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, it can just be return result ??= factory().

I don't know if we have a linter rule enforcing that, but I think we tend to avoid mixing assignments and return statements in the code base, to improve readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of assignments in return statements:

$ git grep "return .* = " lib

Since this is a smallish statement, should we prefer return result ??= factory();?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, it can just be return result ??= factory().

I don't know if we have a linter rule enforcing that, but I think we tend to avoid mixing assignments and return statements in the code base, to improve readability.

Why would that improve readability?

return result;
}];
}),
),
);
2 changes: 2 additions & 0 deletions test/parallel/test-process-binding-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ assert.deepStrictEqual(
for (const k of Object.keys(utilBinding)) {
assert.strictEqual(utilBinding[k], util.types[k]);
}

assert.strictEqual(utilBinding, process.binding('util'));