From ab470a9450359a742f3668fcacdaee5fe38f7eee Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 8 Jun 2021 16:44:51 +0300 Subject: [PATCH] added .replace(..)... Signed-off-by: Alex A. Naanou --- README.md | 12 ++++++++++++ Set.js | 12 ++++++++++++ package.json | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59494e7..f05aadd 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Library of JavaScript type extensions, types and utilities. - [`Map`](#map) - [`.sort(..)`](#mapsort) - [`Set`](#set) + - [`.replace(..)`](#setreplace) - [`.unite(..)`](#setunite) - [`.intersect(..)`](#setintersect) - [`.subtract(..)`](#setsubtract) @@ -887,6 +888,17 @@ require('ig-types/Set') ``` +### `.replace(..)` + +Replace value in set with other value retaining item order +```bnf +.replace(, ) + -> +``` + +Note that this might not be cheap for very large sets. + + ### `.unite(..)` Unite two sets and return the resulting set diff --git a/Set.js b/Set.js index 7b27545..cd7fa84 100644 --- a/Set.js +++ b/Set.js @@ -45,6 +45,18 @@ object.Mixin('SetMixin', 'soft', { del(e) add(e) } }.bind(this)) return this }, + + replace: function(old, value){ + // nothing to do... + if(!this.has(old) || old === value){ + return this } + var order = [...this] + // XXX is this fast enough??? + order[order.lastIndexOf(old)] = value + this.delete(old) + this.add(value) + this.sort(order) + return this }, }) diff --git a/package.json b/package.json index 44f5489..09eecbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.4.0", + "version": "6.5.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {