diff --git a/Map.js b/Map.js index ff52e56..08ee2b4 100644 --- a/Map.js +++ b/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 }, }) diff --git a/README.md b/README.md index d6512d9..f3376e0 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Library of JavaScript type extensions, types and utilities. - [`.CHUNK_SIZE`](#arraychunk_size) - [`.mapChunks(..)` / `.filterChunks(..)` / `.reduceChunks(..)`](#arraymapchunks--arrayfilterchunks--arrayreducechunks) - [`Map`](#map) + - [`.replaceKey(..)`](#mapreplacekey) - [`.sort(..)`](#mapsort) - [`Set`](#set) - [`.replace(..)`](#setreplace) @@ -858,6 +859,24 @@ require('ig-types/Map') ``` +### `.replaceKey(..)` + +Replace key in map retaining item order +```bnf +.replaceKey(, ) + -> +``` + +Replace the key without sorting +```bnf +.replaceKey(, , false) + -> +``` + +Note that when sorting large maps this can get expensive. + + + ### `.sort(..)` Sort `` keys in-place diff --git a/package.json b/package.json index c0fad7f..f1d5205 100644 --- a/package.json +++ b/package.json @@ -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": {