ig-types 6.11.0 → 6.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Array.js +1 -0
- package/Promise.js +107 -71
- package/README.md +49 -3
- package/RegExp.js +2 -1
- package/generator.js +1 -0
- package/package.json +1 -1
package/Array.js
CHANGED
|
@@ -492,6 +492,7 @@ object.Mixin('ArrayProtoMixin', 'soft', {
|
|
|
492
492
|
smap: stoppableList('map'),
|
|
493
493
|
sfilter: stoppableList('filter'),
|
|
494
494
|
sreduce: stoppableValue('reduce'),
|
|
495
|
+
sreduceRight: stoppableValue('reduceRight'),
|
|
495
496
|
sforEach: stoppableValue('map', true),
|
|
496
497
|
|
|
497
498
|
// Chunk iteration...
|
package/Promise.js
CHANGED
|
@@ -80,12 +80,13 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
80
80
|
// NOTE: these are different to Array's equivalents in that the handler
|
|
81
81
|
// is called not in the order of the elements but rather in order
|
|
82
82
|
// of promise resolution...
|
|
83
|
-
// NOTE: index of items is unknowable because items can expand
|
|
83
|
+
// NOTE: index of items is unknowable because items can expand and
|
|
84
84
|
// contract depending on handlrs (e.g. .filter(..) can remove
|
|
85
85
|
// items)...
|
|
86
86
|
// This the following can not be implemented here:
|
|
87
87
|
// .slice(..)
|
|
88
88
|
// .splice(..)
|
|
89
|
+
// .values() / .keys()
|
|
89
90
|
// .at(..)
|
|
90
91
|
// [Symbol.iterator]() - needs to be sync...
|
|
91
92
|
// The followng methods are questionable:
|
|
@@ -93,36 +94,65 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
93
94
|
// .includes(..)
|
|
94
95
|
// .some(..) / .every(..)
|
|
95
96
|
// .sort(..)
|
|
97
|
+
//
|
|
96
98
|
// XXX should these support STOP???
|
|
97
99
|
map: function(func){
|
|
98
|
-
return this.constructor(this
|
|
100
|
+
return this.constructor(this,
|
|
99
101
|
function(e){
|
|
100
102
|
return [func(e)] }) },
|
|
101
103
|
filter: function(func){
|
|
102
|
-
return this.constructor(this
|
|
104
|
+
return this.constructor(this,
|
|
103
105
|
function(e){
|
|
104
106
|
return func(e) ?
|
|
105
107
|
[e]
|
|
106
108
|
: [] }) },
|
|
109
|
+
// NOTE: this does not return an iterable promise as we can't know
|
|
110
|
+
// what the user reduces to...
|
|
111
|
+
// XXX we could look at the initial state though...
|
|
112
|
+
// NOTE: the items can be handled out of order because the nested
|
|
113
|
+
// promises can resolve in any order.
|
|
114
|
+
// XXX write how to go around this...
|
|
115
|
+
// NOTE: since order of execution can not be guaranteed there is no
|
|
116
|
+
// point in implementing .reduceRight(..)
|
|
107
117
|
reduce: function(func, res){
|
|
108
|
-
return this.constructor(this
|
|
118
|
+
return this.constructor(this,
|
|
109
119
|
function(e){
|
|
110
120
|
res = func(res, e)
|
|
111
121
|
return [] })
|
|
112
122
|
.then(function(){
|
|
113
123
|
return res }) },
|
|
114
124
|
flat: function(depth=1){
|
|
115
|
-
return this.constructor(this
|
|
125
|
+
return this.constructor(this,
|
|
116
126
|
function(e){
|
|
117
|
-
return (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
return (depth > 1
|
|
128
|
+
&& e != null
|
|
129
|
+
&& e.flat) ?
|
|
130
|
+
e.flat(depth-1)
|
|
131
|
+
: depth != 0 ?
|
|
132
|
+
e
|
|
133
|
+
: [e] }) },
|
|
134
|
+
reverse: function(){
|
|
135
|
+
var lst = this.__list
|
|
136
|
+
return this.constructor(
|
|
137
|
+
lst instanceof Promise ?
|
|
138
|
+
lst.then(function(elems){
|
|
139
|
+
return elems instanceof Array ?
|
|
140
|
+
elems.slice()
|
|
141
|
+
.reverse()
|
|
142
|
+
: elems })
|
|
143
|
+
: lst
|
|
144
|
+
.map(function(elems){
|
|
145
|
+
return elems instanceof Array ?
|
|
146
|
+
elems.slice()
|
|
147
|
+
.reverse()
|
|
148
|
+
: elems instanceof Promise ?
|
|
149
|
+
elems.then(function(elems){
|
|
150
|
+
return elems.reverse() })
|
|
151
|
+
: elems })
|
|
152
|
+
.reverse(),
|
|
153
|
+
'raw') },
|
|
154
|
+
|
|
155
|
+
// XXX do we need these?
|
|
126
156
|
// .pop()
|
|
127
157
|
// .shift()
|
|
128
158
|
// .first() / .last()
|
|
@@ -182,8 +212,8 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
182
212
|
//
|
|
183
213
|
// Spectial cases usefull for extending this constructor...
|
|
184
214
|
//
|
|
185
|
-
//
|
|
186
|
-
// Promise.iter([ .. ],
|
|
215
|
+
// Set raw .__list without any pre-processing...
|
|
216
|
+
// Promise.iter([ .. ], 'raw')
|
|
187
217
|
// -> iterable-promise
|
|
188
218
|
//
|
|
189
219
|
// Create a rejected iterator...
|
|
@@ -204,9 +234,8 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
204
234
|
// - another issue here is that the stop would happen in order of
|
|
205
235
|
// execution and not order of elements...
|
|
206
236
|
__new__: function(_, list, handler){
|
|
207
|
-
var promise
|
|
208
|
-
|
|
209
237
|
// instance...
|
|
238
|
+
var promise
|
|
210
239
|
var obj = Reflect.construct(
|
|
211
240
|
IterablePromise.__proto__,
|
|
212
241
|
[function(resolve, reject){
|
|
@@ -220,49 +249,47 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
220
249
|
IterablePromise)
|
|
221
250
|
|
|
222
251
|
if(promise){
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
//
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
:
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
// clone...
|
|
265
|
-
: list.slice()
|
|
252
|
+
|
|
253
|
+
if(handler != 'raw'){
|
|
254
|
+
handler = handler
|
|
255
|
+
?? function(e){
|
|
256
|
+
return [e] }
|
|
257
|
+
|
|
258
|
+
// NOTE: this is recursive to handle expanding nested promises...
|
|
259
|
+
var handle = function(elem){
|
|
260
|
+
// call the handler...
|
|
261
|
+
return (elem && elem.then) ?
|
|
262
|
+
//elem.then(function(elem){
|
|
263
|
+
// return handler(elem) })
|
|
264
|
+
elem.then(handler)
|
|
265
|
+
: handler(elem) }
|
|
266
|
+
|
|
267
|
+
// handle the list...
|
|
268
|
+
// NOTE: we can't .flat() the results here as we need to
|
|
269
|
+
// wait till all the promises resolve...
|
|
270
|
+
list =
|
|
271
|
+
(list instanceof IterablePromise
|
|
272
|
+
&& !(list.__list instanceof Promise)) ?
|
|
273
|
+
// NOTE: this is essentially the same as below but
|
|
274
|
+
// with a normalized list as input...
|
|
275
|
+
// XXX can we merge the two???
|
|
276
|
+
list.__list
|
|
277
|
+
.map(function(elems){
|
|
278
|
+
return elems instanceof Promise ?
|
|
279
|
+
elems.then(function(elems){
|
|
280
|
+
return elems
|
|
281
|
+
.map(handle)
|
|
282
|
+
.flat() })
|
|
283
|
+
: elems
|
|
284
|
+
.map(handle)
|
|
285
|
+
.flat() })
|
|
286
|
+
: list instanceof Promise ?
|
|
287
|
+
// special case: promised list...
|
|
288
|
+
list.then(function(list){
|
|
289
|
+
return [list].flat()
|
|
290
|
+
.map(handle) })
|
|
291
|
+
: [list].flat()
|
|
292
|
+
.map(handle) }
|
|
266
293
|
|
|
267
294
|
Object.defineProperty(obj, '__list', {
|
|
268
295
|
value: list,
|
|
@@ -270,9 +297,14 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
270
297
|
})
|
|
271
298
|
|
|
272
299
|
// handle promise state...
|
|
273
|
-
|
|
300
|
+
;(list instanceof Promise ?
|
|
301
|
+
// special case: promised list...
|
|
302
|
+
list
|
|
303
|
+
: Promise.all([list].flat()))
|
|
274
304
|
.then(function(res){
|
|
275
|
-
promise.resolve(
|
|
305
|
+
promise.resolve(handler ?
|
|
306
|
+
res.flat()
|
|
307
|
+
: res) })
|
|
276
308
|
.catch(promise.reject) }
|
|
277
309
|
|
|
278
310
|
return obj },
|
|
@@ -417,6 +449,7 @@ object.Constructor('CooperativePromise', Promise, {
|
|
|
417
449
|
|
|
418
450
|
//---------------------------------------------------------------------
|
|
419
451
|
|
|
452
|
+
// XXX EXPEREMENTAL...
|
|
420
453
|
var ProxyPromise =
|
|
421
454
|
module.ProxyPromise =
|
|
422
455
|
object.Constructor('ProxyPromise', Promise, {
|
|
@@ -461,14 +494,6 @@ object.Constructor('ProxyPromise', Promise, {
|
|
|
461
494
|
|
|
462
495
|
//---------------------------------------------------------------------
|
|
463
496
|
|
|
464
|
-
// XXX EXPEREMENTAL...
|
|
465
|
-
var PromiseProtoMixin =
|
|
466
|
-
module.PromiseProtoMixin =
|
|
467
|
-
object.Mixin('PromiseProtoMixin', 'soft', {
|
|
468
|
-
as: ProxyPromise,
|
|
469
|
-
})
|
|
470
|
-
|
|
471
|
-
|
|
472
497
|
var PromiseMixin =
|
|
473
498
|
module.PromiseMixin =
|
|
474
499
|
object.Mixin('PromiseMixin', 'soft', {
|
|
@@ -479,7 +504,18 @@ object.Mixin('PromiseMixin', 'soft', {
|
|
|
479
504
|
|
|
480
505
|
PromiseMixin(Promise)
|
|
481
506
|
|
|
507
|
+
|
|
482
508
|
// XXX EXPEREMENTAL...
|
|
509
|
+
var PromiseProtoMixin =
|
|
510
|
+
module.PromiseProtoMixin =
|
|
511
|
+
object.Mixin('PromiseProtoMixin', 'soft', {
|
|
512
|
+
as: ProxyPromise,
|
|
513
|
+
|
|
514
|
+
// XXX
|
|
515
|
+
iter: function(){
|
|
516
|
+
return IterablePromise(this) },
|
|
517
|
+
})
|
|
518
|
+
|
|
483
519
|
PromiseProtoMixin(Promise.prototype)
|
|
484
520
|
|
|
485
521
|
|
package/README.md
CHANGED
|
@@ -70,8 +70,11 @@ Library of JavaScript type extensions, types and utilities.
|
|
|
70
70
|
- [`<promise-coop>.then(..)`](#promise-coopthen)
|
|
71
71
|
- [Promise iteration](#promise-iteration)
|
|
72
72
|
- [`Promise.iter(..)` / `promise.IterablePromise(..)`](#promiseiter--promiseiterablepromise)
|
|
73
|
+
- [`<promise>.iter()`](#promiseiter)
|
|
74
|
+
- [`<promise-iter>.iter()`](#promise-iteriter)
|
|
73
75
|
- [`<promise-iter>.map(..)` / `<promise-iter>.filter(..)` / `<promise-iter>.reduce(..)`](#promise-itermap--promise-iterfilter--promise-iterreduce)
|
|
74
76
|
- [`<promise-iter>.flat(..)`](#promise-iterflat)
|
|
77
|
+
- [`<promise-iter>.reverse()`](#promise-iterreverse)
|
|
75
78
|
- [`<promise-iter>.then(..)` / `<promise-iter>.catch(..)` / `<promise-iter>.finally(..)`](#promise-iterthen--promise-itercatch--promise-iterfinally)
|
|
76
79
|
- [Advanced handler](#advanced-handler)
|
|
77
80
|
- [Promise proxies](#promise-proxies)
|
|
@@ -1452,8 +1455,8 @@ promise, and it is similar to a _generator_ in that it allows iteration over the
|
|
|
1452
1455
|
contained values and chaining of operations but unlike `Promise.all(..)` this
|
|
1453
1456
|
iteration occurs depth-first instead of breadth first.
|
|
1454
1457
|
|
|
1455
|
-
|
|
1456
|
-
|
|
1458
|
+
One can think of _promise iterators_ vs. _generators_ as the former being
|
|
1459
|
+
internally controlled and asynchronous while the later being externally
|
|
1457
1460
|
controlled and synchronous.
|
|
1458
1461
|
|
|
1459
1462
|
Here is a traditional example using `Promise.all(..)`:
|
|
@@ -1515,8 +1518,32 @@ XXX should we support infinite generators as input?
|
|
|
1515
1518
|
#### `Promise.iter(..)` / `promise.IterablePromise(..)`
|
|
1516
1519
|
|
|
1517
1520
|
Create an _iterable promise_
|
|
1521
|
+
|
|
1518
1522
|
```bnf
|
|
1519
1523
|
Promise.iter(<array>)
|
|
1524
|
+
Promise.iter(<promise>)
|
|
1525
|
+
-> <promise-iter>
|
|
1526
|
+
```
|
|
1527
|
+
|
|
1528
|
+
#### `<promise>.iter()`
|
|
1529
|
+
|
|
1530
|
+
Wrap a promise in an promise iterator.
|
|
1531
|
+
|
|
1532
|
+
```bnf
|
|
1533
|
+
<promise>.iter()
|
|
1534
|
+
-> <promise-iter>
|
|
1535
|
+
```
|
|
1536
|
+
|
|
1537
|
+
If `<promise>` resolves to a non-array value it will be treated as a single
|
|
1538
|
+
element, otherwise the array will be iterated over.
|
|
1539
|
+
|
|
1540
|
+
|
|
1541
|
+
#### `<promise-iter>.iter()`
|
|
1542
|
+
|
|
1543
|
+
Return a shallow copy of the current promise iterator.
|
|
1544
|
+
|
|
1545
|
+
```bnf
|
|
1546
|
+
<promise-iter>.iter()
|
|
1520
1547
|
-> <promise-iter>
|
|
1521
1548
|
```
|
|
1522
1549
|
|
|
@@ -1545,7 +1572,7 @@ and [`.reduce(..)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
|
|
|
1545
1572
|
|
|
1546
1573
|
```bnf
|
|
1547
1574
|
<promise-iter>.reduce(<handler>, <state>)
|
|
1548
|
-
-> <promise
|
|
1575
|
+
-> <promise>
|
|
1549
1576
|
|
|
1550
1577
|
<handler>(<state>, <elem>)
|
|
1551
1578
|
-> <state>
|
|
@@ -1558,6 +1585,15 @@ Note that these are different to `Array`'s equivalents in some details:
|
|
|
1558
1585
|
this is because the index with _out-of-order_ and _depth-first_ execution the
|
|
1559
1586
|
index is unknowable and the container is a promise/black-box.
|
|
1560
1587
|
|
|
1588
|
+
This is especially critical for `.reduce(..)` as iteration in an order different
|
|
1589
|
+
from the order of elements _can_ affect actual result if this is not expected.
|
|
1590
|
+
|
|
1591
|
+
`.reduce(..)` is also a bit different here in that it will return a basic
|
|
1592
|
+
`<promise>` object as we can't know what will it will reduce to.
|
|
1593
|
+
|
|
1594
|
+
Note that since `.reduce(..)` order can not be guaranteed there is no point
|
|
1595
|
+
in implementing `.reduceRigth(..)`.
|
|
1596
|
+
|
|
1561
1597
|
|
|
1562
1598
|
#### `<promise-iter>.flat(..)`
|
|
1563
1599
|
|
|
@@ -1570,6 +1606,16 @@ Note that these are different to `Array`'s equivalents in some details:
|
|
|
1570
1606
|
This is similar to [`<array>.flat(..)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) see it for more info.
|
|
1571
1607
|
|
|
1572
1608
|
|
|
1609
|
+
#### `<promise-iter>.reverse()`
|
|
1610
|
+
|
|
1611
|
+
```bnf
|
|
1612
|
+
<promise-iter>.reverse()
|
|
1613
|
+
-> <promise-iter>
|
|
1614
|
+
```
|
|
1615
|
+
|
|
1616
|
+
This is similar to [`<array>.reverse(..)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) see it for more info.
|
|
1617
|
+
|
|
1618
|
+
|
|
1573
1619
|
#### `<promise-iter>.then(..)` / `<promise-iter>.catch(..)` / `<promise-iter>.finally(..)`
|
|
1574
1620
|
|
|
1575
1621
|
An extension to
|
package/RegExp.js
CHANGED
package/generator.js
CHANGED