added <map>.replaceKey(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-08 18:43:02 +03:00
parent 9978661a42
commit 4f41f81f70
3 changed files with 34 additions and 1 deletions

14
Map.js
View File

@ -40,6 +40,20 @@ object.Mixin('MapProtoMixin', 'soft', {
del(k)
set(k, v) } }.bind(this))
return this },
replaceKey: function(old, key, ordered=true){
if(!this.has(old)){
return this }
if(ordered){
var order = [...this.keys()]
order[order.lastIndexOf(old)] = key }
// replace...
var value = this.get(old)
this.delete(old)
this.set(key, value)
ordered
&& this.sort(order)
return this },
})

View File

@ -36,6 +36,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<array>.CHUNK_SIZE`](#arraychunk_size)
- [`<array>.mapChunks(..)` / `<array>.filterChunks(..)` / `<array>.reduceChunks(..)`](#arraymapchunks--arrayfilterchunks--arrayreducechunks)
- [`Map`](#map)
- [`<map>.replaceKey(..)`](#mapreplacekey)
- [`<map>.sort(..)`](#mapsort)
- [`Set`](#set)
- [`<set>.replace(..)`](#setreplace)
@ -858,6 +859,24 @@ require('ig-types/Map')
```
### `<map>.replaceKey(..)`
Replace key in map retaining item order
```bnf
<map>.replaceKey(<old>, <new>)
-> <map>
```
Replace the key without sorting
```bnf
<map>.replaceKey(<old>, <new>, false)
-> <map>
```
Note that when sorting large maps this can get expensive.
### `<map>.sort(..)`
Sort `<map>` keys in-place

View File

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