-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
{ Iterator, AsyncIterator }.prototype.flatMap
supports returning bo…
…th - iterables and iterators tc39/proposal-iterator-helpers#233
- Loading branch information
Showing
9 changed files
with
68 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/core-js/internals/get-async-iterator-flattenable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var call = require('../internals/function-call'); | ||
var isCallable = require('../internals/is-callable'); | ||
var toObject = require('../internals/to-object'); | ||
var getIteratorDirect = require('../internals/get-iterator-direct'); | ||
var getIteratorMethod = require('../internals/get-iterator-method'); | ||
var getMethod = require('../internals/get-method'); | ||
var wellKnownSymbol = require('../internals/well-known-symbol'); | ||
var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator'); | ||
|
||
var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator'); | ||
|
||
module.exports = function from(obj) { | ||
var object = toObject(obj); | ||
var alreadyAsync = true; | ||
var method = getMethod(object, ASYNC_ITERATOR); | ||
var iterator; | ||
if (!isCallable(method)) { | ||
method = getIteratorMethod(object); | ||
alreadyAsync = false; | ||
} | ||
if (isCallable(method)) { | ||
iterator = call(method, object); | ||
} else { | ||
iterator = object; | ||
alreadyAsync = true; | ||
} | ||
return getIteratorDirect(alreadyAsync ? iterator : new AsyncFromSyncIterator(getIteratorDirect(iterator))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var call = require('../internals/function-call'); | ||
var isCallable = require('../internals/is-callable'); | ||
var toObject = require('../internals/to-object'); | ||
var getIteratorDirect = require('../internals/get-iterator-direct'); | ||
var getIteratorMethod = require('../internals/get-iterator-method'); | ||
|
||
module.exports = function (obj) { | ||
var object = toObject(obj); | ||
var method = getIteratorMethod(object); | ||
return getIteratorDirect(isCallable(method) ? call(method, object) : object); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,15 @@ | ||
// https://github.com/tc39/proposal-iterator-helpers | ||
var $ = require('../internals/export'); | ||
var toObject = require('../internals/to-object'); | ||
var isPrototypeOf = require('../internals/object-is-prototype-of'); | ||
var getAsyncIteratorFlattenable = require('../internals/get-async-iterator-flattenable'); | ||
var AsyncIteratorPrototype = require('../internals/async-iterator-prototype'); | ||
var getAsyncIterator = require('../internals/get-async-iterator'); | ||
var getIterator = require('../internals/get-iterator'); | ||
var getIteratorDirect = require('../internals/get-iterator-direct'); | ||
var getIteratorMethod = require('../internals/get-iterator-method'); | ||
var getMethod = require('../internals/get-method'); | ||
var wellKnownSymbol = require('../internals/well-known-symbol'); | ||
var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator'); | ||
var WrapAsyncIterator = require('../internals/async-iterator-wrap'); | ||
|
||
var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator'); | ||
|
||
$({ target: 'AsyncIterator', stat: true, forced: true }, { | ||
from: function from(O) { | ||
var object = toObject(O); | ||
var usingIterator = getMethod(object, ASYNC_ITERATOR); | ||
var iteratorRecord; | ||
if (usingIterator) { | ||
iteratorRecord = getIteratorDirect(getAsyncIterator(object, usingIterator)); | ||
if (isPrototypeOf(AsyncIteratorPrototype, iteratorRecord.iterator)) return iteratorRecord.iterator; | ||
} | ||
if (iteratorRecord === undefined) { | ||
usingIterator = getIteratorMethod(object); | ||
if (usingIterator) iteratorRecord = getIteratorDirect(new AsyncFromSyncIterator( | ||
getIteratorDirect(getIterator(object, usingIterator)) | ||
)); | ||
} | ||
return new WrapAsyncIterator(iteratorRecord !== undefined ? iteratorRecord : getIteratorDirect(object)); | ||
var iteratorRecord = getAsyncIteratorFlattenable(O); | ||
return isPrototypeOf(AsyncIteratorPrototype, iteratorRecord.iterator) | ||
? iteratorRecord.iterator | ||
: new WrapAsyncIterator(iteratorRecord); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,20 @@ | ||
// https://github.com/tc39/proposal-iterator-helpers | ||
var $ = require('../internals/export'); | ||
var call = require('../internals/function-call'); | ||
var toObject = require('../internals/to-object'); | ||
var isPrototypeOf = require('../internals/object-is-prototype-of'); | ||
var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype; | ||
var createIteratorProxy = require('../internals/iterator-create-proxy'); | ||
var getIterator = require('../internals/get-iterator'); | ||
var getIteratorDirect = require('../internals/get-iterator-direct'); | ||
var getIteratorMethod = require('../internals/get-iterator-method'); | ||
var getIteratorFlattenable = require('../internals/get-iterator-flattenable'); | ||
|
||
var IteratorProxy = createIteratorProxy(function () { | ||
return call(this.next, this.iterator); | ||
}, true); | ||
|
||
$({ target: 'Iterator', stat: true, forced: true }, { | ||
from: function from(O) { | ||
var object = toObject(O); | ||
var usingIterator = getIteratorMethod(object); | ||
var iteratorRecord; | ||
if (usingIterator) { | ||
iteratorRecord = getIteratorDirect(getIterator(object, usingIterator)); | ||
if (isPrototypeOf(IteratorPrototype, iteratorRecord.iterator)) return iteratorRecord.iterator; | ||
} else { | ||
iteratorRecord = getIteratorDirect(object); | ||
} return new IteratorProxy(iteratorRecord); | ||
var iteratorRecord = getIteratorFlattenable(O); | ||
return isPrototypeOf(IteratorPrototype, iteratorRecord.iterator) | ||
? iteratorRecord.iterator | ||
: new IteratorProxy(iteratorRecord); | ||
} | ||
}); |