added .push(..)/.unshift(..) to generators...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-26 10:51:49 +03:00
parent c735bc84d1
commit 3a29f4b5c4
3 changed files with 34 additions and 1 deletions

View File

@ -87,6 +87,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<generator>.at(..)` / `<generator>.gat(..)`](#generatorat--generatorgat)
- [`<generator>.flat(..)`](#generatorflat)
- [`<generator>.shift()` / `<generator>.pop()` / `<generator>.gshift()` / `<generator>.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop)
- [`<generator>.unshift(..)` / `<generator>.push(..)`](#generatorunshift--generatorpush)
- [`<generator>.promise()`](#generatorpromise)
- [`<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`](#generatorthen--generatorcatch--generatorfinally)
- [`<generator>.toArray()`](#generatortoarray)
@ -95,6 +96,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<Generator>.iter(..)`](#generatoriter-2)
- [`<Generator>.at(..)` / `<Generator>.gat(..)`](#generatorat--generatorgat-1)
- [`<Generator>.shift()` / `<Generator>.pop()` / `<Generator>.gshift()` / `<Generator>.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop-1)
- [`<generator>.unshift(..)` / `<generator>.push(..)`](#generatorunshift--generatorpush-1)
- [`<Generator>.slice(..)`](#generatorslice-1)
- [`<Generator>.map(..)` / `<Generator>.filter(..)` / `<Generator>.reduce(..)` / `<Generator>.flat()`](#generatormap--generatorfilter--generatorreduce--generatorflat)
- [`<Generator>.toArray()`](#generatortoarray-1)
@ -1861,6 +1863,18 @@ Note that `.shift()`/`.gshift()` will yield the item the generator is at at
time of call, this may not be the _first_ item if the generator is partially
depleted.
#### `<generator>.unshift(..)` / `<generator>.push(..)`
Add a value to the generator sequence at start/end.
```bnf
<generator>.unshift(<value>)
<generator>.push(<value>)
-> <generator>
```
Value added by `.unshift(..)` will be yielded by `<generator>` "first", i.e. on _next_ call to `.next()`, regardless of the current generator state.
#### `<generator>.promise()`
@ -2021,6 +2035,17 @@ currently which may not be the first element in the sequence.
Equivalents to [`<generator>`'s `.shift(..)`/`.pop(..)`/..](#generatorshift--generatorpop--generatorgshift--generatorgpop)
but returning a reusable `<func>`/`<Generator>`.
#### `<generator>.unshift(..)` / `<generator>.push(..)`
```bnf
<Generator>.unshift(<value>)
<Generator>.push(<value>)
-> <Generator>
```
Equivalents to [`<generator>`'s `.unshift(..)`/`.push(..)`](#generatorunshift--generatorpush)
but returning a reusable `<Generator>`.
#### `<Generator>.slice(..)`

View File

@ -231,6 +231,7 @@ object.Mixin('GeneratorMixin', 'soft', {
{ toString: function(){
return that.toString()
+ '\n .gpop()'}, }) },
push: makeGenerator('push'),
gshift: makeGenerator('gshift'),
shift: function(){
var that = this
@ -241,6 +242,7 @@ object.Mixin('GeneratorMixin', 'soft', {
{ toString: function(){
return that.toString()
+ '\n .gshift()'}, }) },
unshift: makeGenerator('unshift'),
// promises...
//
@ -360,11 +362,17 @@ object.Mixin('GeneratorProtoMixin', 'soft', {
// XXX this needs the value to be iterable...
gpop: function*(){
yield [...this].pop() },
push: function*(value){
yield* this
yield value },
shift: function(){
return this.next().value },
// XXX this needs the value to be iterable...
gshift: function*(){
yield this.next().value },
unshift: function*(value){
yield value
yield* this },
// non-generators...
//

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "6.9.2",
"version": "6.9.3",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {