diff --git a/README.md b/README.md index f05aadd..a8e0bb3 100644 --- a/README.md +++ b/README.md @@ -893,10 +893,15 @@ require('ig-types/Set') Replace value in set with other value retaining item order ```bnf .replace(, ) - -> + -> ``` -Note that this might not be cheap for very large sets. +This might not be cheap for very large sets, to simply replace +the value without sorting use: +```bnf +.replace(, , false) + -> +``` ### `.unite(..)` diff --git a/Set.js b/Set.js index cd7fa84..bb2c7af 100644 --- a/Set.js +++ b/Set.js @@ -46,16 +46,20 @@ object.Mixin('SetMixin', 'soft', { add(e) } }.bind(this)) return this }, - replace: function(old, value){ + replace: function(old, value, ordered=true){ // nothing to do... if(!this.has(old) || old === value){ return this } - var order = [...this] - // XXX is this fast enough??? - order[order.lastIndexOf(old)] = value + if(ordered){ + var order = [...this] + // XXX is this fast enough??? + order[order.lastIndexOf(old)] = value } + this.delete(old) this.add(value) - this.sort(order) + + ordered + && this.sort(order) return this }, }) diff --git a/package.json b/package.json index 09eecbf..52ff28e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.5.0", + "version": "6.5.1", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {