added <set>.replaceIndex(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-08 17:00:55 +03:00
parent 31c0755124
commit 9978661a42
3 changed files with 39 additions and 5 deletions

View File

@ -39,6 +39,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<map>.sort(..)`](#mapsort)
- [`Set`](#set)
- [`<set>.replace(..)`](#setreplace)
- [`<set>.replaceIndex(..)`](#setreplaceindex)
- [`<set>.unite(..)`](#setunite)
- [`<set>.intersect(..)`](#setintersect)
- [`<set>.subtract(..)`](#setsubtract)
@ -896,13 +897,33 @@ Replace value in set with other value retaining item order
-> <set>
```
This might not be cheap for very large sets, to simply replace
the value without sorting use:
Replace the value without sorting
```bnf
<set>.replace(<old>, <new>, false)
-> <set>
```
Note that when sorting large sets this can get expensive.
### `<set>.replaceIndex(..)`
Replace item at position in set retaining order
```bnf
<set>.replace(<old>, <new>)
-> <set>
```
Replace the value at index without sorting
```bnf
<set>.replace(<old>, <new>, false)
-> <set>
```
Note that when sorting large sets this can get expensive.
### `<set>.unite(..)`

17
Set.js
View File

@ -54,10 +54,23 @@ object.Mixin('SetMixin', 'soft', {
var order = [...this]
// XXX is this fast enough???
order[order.lastIndexOf(old)] = value }
// replace...
this.delete(old)
this.add(value)
ordered
&& this.sort(order)
return this },
replaceIndex: function(index, value, ordered=true){
// nothing to do...
if(this.size < index || old === value){
return this }
var order = [...this]
var old = order[index]
ordered
&& (order[index] = value)
// replace...
this.delete(old)
this.add(value)
ordered
&& this.sort(order)
return this },

View File

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