Skip to content

Commit 4d09085

Browse files
committed
lib: avoid using forEach in LazyTransform
PR-URL: #11582 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a993beb commit 4d09085

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

lib/internal/streams/lazy_transform.js

+41-25
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,47 @@ function LazyTransform(options) {
1414
}
1515
util.inherits(LazyTransform, stream.Transform);
1616

17-
[
18-
'_readableState',
19-
'_writableState',
20-
'_transformState'
21-
].forEach(function(prop, i, props) {
22-
Object.defineProperty(LazyTransform.prototype, prop, {
23-
get: function() {
24-
stream.Transform.call(this, this._options);
25-
this._writableState.decodeStrings = false;
26-
27-
if (!this._options || !this._options.defaultEncoding) {
28-
this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING;
29-
}
30-
31-
return this[prop];
32-
},
33-
set: function(val) {
34-
Object.defineProperty(this, prop, {
35-
value: val,
36-
enumerable: true,
37-
configurable: true,
38-
writable: true
39-
});
40-
},
17+
function makeGetter(name) {
18+
return function() {
19+
stream.Transform.call(this, this._options);
20+
this._writableState.decodeStrings = false;
21+
22+
if (!this._options || !this._options.defaultEncoding) {
23+
this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING;
24+
}
25+
26+
return this[name];
27+
};
28+
}
29+
30+
function makeSetter(name) {
31+
return function(val) {
32+
Object.defineProperty(this, name, {
33+
value: val,
34+
enumerable: true,
35+
configurable: true,
36+
writable: true
37+
});
38+
};
39+
}
40+
41+
Object.defineProperties(LazyTransform.prototype, {
42+
_readableState: {
43+
get: makeGetter('_readableState'),
44+
set: makeSetter('_readableState'),
45+
configurable: true,
46+
enumerable: true
47+
},
48+
_writableState: {
49+
get: makeGetter('_writableState'),
50+
set: makeSetter('_writableState'),
51+
configurable: true,
52+
enumerable: true
53+
},
54+
_transformState: {
55+
get: makeGetter('_transformState'),
56+
set: makeSetter('_transformState'),
4157
configurable: true,
4258
enumerable: true
43-
});
59+
}
4460
});

0 commit comments

Comments
 (0)