ig-types 6.18.0 → 6.20.0

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.
Files changed (4) hide show
  1. package/README.md +18 -2
  2. package/Set.js +24 -0
  3. package/generator.js +23 -16
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -49,6 +49,7 @@ Library of JavaScript type extensions, types and utilities.
49
49
  - [`<set>.replace(..)`](#setreplace)
50
50
  - [`<set>.replaceAt(..)`](#setreplaceat)
51
51
  - [`<set>.sort(..)`](#setsort)
52
+ - [`<set>.filter(..)` / `<set>.map(..)` / `<set>.forEach(..)` / `<set>.reduce(..)` / `<set>.reduceRight(..)`](#setfilter--setmap--setforeach--setreduce--setreduceright)
52
53
  - [`Date`](#date)
53
54
  - [`Date.timeStamp(..)`](#datetimestamp)
54
55
  - [`Date.fromTimeStamp(..)`](#datefromtimestamp)
@@ -110,6 +111,7 @@ Library of JavaScript type extensions, types and utilities.
110
111
  - [`<generator>.shift()` / `<generator>.pop()` / `<generator>.gshift()` / `<generator>.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop)
111
112
  - [`<generator>.unshift(..)` / `<generator>.push(..)`](#generatorunshift--generatorpush)
112
113
  - [`<generator>.join(..)`](#generatorjoin)
114
+ - [`<generator>.unwind(..)`](#generatorunwind)
113
115
  - [`<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`](#generatorthen--generatorcatch--generatorfinally)
114
116
  - [`<generator>.toArray()`](#generatortoarray)
115
117
  - [Treating iterators the same as generators](#treating-iterators-the-same-as-generators)
@@ -123,7 +125,7 @@ Library of JavaScript type extensions, types and utilities.
123
125
  - [`<Generator>.between(..)`](#generatorbetween-1)
124
126
  - [`<Generator>.toArray()`](#generatortoarray-1)
125
127
  - [`<Generator>.join(..)`](#generatorjoin-1)
126
- - [`<Generator>.then(..)` / `<Generator>.catch(..)` / `<Generator>.finally(..)`](#generatorthen--generatorcatch--generatorfinally-1)
128
+ - [`<Generator>.unwind(..)`](#generatorunwind-1)
127
129
  - [Generator combinators](#generator-combinators)
128
130
  - [`<Generator>.chain(..)` / `<generator>.chain(..)`](#generatorchain--generatorchain)
129
131
  - [`<Generator>.concat(..)` / `<generator>.concat(..)`](#generatorconcat--generatorconcat)
@@ -135,6 +137,7 @@ Library of JavaScript type extensions, types and utilities.
135
137
  - [`generator.stoppable(..)`](#generatorstoppable)
136
138
  - [Async generator extensions](#async-generator-extensions)
137
139
  - [`generator.AsyncGenerator`](#generatorasyncgenerator)
140
+ - [`<async-generator>.unwind(..)`](#async-generatorunwind)
138
141
  - [`<async-generator>.then(..)` / `<async-generator>.catch(..)` / `<async-generator>.finally(..)`](#async-generatorthen--async-generatorcatch--async-generatorfinally)
139
142
  - [`<async-generator>.iter(..)`](#async-generatoriter)
140
143
  - [`<async-generator>.map(..)` / `<async-generator>.filter(..)` / `<async-generator>.reduce(..)`](#async-generatormap--async-generatorfilter--async-generatorreduce)
@@ -1094,6 +1097,12 @@ This is similar to [`<map>.sort(..)`](#mapsort) and [`Object.sort(..)`](#objects
1094
1097
  see the later for more info.
1095
1098
 
1096
1099
 
1100
+ ### `<set>.filter(..)` / `<set>.map(..)` / `<set>.forEach(..)` / `<set>.reduce(..)` / `<set>.reduceRight(..)`
1101
+
1102
+ For more info see corresponding stoppable methods in
1103
+ [`Array`'s section](#arraysmap--arraysfilter--arraysreduce--arraysforeach).
1104
+
1105
+
1097
1106
 
1098
1107
  ## `Date`
1099
1108
 
@@ -2285,6 +2294,11 @@ _next_ call to `.next()`, regardless of the current generator state.
2285
2294
  XXX
2286
2295
 
2287
2296
 
2297
+ #### `<generator>.unwind(..)`
2298
+
2299
+ XXX
2300
+
2301
+
2288
2302
  #### `<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`
2289
2303
 
2290
2304
  Return a promise and resolve it with the generator value.
@@ -2506,7 +2520,7 @@ Return a function that will return a `<generator>` output as an `Array`.
2506
2520
  XXX
2507
2521
 
2508
2522
 
2509
- #### `<Generator>.then(..)` / `<Generator>.catch(..)` / `<Generator>.finally(..)`
2523
+ #### `<Generator>.unwind(..)`
2510
2524
 
2511
2525
  <!-- XXX -->
2512
2526
 
@@ -2634,6 +2648,8 @@ XXX EXPERIMENTAL
2634
2648
 
2635
2649
  #### `generator.AsyncGenerator`
2636
2650
 
2651
+ #### `<async-generator>.unwind(..)`
2652
+
2637
2653
  #### `<async-generator>.then(..)` / `<async-generator>.catch(..)` / `<async-generator>.finally(..)`
2638
2654
 
2639
2655
  #### `<async-generator>.iter(..)`
package/Set.js CHANGED
@@ -8,11 +8,29 @@
8
8
  /*********************************************************************/
9
9
 
10
10
  var object = require('ig-object')
11
+ var stoppable = require('ig-stoppable')
11
12
 
12
13
 
13
14
 
14
15
  /*********************************************************************/
15
16
 
17
+ // Wrap .map(..) / .filter(..) / .reduce(..) / .. to support STOP...
18
+ //
19
+ // NOTE: internally these are implemented as for-of loops (./generator.js)
20
+ var stoppableSet = function(iter){
21
+ return function(func){
22
+ return new Set([...this][iter](...arguments)) } }
23
+ var stoppableValue = function(iter, no_return=false){
24
+ return function(func){
25
+ var res = [...this][iter](...arguments)
26
+ return no_return ?
27
+ undefined
28
+ : res } }
29
+
30
+
31
+
32
+ //---------------------------------------------------------------------
33
+
16
34
  var SetProtoMixin =
17
35
  module.SetProtoMixin =
18
36
  object.Mixin('SetMixin', 'soft', {
@@ -110,6 +128,12 @@ object.Mixin('SetMixin', 'soft', {
110
128
  this.sort(order)
111
129
 
112
130
  return removed },
131
+
132
+ filter: stoppableSet('filter'),
133
+ map: stoppableSet('map'),
134
+ reduce: stoppableValue('reduce'),
135
+ reduceRight: stoppableValue('reduceRight'),
136
+ forEach: stoppableValue('map', true),
113
137
  })
114
138
 
115
139
 
package/generator.js CHANGED
@@ -189,11 +189,11 @@ var makeGenerator = function(name, pre){
189
189
  ].join('\n ') }, }) } }
190
190
 
191
191
  // XXX do a better doc...
192
- var makePromise = function(name){
192
+ var makeProxy = function(name){
193
193
  return function(...args){
194
194
  var that = this
195
195
  return function(){
196
- return that(...arguments)[name](func) } } }
196
+ return that(...arguments)[name](...args) } } }
197
197
 
198
198
 
199
199
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -267,9 +267,12 @@ object.Mixin('GeneratorMixin', 'soft', {
267
267
 
268
268
  // promises...
269
269
  //
270
- then: makePromise('then'),
271
- catch: makePromise('catch'),
272
- finally: makePromise('finally'),
270
+ // NOTE: .then(..) and friends are intentionally not defined here to
271
+ // prevent control deadlocks when awaiting for a generator that
272
+ // expects manual unwinding e.g.:
273
+ // g = function*(){}
274
+ // await g // will hang waiting for g to resolve
275
+ unwind: makeProxy('unwind'),
273
276
 
274
277
  // combinators...
275
278
  //
@@ -450,7 +453,8 @@ object.Mixin('GeneratorProtoMixin', 'soft', {
450
453
 
451
454
  // promises...
452
455
  //
453
- then: function(onresolve, onreject){
456
+ // NOTE: this will unwind the generator...
457
+ unwind: function(onresolve, onreject){
454
458
  var that = this
455
459
  var p = new Promise(
456
460
  function(resolve){
@@ -459,10 +463,12 @@ object.Mixin('GeneratorProtoMixin', 'soft', {
459
463
  p.then(...arguments)
460
464
  : p
461
465
  return p },
466
+ then: function(...args){
467
+ return this.unwind(...args) },
462
468
  catch: function(func){
463
- return this.then().catch(func) },
469
+ return this.unwind().catch(...arguments) },
464
470
  finally: function(func){
465
- return this.then().finally(func) },
471
+ return this.unwind.finally(...arguments) },
466
472
 
467
473
  // combinators...
468
474
  //
@@ -531,22 +537,23 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
531
537
  //
532
538
  // NOTE: this will unwind the generator...
533
539
  // XXX create an iterator promise???
534
- // XXX should we unwind???
535
- then: function(resolve, reject){
540
+ unwind: function(onresolve, onreject){
536
541
  var that = this
537
- var p = new Promise(async function(_resolve, _reject){
542
+ var p = new Promise(async function(resolve){
538
543
  var res = []
539
544
  for await(var elem of that){
540
545
  res.push(elem) }
541
- _resolve(res) })
542
- p = (resolve || reject) ?
546
+ resolve(res) })
547
+ p = (onresolve || onreject) ?
543
548
  p.then(...arguments)
544
549
  : p
545
550
  return p },
551
+ then: function(...args){
552
+ return this.unwind(...args) },
546
553
  catch: function(func){
547
- return this.then().catch(func) },
548
- finally: function(){
549
- return this.then().finally(func) },
554
+ return this.unwind().catch(...arguments) },
555
+ finally: function(func){
556
+ return this.unwind.finally(...arguments) },
550
557
 
551
558
  // XXX might be a good idea to use this approach above...
552
559
  iter: stoppable(async function*(handler=undefined){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.18.0",
3
+ "version": "6.20.0",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {