mirror of
https://github.com/flynx/types.js.git
synced 2025-12-20 10:21:38 +00:00
added <map>.replaceKey(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
9978661a42
commit
4f41f81f70
14
Map.js
14
Map.js
@ -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 },
|
||||
})
|
||||
|
||||
|
||||
|
||||
19
README.md
19
README.md
@ -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
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user