Skip to content

Commit d77c3ee

Browse files
authored
Support mjs modules (#34)
This reshapes the source files to make index.mjs the source of truth and inlines the flow definitions using flow comments (removing the need for a separate file). A new babel based build step replaces the mjs exports with cjs exports to produce an index.js artifact (which is no longer checked in) for distribution.
1 parent d576f74 commit d77c3ee

File tree

7 files changed

+1460
-668
lines changed

7 files changed

+1460
-668
lines changed

.flowconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
[options]
22
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
33
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe
4+
module.file_ext=.js
5+
module.file_ext=.mjs

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/coverage
66
/node_modules
77
npm-debug.log
8+
/index.js

index.js.flow

Lines changed: 0 additions & 77 deletions
This file was deleted.

index.js renamed to index.mjs

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.
77
*
8+
* @flow
89
* @ignore
910
*/
1011

@@ -35,8 +36,11 @@
3536
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable|MDN Iteration protocols}
3637
*/
3738

39+
// In ES2015 environments, Symbol exists
40+
var SYMBOL /*: any */ = typeof Symbol === 'function' ? Symbol : void 0
41+
3842
// In ES2015 (or a polyfilled) environment, this will be Symbol.iterator
39-
var SYMBOL_ITERATOR = typeof Symbol === 'function' && Symbol.iterator
43+
var SYMBOL_ITERATOR = SYMBOL && SYMBOL.iterator
4044

4145
/**
4246
* A property name to be used as the name of an Iterable's method responsible
@@ -76,8 +80,8 @@ var SYMBOL_ITERATOR = typeof Symbol === 'function' && Symbol.iterator
7680
*
7781
* @type {Symbol|string}
7882
*/
79-
var $$iterator = SYMBOL_ITERATOR || '@@iterator'
80-
exports.$$iterator = $$iterator
83+
/*:: declare export var $$iterator: '@@iterator'; */
84+
export var $$iterator = SYMBOL_ITERATOR || '@@iterator'
8185

