From 1f2faf9e111b232dbaa7b42196b602bb214ed23f Mon Sep 17 00:00:00 2001 From: gasim Date: Sun, 11 Aug 2024 12:46:33 +0200 Subject: [PATCH] Add algorithm for console table --- index.bs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/index.bs b/index.bs index db579fb..36e00c4 100644 --- a/index.bs +++ b/index.bs @@ -122,10 +122,47 @@ its \[[Prototype]] an empty object, created as if by

table(|tabularData|, |properties|)

Try to construct a table with the columns of the properties of |tabularData| (or use -|properties|) and rows of |tabularData| and log it with a logLevel of "log". Fall -back to just logging the argument if it can't be parsed as tabular. - -

TODO: This will need a good algorithm.

+|properties|) and rows of |tabularData| and log it with a logLevel of "log". + +1. Let |finalRows| be the new [=/list=], initially empty +2. Let |finalColumns| be the new [=/list=], initially empty +3. If |tabularData| is a [=/list=], then: + 1. Let |indices| be [=list/get the indices=] of |tabularData| + 2. [=list/For each=] |index| of |indices| + 1. Let |value| be |tabularData|[|index|] + 2. Perform [create table row] with |value|, |index|, |finalColumns|, |properties| that returns |row| + 3. [=list/Append=] |row| to |finalRows| +4. Otherwise, if |tabularData| is a [=/map=], then: + 1. [=map/For each=] |key| → |value| of |tabularData| + 1. Perform [create table row] with |value|, |key|, |finalColumns|, and |properties| that returns |row| + 2. [=list/Append=] |row| to |finalRows| +5. If |finalRows| is not [=list/empty=], then: + 1. Let |finalData| be a new [=/map=]. + 2. [=map/Set=] |finalData|["rows"] to |finalRows| + 3. [=map/Set=] |finalData|["columns"] to |finalColumns| + 3. Perform Printer("table", « |finalData| »). +6. Otherwise, perform Printer("log", « |tabularData| »). + +To [create table row] given |tabularDataItem|, |rowIndex|, [=/list=] |finalColumns|, and optional [=/list=] |properties|, perform the following steps: + +1. Let |row| be a new [=/map=] +2. [=map/Set=] |row|["(index)"] to |rowIndex| +3. If |tabularDataItem| is a [=/list=], then: + 1. Let |indices| be [=list/get the indices=] of |tabularDataItem| + 2. [=list/For each=] |index| of |indices| + 1. Let |value| be |tabularDataItem|[|index|] + 2. If |properties| is not [=list/empty=] and |properties| does not [=list/contain=] |index|, [=iteration/continue=] + 3. [=map/Set=] |row|[|index|] to |value| + 4. If |finalColumns| does not [=list/contain=] |index|, [=list/append=] |index| to |finalColumns| +4. Otherwise, if |tabularDataItem| is a [=/map=], then: + 1. [=map/For each=] |key| → |value| of |tabularDataItem| + 1. If |properties| is not [=list/empty=] and |properties| does not [=list/contain=] |key|, [=iteration/continue=] + 2. [=map/Set=] |row|[|key|] to |value| + 3. If |finalColumns| does not [=list/contain=] |key|, [=list/append=] |key| to |finalColumns| +5. Otherwise: + 1. [=map/set=] |row|["Value"] to |tabularDataItem| + 2. If |finalColumns| does not [=list/contain=] "Value", [=list/append=] "Value" to |finalColumns| +6. Return |row|

trace(...|data|)