added a unorderd option to <set>.replace(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-08 16:49:11 +03:00
parent ab470a9450
commit 31c0755124
3 changed files with 17 additions and 8 deletions

View File

@ -893,10 +893,15 @@ require('ig-types/Set')
Replace value in set with other value retaining item order
```bnf
<set>.replace(<old>, <new>)
-> <union-set>
-> <set>
```
Note that this might not be cheap for very large sets.
This might not be cheap for very large sets, to simply replace
the value without sorting use:
```bnf
<set>.replace(<old>, <new>, false)
-> <set>
```
### `<set>.unite(..)`

14
Set.js
View File

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

View File

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