Skip to content

Commit ff9bd33

Browse files
committed
stream: make additional minor tweaks in stream/iter
1 parent 539c824 commit ff9bd33

File tree

2 files changed

+9
-36
lines changed

2 files changed

+9
-36
lines changed

lib/internal/streams/iter/from.js

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,42 +63,23 @@ function isPrimitiveChunk(value) {
6363
return typeof value === 'string' || isArrayBuffer(value) || ArrayBufferIsView(value);
6464
}
6565

66-
/**
67-
* Check if value implements ToStreamable protocol.
68-
* @returns {boolean}
69-
*/
70-
function isToStreamable(value) {
71-
return hasProtocol(value, toStreamable);
72-
}
73-
74-
function isToAsyncStreamable(value) {
75-
return hasProtocol(value, toAsyncStreamable);
76-
}
77-
7866
/**
7967
* Check if value is a sync iterable (has Symbol.iterator).
8068
* @returns {boolean}
8169
*/
8270
function isSyncIterable(value) {
83-
return (
84-
value !== null &&
85-
typeof value === 'object' &&
86-
SymbolIterator in value &&
87-
typeof value[SymbolIterator] === 'function'
88-
);
71+
// We do not consider regular strings to be sync iterables in this context.
72+
// We don't care about boxed strings (String objects) since they are uncommon.
73+
return typeof value !== 'string' &&
74+
typeof value?.[SymbolIterator] === 'function';
8975
}
9076

9177
/**
9278
* Check if value is an async iterable (has Symbol.asyncIterator).
9379
* @returns {boolean}
9480
*/
9581
function isAsyncIterable(value) {
96-
return (
97-
value !== null &&
98-
typeof value === 'object' &&
99-
SymbolAsyncIterator in value &&
100-
typeof value[SymbolAsyncIterator] === 'function'
101-
);
82+
return typeof value?.[SymbolAsyncIterator] === 'function';
10283
}
10384

10485
// =============================================================================
@@ -138,7 +119,6 @@ function primitiveToUint8Array(chunk) {
138119
);
139120
}
140121

141-
142122
// =============================================================================
143123
// Sync Normalization (for fromSync and sync contexts)
144124
// =============================================================================
@@ -156,7 +136,7 @@ function* normalizeSyncValue(value) {
156136
}
157137

158138
// Handle ToStreamable protocol
159-
if (isToStreamable(value)) {
139+
if (hasProtocol(value, toStreamable)) {
160140
const result = FunctionPrototypeCall(value[toStreamable], value);
161141
yield* normalizeSyncValue(result);
162142
return;
@@ -260,7 +240,7 @@ async function* normalizeAsyncValue(value) {
260240
}
261241

262242
// Handle ToAsyncStreamable protocol (check before ToStreamable)
263-
if (isToAsyncStreamable(value)) {
243+
if (hasProtocol(value, toAsyncStreamable)) {
264244
const result = FunctionPrototypeCall(value[toAsyncStreamable], value);
265245
if (isPromise(result)) {
266246
yield* normalizeAsyncValue(await result);
@@ -271,7 +251,7 @@ async function* normalizeAsyncValue(value) {
271251
}
272252

273253
// Handle ToStreamable protocol
274-
if (isToStreamable(value)) {
254+
if (hasProtocol(value, toStreamable)) {
275255
const result = FunctionPrototypeCall(value[toStreamable], value);
276256
yield* normalizeAsyncValue(result);
277257
return;
@@ -593,8 +573,6 @@ module.exports = {
593573
isAsyncIterable,
594574
isPrimitiveChunk,
595575
isSyncIterable,
596-
isToAsyncStreamable,
597-
isToStreamable,
598576
isUint8ArrayBatch,
599577
normalizeAsyncSource,
600578
normalizeAsyncValue,

lib/internal/streams/iter/pull.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ const {
5858
* @returns {boolean}
5959
*/
6060
function hasMethod(value, name) {
61-
return (
62-
value !== null &&
63-
typeof value === 'object' &&
64-
name in value &&
65-
typeof value[name] === 'function'
66-
);
61+
return typeof value?.[name] === 'function';
6762
}
6863

6964
/**

0 commit comments

Comments
 (0)