8286
/**
8387
* Returns true if the provided object implements the Iterator protocol via
@@ -96,10 +100,10 @@ exports.$$iterator = $$iterator
96100
* A value which might implement the Iterable protocol.
97101
* @return {boolean} true if Iterable.
98102
*/
99-
function isIterable(obj) {
103+
/*:: declare export function isIterable(obj: any): boolean; */
104+
export function isIterable(obj) {
100105
return !!getIteratorMethod(obj)
101106
}
102-
exports.isIterable = isIterable
103107

104108
/**
105109
* Returns true if the provided object implements the Array-like protocol via
@@ -118,11 +122,11 @@ exports.isIterable = isIterable
118122
* A value which might implement the Array-like protocol.
119123
* @return {boolean} true if Array-like.
120124
*/
121-
function isArrayLike(obj) {
125+
/*:: declare export function isArrayLike(obj: any): boolean; */
126+
export function isArrayLike(obj) {
122127
var length = obj != null && obj.length
123128
return typeof length === 'number' && length >= 0 && length % 1 === 0
124129
}
125-
exports.isArrayLike = isArrayLike
126130

127131
/**
128132
* Returns true if the provided object is an Object (i.e. not a string literal)
@@ -155,10 +159,10 @@ exports.isArrayLike = isArrayLike
155159
* An Object value which might implement the Iterable or Array-like protocols.
156160
* @return {boolean} true if Iterable or Array-like Object.
157161
*/
158-
function isCollection(obj) {
162+
/*:: declare export function isCollection(obj: any): boolean; */
163+
export function isCollection(obj) {
159164
return Object(obj) === obj && (isArrayLike(obj) || isIterable(obj))
160165
}
161-
exports.isCollection = isCollection
162166

163167
/**
164168
* If the provided object implements the Iterator protocol, its Iterator object
@@ -178,13 +182,15 @@ exports.isCollection = isCollection
178182
* An Iterable object which is the source of an Iterator.
179183
* @return {Iterator<T>} new Iterator instance.
180184
*/
181-
function getIterator(iterable) {
185+
/*:: declare export var getIterator:
186+
& (<+TValue>(iterable: Iterable<TValue>) => Iterator<TValue>)
187+
& ((iterable: mixed) => void | Iterator<mixed>); */
188+
export function getIterator(iterable) {
182189
var method = getIteratorMethod(iterable)
183190
if (method) {
184191
return method.call(iterable)
185192
}
186193
}
187-
exports.getIterator = getIterator
188194

189195
/**
190196
* If the provided object implements the Iterator protocol, the method
@@ -207,7 +213,10 @@ exports.getIterator = getIterator
207213
* An Iterable object which defines an `@@iterator` method.
208214
* @return {function(): Iterator<T>} `@@iterator` method.
209215
*/
210-
function getIteratorMethod(iterable) {
216+
/*:: declare export var getIteratorMethod:
217+
& (<+TValue>(iterable: Iterable<TValue>) => (() => Iterator<TValue>))
218+
& ((iterable: mixed) => (void | (() => Iterator<mixed>))); */
219+
export function getIteratorMethod(iterable) {
211220
if (iterable != null) {
212221
var method =
213222
(SYMBOL_ITERATOR && iterable[SYMBOL_ITERATOR]) || iterable['@@iterator']
@@ -216,7 +225,6 @@ function getIteratorMethod(iterable) {
216225
}
217226
}
218227
}
219-
exports.getIteratorMethod = getIteratorMethod
220228

221229
/**
222230
* Similar to {@link getIterator}, this method returns a new Iterator given an
@@ -246,7 +254,11 @@ exports.getIteratorMethod = getIteratorMethod
246254
* An Iterable or Array-like object to produce an Iterator.
247255
* @return {Iterator<T>} new Iterator instance.
248256
*/
249-
function createIterator(collection) {
257+
/*:: declare export var createIterator:
258+
& (<+TValue>(collection: Iterable<TValue>) => Iterator<TValue>)
259+
& ((collection: {length: number}) => Iterator<mixed>)
260+
& ((collection: mixed) => (void | Iterator<mixed>)); */
261+
export function createIterator(collection) {
250262
if (collection != null) {
251263
var iterator = getIterator(collection)
252264
if (iterator) {
@@ -257,7 +269,6 @@ function createIterator(collection) {
257269
}
258270
}
259271
}
260-
exports.createIterator = createIterator
261272

262273
// When the object provided to `createIterator` is not Iterable but is
263274
// Array-like, this simple Iterator is created.
@@ -326,7 +337,18 @@ ArrayLikeIterator.prototype.next = function() {
326337
* @param [thisArg]
327338
* Optional. Value to use as `this` when executing `callback`.
328339
*/
329-
function forEach(collection, callback, thisArg) {
340+
/*:: declare export var forEach:
341+
& (<+TValue, TCollection: Iterable<TValue>>(
342+
collection: TCollection,
343+
callbackFn: (value: TValue, index: number, collection: TCollection) => any,
344+
thisArg?: any
345+
) => void)
346+
& (<TCollection: {length: number}>(
347+
collection: TCollection,
348+
callbackFn: (value: mixed, index: number, collection: TCollection) => any,
349+
thisArg?: any
350+
) => void); */
351+
export function forEach(collection, callback, thisArg) {
330352
if (collection != null) {
331353
if (typeof collection.forEach === 'function') {
332354
return collection.forEach(callback, thisArg)
@@ -353,7 +375,6 @@ function forEach(collection, callback, thisArg) {
353375
}
354376
}
355377
}
356-
exports.forEach = forEach
357378

358379
/////////////////////////////////////////////////////
359380
// //
@@ -396,7 +417,7 @@ exports.forEach = forEach
396417
*/
397418

398419
// In ES2017 (or a polyfilled) environment, this will be Symbol.asyncIterator
399-
var SYMBOL_ASYNC_ITERATOR = typeof Symbol === 'function' && Symbol.asyncIterator
420+
var SYMBOL_ASYNC_ITERATOR = SYMBOL && SYMBOL.asyncIterator
400421

401422
/**
402423
* A property name to be used as the name of an AsyncIterable's method
@@ -441,8 +462,8 @@ var SYMBOL_ASYNC_ITERATOR = typeof Symbol === 'function' && Symbol.asyncIterator
441462
*
442463
* @type {Symbol|string}
443464
*/
444-
var $$asyncIterator = SYMBOL_ASYNC_ITERATOR || '@@asyncIterator'
445-
exports.$$asyncIterator = $$asyncIterator
465+
/*:: declare export var $$asyncIterator: '@@asyncIterator'; */
466+
export var $$asyncIterator = SYMBOL_ASYNC_ITERATOR || '@@asyncIterator'
446467

447468
/**
448469
* Returns true if the provided object implements the AsyncIterator protocol via
@@ -458,10 +479,10 @@ exports.$$asyncIterator = $$asyncIterator
458479
* A value which might implement the AsyncIterable protocol.
459480
* @return {boolean} true if AsyncIterable.
460481
*/
461-
function isAsyncIterable(obj) {
482+
/*:: declare export function isAsyncIterable(obj: any): boolean; */
483+
export function isAsyncIterable(obj) {
462484
return !!getAsyncIteratorMethod(obj)
463485
}
464-
exports.isAsyncIterable = isAsyncIterable
465486

466487
/**
467488
* If the provided object implements the AsyncIterator protocol, its
@@ -481,13 +502,15 @@ exports.isAsyncIterable = isAsyncIterable
481502
* An AsyncIterable object which is the source of an AsyncIterator.
482503
* @return {AsyncIterator<T>} new AsyncIterator instance.
483504
*/
484-
function getAsyncIterator(asyncIterable) {
505+
/*:: declare export var getAsyncIterator:
506+
& (<+TValue>(asyncIterable: AsyncIterable<TValue>) => AsyncIterator<TValue>)
507+
& ((asyncIterable: mixed) => (void | AsyncIterator<mixed>)); */
508+
export function getAsyncIterator(asyncIterable) {
485509
var method = getAsyncIteratorMethod(asyncIterable)
486510
if (method) {
487511
return method.call(asyncIterable)
488512
}
489513
}
490-
exports.getAsyncIterator = getAsyncIterator
491514

492515
/**
493516
* If the provided object implements the AsyncIterator protocol, the method
@@ -509,7 +532,10 @@ exports.getAsyncIterator = getAsyncIterator
509532
* An AsyncIterable object which defines an `@@asyncIterator` method.
510533
* @return {function(): AsyncIterator<T>} `@@asyncIterator` method.
511534
*/
512-
function getAsyncIteratorMethod(asyncIterable) {
535+
/*:: declare export var getAsyncIteratorMethod:
536+
& (<+TValue>(asyncIterable: AsyncIterable<TValue>) => (() => AsyncIterator<TValue>))
537+
& ((asyncIterable: mixed) => (void | (() => AsyncIterator<mixed>))); */
538+
export function getAsyncIteratorMethod(asyncIterable) {
513539
if (asyncIterable != null) {
514540
var method =
515541
(SYMBOL_ASYNC_ITERATOR && asyncIterable[SYMBOL_ASYNC_ITERATOR]) ||
@@ -519,7 +545,6 @@ function getAsyncIteratorMethod(asyncIterable) {
519545
}
520546
}
521547
}
522-
exports.getAsyncIteratorMethod = getAsyncIteratorMethod
523548

524549
/**
525550
* Similar to {@link getAsyncIterator}, this method returns a new AsyncIterator
@@ -554,7 +579,13 @@ exports.getAsyncIteratorMethod = getAsyncIteratorMethod
554579
* An AsyncIterable, Iterable, or Array-like object to produce an Iterator.
555580
* @return {AsyncIterator<T>} new AsyncIterator instance.
556581
*/
557-
function createAsyncIterator(source) {
582+
/*:: declare export var createAsyncIterator:
583+
& (<+TValue>(
584+
collection: Iterable<Promise<TValue> | TValue> | AsyncIterable<TValue>
585+
) => AsyncIterator<TValue>)
586+
& ((collection: {length: number}) => AsyncIterator<mixed>)
587+
& ((collection: mixed) => (void | AsyncIterator<mixed>)); */
588+
export function createAsyncIterator(source) {
558589
if (source != null) {
559590
var asyncIterator = getAsyncIterator(source)
560591
if (asyncIterator) {
@@ -566,7 +597,6 @@ function createAsyncIterator(source) {
566597
}
567598
}
568599
}
569-
exports.createAsyncIterator = createAsyncIterator
570600

571601
// When the object provided to `createAsyncIterator` is not AsyncIterable but is
572602
// sync Iterable, this simple wrapper is created.
@@ -632,7 +662,18 @@ AsyncFromSyncIterator.prototype.next = function() {
632662
* @param [thisArg]
633663
* Optional. Value to use as `this` when executing `callback`.
634664
*/
635-
function forAwaitEach(source, callback, thisArg) {
665+
/*:: declare export var forAwaitEach:
666+
& (<+TValue, TCollection: Iterable<Promise<TValue> | TValue> | AsyncIterable<TValue>>(
667+
collection: TCollection,
668+
callbackFn: (value: TValue, index: number, collection: TCollection) => any,
669+
thisArg?: any
670+
) => Promise<void>)
671+
& (<TCollection: { length: number }>(
672+
collection: TCollection,
673+
callbackFn: (value: mixed, index: number, collection: TCollection) => any,
674+
thisArg?: any
675+
) => Promise<void>); */
676+
export function forAwaitEach(source, callback, thisArg) {
636677
var asyncIterator = createAsyncIterator(source)
637678
if (asyncIterator) {
638679
var i = 0
@@ -659,4 +700,3 @@ function forAwaitEach(source, callback, thisArg) {
659700
})
660701
}
661702
}
662-
exports.forAwaitEach = forAwaitEach

0 commit comments

Comments
 (0)