mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 10:30:08 +00:00
added <set>.replaceIndex(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
31c0755124
commit
9978661a42
25
README.md
25
README.md
@ -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
17
Set.js
@ -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 },
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user