From 2e66cb3083ba46bd4648758f95309ef215e4b108 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 26 Dec 2020 07:27:23 +0300 Subject: [PATCH] started work on ObjectWithAutoKeys(..)... Signed-off-by: Alex A. Naanou --- containers.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/containers.js b/containers.js index 04b11c6..2431ee2 100644 --- a/containers.js +++ b/containers.js @@ -175,6 +175,34 @@ object.Constructor('UniqueKeyMap', Map, { }) +//--------------------------------------------------------------------- + +// XXX should this be a map??? +// XXX make two variants: ordered and unordered... +var ObjectWithAutoKeys = +module.ObjectWithAutoKeys = +object.Constructor('ObjectWithAutoKeys', { + + __last_key: null, + __new_key__: function(value){ + return (this.__last_key = (this.__last_key || -1) + 1) }, + + // + // .push(value) + // -> key + // + // .push(value, ..) + // -> [key, ..] + push: function(...values){ + var that = this + var res = values + .map(function(value){ + that[that.__new_key__(value)] = value }) + return values.length == 1 ? + res.pop() + : res }, +}) + /**********************************************************************