some cleanup and minor fixes...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-09 12:17:42 +03:00
parent 58483dce17
commit 46cc38fcf7
3 changed files with 34 additions and 8 deletions

View File

@ -864,6 +864,7 @@ require('ig-types/Map')
Replace key in map retaining item order
```bnf
<map>.replaceKey(<old>, <new>)
<map>.replaceKey(<old>, <new>, true)
-> <map>
```
@ -913,6 +914,7 @@ require('ig-types/Set')
Replace value in set with other value retaining item order
```bnf
<set>.replace(<old>, <new>)
<set>.replace(<old>, <new>, true)
-> <set>
```
@ -930,16 +932,24 @@ Note that when sorting large sets this can get expensive.
Replace item at position in set retaining order
```bnf
<set>.replace(<old>, <new>)
<set>.replaceAt(<index>, <new>)
<set>.replaceAt(<index>, <new>, true)
-> <set>
```
If `<index>` is less than `0` the `<new>` item will be prepended to `<set>`,
if the `<index>` is greater than or equal to `<set>.size` then `<new>` is
appended.
Replace the value at index without sorting
```bnf
<set>.replace(<old>, <new>, false)
<set>.replaceAt(<index>, <new>, false)
-> <set>
```
Here, if `<index>` is less than `0` or greater than or equal to `<set>.size`
`<new>` will always be appended to `<set>`.
Note that when sorting large sets this can get expensive.

26
Set.js
View File

@ -60,17 +60,33 @@ object.Mixin('SetMixin', 'soft', {
ordered
&& this.sort(order)
return this },
// NOTE: if index is <0 then the value is prepended to the set, if
// it's >=this.size then the value will be appended.
// if ordered is set to false in both cases the value is appended.
replaceAt: function(index, value, ordered=true){
// nothing to do...
if(this.size < index || old === value){
// append...
if(index >= this.size){
this.add(value)
return this }
var order = [...this]
var old = order[index]
// prepend...
if(index < 0){
index = 0
var order = [, ...this]
// replace...
} else {
var order = [...this]
var old = order[index]
// nothing to do...
if(old === value){
return this } }
ordered
&& (order[index] = value)
// replace...
this.delete(old)
this.has(old)
&& this.delete(old)
this.add(value)
ordered
&& this.sort(order)
return this },

View File

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