From 9978661a42af9a5ab723acb33b281f6232f08952 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 8 Jun 2021 17:00:55 +0300 Subject: [PATCH] added .replaceIndex(..)... Signed-off-by: Alex A. Naanou --- README.md | 25 +++++++++++++++++++++++-- Set.js | 17 +++++++++++++++-- package.json | 2 +- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a8e0bb3..d6512d9 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Library of JavaScript type extensions, types and utilities. - [`.sort(..)`](#mapsort) - [`Set`](#set) - [`.replace(..)`](#setreplace) + - [`.replaceIndex(..)`](#setreplaceindex) - [`.unite(..)`](#setunite) - [`.intersect(..)`](#setintersect) - [`.subtract(..)`](#setsubtract) @@ -896,13 +897,33 @@ Replace value in set with other value retaining item order -> ``` -This might not be cheap for very large sets, to simply replace -the value without sorting use: +Replace the value without sorting ```bnf .replace(, , false) -> ``` +Note that when sorting large sets this can get expensive. + + + +### `.replaceIndex(..)` + +Replace item at position in set retaining order +```bnf +.replace(, ) + -> +``` + +Replace the value at index without sorting +```bnf +.replace(, , false) + -> +``` + +Note that when sorting large sets this can get expensive. + + ### `.unite(..)` diff --git a/Set.js b/Set.js index bb2c7af..d5a8b3b 100644 --- a/Set.js +++ b/Set.js @@ -54,10 +54,23 @@ object.Mixin('SetMixin', 'soft', { var order = [...this] // XXX is this fast enough??? order[order.lastIndexOf(old)] = value } - + // replace... + this.delete(old) + this.add(value) + ordered + && this.sort(order) + return this }, + replaceIndex: function(index, value, ordered=true){ + // nothing to do... + if(this.size < index || old === value){ + return this } + var order = [...this] + var old = order[index] + ordered + && (order[index] = value) + // replace... this.delete(old) this.add(value) - ordered && this.sort(order) return this }, diff --git a/package.json b/package.json index 52ff28e..c0fad7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.5.1", + "version": "6.6.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {