From 46cc38fcf76c85cc49edf46b66b106e91ca71ccd Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 9 Jun 2021 12:17:42 +0300 Subject: [PATCH] some cleanup and minor fixes... Signed-off-by: Alex A. Naanou --- README.md | 14 ++++++++++++-- Set.js | 26 +++++++++++++++++++++----- package.json | 2 +- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8f72d49..4967af8 100644 --- a/README.md +++ b/README.md @@ -864,6 +864,7 @@ require('ig-types/Map') Replace key in map retaining item order ```bnf .replaceKey(, ) +.replaceKey(, , true) -> ``` @@ -913,6 +914,7 @@ require('ig-types/Set') Replace value in set with other value retaining item order ```bnf .replace(, ) +.replace(, , true) -> ``` @@ -930,16 +932,24 @@ Note that when sorting large sets this can get expensive. Replace item at position in set retaining order ```bnf -.replace(, ) +.replaceAt(, ) +.replaceAt(, , true) -> ``` +If `` is less than `0` the `` item will be prepended to ``, +if the `` is greater than or equal to `.size` then `` is +appended. + Replace the value at index without sorting ```bnf -.replace(, , false) +.replaceAt(, , false) -> ``` +Here, if `` is less than `0` or greater than or equal to `.size` +`` will always be appended to ``. + Note that when sorting large sets this can get expensive. diff --git a/Set.js b/Set.js index 0e83feb..aed9928 100644 --- a/Set.js +++ b/Set.js @@ -60,17 +60,33 @@ object.Mixin('SetMixin', 'soft', { ordered && this.sort(order) return this }, + // NOTE: if index is <0 then the value is prepended to the set, if + // it's >=this.size then the value will be appended. + // if ordered is set to false in both cases the value is appended. replaceAt: function(index, value, ordered=true){ - // nothing to do... - if(this.size < index || old === value){ + // append... + if(index >= this.size){ + this.add(value) return this } - var order = [...this] - var old = order[index] + // prepend... + if(index < 0){ + index = 0 + var order = [, ...this] + // replace... + } else { + var order = [...this] + var old = order[index] + // nothing to do... + if(old === value){ + return this } } ordered && (order[index] = value) + // replace... - this.delete(old) + this.has(old) + && this.delete(old) this.add(value) + ordered && this.sort(order) return this }, diff --git a/package.json b/package.json index 9846ae8..c06af6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.8.0", + "version": "6.8.1", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {