added <set>.replace(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-08 16:44:51 +03:00
parent e3992f3d0a
commit ab470a9450
3 changed files with 25 additions and 1 deletions

View File

@ -38,6 +38,7 @@ Library of JavaScript type extensions, types and utilities.
- [`Map`](#map)
- [`<map>.sort(..)`](#mapsort)
- [`Set`](#set)
- [`<set>.replace(..)`](#setreplace)
- [`<set>.unite(..)`](#setunite)
- [`<set>.intersect(..)`](#setintersect)
- [`<set>.subtract(..)`](#setsubtract)
@ -887,6 +888,17 @@ require('ig-types/Set')
```
### `<set>.replace(..)`
Replace value in set with other value retaining item order
```bnf
<set>.replace(<old>, <new>)
-> <union-set>
```
Note that this might not be cheap for very large sets.
### `<set>.unite(..)`
Unite two sets and return the resulting set

12
Set.js
View File

@ -45,6 +45,18 @@ object.Mixin('SetMixin', 'soft', {
del(e)
add(e) } }.bind(this))
return this },
replace: function(old, value){
// nothing to do...
if(!this.has(old) || old === value){
return this }
var order = [...this]
// XXX is this fast enough???
order[order.lastIndexOf(old)] = value
this.delete(old)
this.add(value)
this.sort(order)
return this },
})

View File

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