added filter/map/reduce to Set -- odd that I did not notice they were missing untill now...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-08-22 15:32:13 +03:00
parent 993faa672d
commit 228f7f746e
2 changed files with 25 additions and 1 deletions

24
Set.js
View File

@ -8,11 +8,29 @@
/*********************************************************************/
var object = require('ig-object')
var stoppable = require('ig-stoppable')
/*********************************************************************/
// Wrap .map(..) / .filter(..) / .reduce(..) / .. to support STOP...
//
// NOTE: internally these are implemented as for-of loops (./generator.js)
var stoppableSet = function(iter){
return function(func){
return new Set([...this][iter](...arguments)) } }
var stoppableValue = function(iter, no_return=false){
return function(func){
var res = [...this][iter](...arguments)
return no_return ?
undefined
: res } }
//---------------------------------------------------------------------
var SetProtoMixin =
module.SetProtoMixin =
object.Mixin('SetMixin', 'soft', {
@ -110,6 +128,12 @@ object.Mixin('SetMixin', 'soft', {
this.sort(order)
return removed },
filter: stoppableSet('filter'),
map: stoppableSet('map'),
reduce: stoppableValue('reduce'),
reduceRight: stoppableValue('reduceRight'),
forEach: stoppableValue('map', true),
})

View File

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