From cc4fbc6bdbbca78527bedc35c61b249169b34100 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 13 Jun 2021 13:59:20 +0300 Subject: [PATCH] added .splice(..)... Signed-off-by: Alex A. Naanou --- README.md | 112 +++++++++++++++++++++++++++++---------------------- Set.js | 13 ++++++ package.json | 2 +- 3 files changed, 77 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 4967af8..1272383 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,12 @@ Library of JavaScript type extensions, types and utilities. - [`.replaceKey(..)`](#mapreplacekey) - [`.sort(..)`](#mapsort) - [`Set`](#set) - - [`.replace(..)`](#setreplace) - - [`.replaceAt(..)`](#setreplaceat) - [`.unite(..)`](#setunite) - [`.intersect(..)`](#setintersect) - [`.subtract(..)`](#setsubtract) + - [`.splice(..)`](#setsplice) + - [`.replace(..)`](#setreplace) + - [`.replaceAt(..)`](#setreplaceat) - [`.sort(..)`](#setsort) - [`Date`](#date) - [`Date.timeStamp(..)`](#datetimestamp) @@ -909,51 +910,6 @@ require('ig-types/Set') ``` -### `.replace(..)` - -Replace value in set with other value retaining item order -```bnf -.replace(, ) -.replace(, , true) - -> -``` - -Replace the value without sorting -```bnf -.replace(, , false) - -> -``` - -Note that when sorting large sets this can get expensive. - - - -### `.replaceAt(..)` - -Replace item at position in set retaining order -```bnf -.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 -.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. - - - ### `.unite(..)` Unite two sets and return the resulting set @@ -983,9 +939,67 @@ Subtract `` from set and return resulting set ``` +### `.splice(..)` + +In-place splice a set +```bnf +.splice() +.splice(, ) +.splice(, , ...) + -> +``` + +This is the same as +[`.splice(..)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) +but without the ability to add more than one instance of an item. + + +### `.replace(..)` + +Replace value in set with other value retaining item order (in-place) +```bnf +.replace(, ) +.replace(, , true) + -> +``` + +Replace the value without sorting +```bnf +.replace(, , false) + -> +``` + +Note that when sorting large sets this can get expensive. + + +### `.replaceAt(..)` + +Replace item at position in set retaining order (in-place) +```bnf +.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 +.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. + + ### `.sort(..)` -Sort `` keys in-place +Sort `` (in-place) ```bnf .sort() -> @@ -1003,11 +1017,11 @@ as a list -> ``` - This is similar to [`.sort(..)`](#mapsort) and [`Object.sort(..)`](#objectsort), see the later for more info. + ## `Date` ```javascript diff --git a/Set.js b/Set.js index aed9928..7cf35f3 100644 --- a/Set.js +++ b/Set.js @@ -63,6 +63,7 @@ object.Mixin('SetMixin', 'soft', { // 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. + // XXX should this be implemented via .splice(..) ??? replaceAt: function(index, value, ordered=true){ // append... if(index >= this.size){ @@ -90,6 +91,18 @@ object.Mixin('SetMixin', 'soft', { ordered && this.sort(order) return this }, + + splice: function(from, count, ...items){ + var that = this + var order = [...this] + var removed = order.splice(...arguments) + + // update the set... + removed.forEach(this.delete.bind(this)) + items.forEach(this.add.bind(this)) + this.sort(order) + + return removed }, }) diff --git a/package.json b/package.json index c06af6f..733d6c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.8.1", + "version": "6.9.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {