From beb77656ccd66e34fad95ab296e547018ae013c9 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 7 Oct 2020 14:48:47 +0300 Subject: [PATCH] experimenting + docs... Signed-off-by: Alex A. Naanou --- README.md | 35 +++++++++++++++++++++++++++++++++-- containers.js | 21 ++++++++++++--------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 39efe4c..d93bce7 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,13 @@ A library of JavaScript type extensions, types and type utilities. - [`RegExp.quoteRegExp(..)`](#regexpquoteregexp) - [Containers](#containers) - [`containers.UniqueKeyMap()` (`Map`)](#containersuniquekeymap-map) + - [`.set(..)`](#unique-key-mapset) - [`.reset(..)`](#unique-key-mapreset) - - [`.uniqueKey(..)`](#unique-key-mapuniquekey) - [`.rename(..)`](#unique-key-maprename) + - [`.unorderedRename(..)`](#unique-key-mapunorderedrename) - [`.keysOf(..)`](#unique-key-mapkeysof) + - [`.originalKey(..)`](#unique-key-maporiginalkey) + - [`.uniqueKey(..)`](#unique-key-mapuniquekey) - [`.__key_pattern__`](#unique-key-map__key_pattern__) - [License](#license) @@ -298,14 +301,42 @@ For more info on `Map` see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map +#### `.set(..)` + +``` +.reset(, ) + -> + +.reset(, , true) + -> +``` + +Add an `` to ``. + +If `` already exists then add an index to it to make it unique. + +Key updating is done via [`.__key_pattern__`](#unique-key-map__key_pattern__). + + #### `.reset(..)` -#### `.uniqueKey(..)` +``` +.reset(, ) + -> +``` + +Explicitly write an `` under `` as-is, this is like `Map`'s `.set(..)`. #### `.rename(..)` +#### `.unorderedRename(..)` + #### `.keysOf(..)` +#### `.originalKey(..)` + +#### `.uniqueKey(..)` + #### `.__key_pattern__` diff --git a/containers.js b/containers.js index fded968..54c694d 100644 --- a/containers.js +++ b/containers.js @@ -58,6 +58,8 @@ module.UniqueKeyMap = object.Constructor('UniqueKeyMap', Map, { // __unique_key_value__: false, + __unorderd_writes__: false, + // helpers... // @@ -152,16 +154,12 @@ module.UniqueKeyMap = object.Constructor('UniqueKeyMap', Map, { s.size == 0 && this.__keys.delete(this.get(key)) } return object.parentCall(UniqueKeyMap.prototype, 'delete', this, key) }, - // XXX this affects order... - rename: function(from, to, return_key=false){ - var e = this.get(from) - this.delete(from) - return this.set(to, e, return_key) }, - // XXX in-place... - // XXX rename to .rename(..) - // XXX do not se how can we avoid rewriting the map if we want to + // NOTE: this maintains the item order. This is done by rewriting + // items in sequence, this may be slow and trigger lots of write + // observer callbacks. to avoid this use .unOrderedRename(..) + // XXX do not see how can we avoid rewriting the map if we want to // keep order... - _rename: function(from, to, return_key=false){ + rename: function(from, to, return_key=false){ var keys = [...this.keys()] // rename the element... var e = this.get(from) @@ -173,6 +171,11 @@ module.UniqueKeyMap = object.Constructor('UniqueKeyMap', Map, { return return_key ? n : this }, + // NOTE: this affects order... + unorderedRename: function(from, to, return_key=false){ + var e = this.get(from) + this.delete(from) + return this.set(to, e, return_key) }, })