diff --git a/README.md b/README.md index 49ef5e3..1cfdac5 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ Library of JavaScript type extensions, types and utilities. - [`.at(..)` / `.gat(..)`](#generatorat--generatorgat) - [`.flat(..)`](#generatorflat) - [`.shift()` / `.pop()` / `.gshift()` / `.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop) + - [`.unshift(..)` / `.push(..)`](#generatorunshift--generatorpush) - [`.promise()`](#generatorpromise) - [`.then(..)` / `.catch(..)` / `.finally(..)`](#generatorthen--generatorcatch--generatorfinally) - [`.toArray()`](#generatortoarray) @@ -95,6 +96,7 @@ Library of JavaScript type extensions, types and utilities. - [`.iter(..)`](#generatoriter-2) - [`.at(..)` / `.gat(..)`](#generatorat--generatorgat-1) - [`.shift()` / `.pop()` / `.gshift()` / `.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop-1) + - [`.unshift(..)` / `.push(..)`](#generatorunshift--generatorpush-1) - [`.slice(..)`](#generatorslice-1) - [`.map(..)` / `.filter(..)` / `.reduce(..)` / `.flat()`](#generatormap--generatorfilter--generatorreduce--generatorflat) - [`.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. +#### `.unshift(..)` / `.push(..)` + +Add a value to the generator sequence at start/end. +```bnf +.unshift() +.push() + -> +``` + +Value added by `.unshift(..)` will be yielded by `` "first", i.e. on _next_ call to `.next()`, regardless of the current generator state. + + #### `.promise()` @@ -2021,6 +2035,17 @@ currently which may not be the first element in the sequence. Equivalents to [``'s `.shift(..)`/`.pop(..)`/..](#generatorshift--generatorpop--generatorgshift--generatorgpop) but returning a reusable ``/``. +#### `.unshift(..)` / `.push(..)` + +```bnf +.unshift() +.push() + -> +``` + +Equivalents to [``'s `.unshift(..)`/`.push(..)`](#generatorunshift--generatorpush) +but returning a reusable ``. + #### `.slice(..)` diff --git a/generator.js b/generator.js index 4d505d6..a10888d 100644 --- a/generator.js +++ b/generator.js @@ -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... // diff --git a/package.json b/package.json index 97e80e7..5f80d66 100644 --- a/package.json +++ b/package.json @@ -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": {